Using tags in Robot Framework is a simple, yet powerful mechanism for classifying test cases. Tags are free text and they can be used at least for the following purposes:

1. Tags are shown in test reports, logs, and, of course, in the test data, so they provide metadata to test cases.
2. Statistics about test cases (total, passed, failed are automatically collected based on tags).
3. With tags, you can include or exclude test cases to be executed.
4. With tags, you can specify which test cases should be skipped.

Let’s write a simple test to further deep dive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
*** Settings ***
Documentation  Tags in Robot Framework

*** Variables ***

*** Test Cases ***
Test Case 1
    [tags]  Smoke
    Log To Console  This is test case 1

Test Case 2
    [tags]  Regression
    Log To Console  This is test case 2

Test Case 3
    [tags]  Regression
    Log To Console  This is test case 3

Test Case 4
    [tags]  Smoke
    Log To Console  This is test case 4

Test Case 5
    [tags]  Sanity
    Log To Console  This is test case 5

*** Keywords ***

robot framework test script with tags
 
Use Case 1: Only execute Tests with ‘Smoke’ Tag.

1
robot -d results -i Smoke Tests/Tags.robot

-i Smoke (Include Smoke) will ensure only tests with ‘Smoke’ tag are executed. So from our test, only Test Case 1 and Test Case 4 will be executed.

robot framework include one tag test execution
 
Use Case 2: Only execute Tests with ‘Smoke’ and ‘Sanity’ Tags.

1
robot -d results -i Smoke -i Sanity Tests/Tags.robot

-i Smoke -i Sanity will ensure only tests with ‘Smoke’ and ‘Sanity’ tags are executed. So from our test, only Test Case 1, Test Case 4, and Test Case 5 will be executed.

robot framework include multiple tag test execution
 
Use Case 3: Execute all tests excluding ‘Smoke’ Tag.

1
robot -d results -e Smoke Tests/Tags.robot

-e Smoke (Exclude Smoke) will ensure that all tests are executed except the ones with the ‘Smoke’ Tag. So from our test, only Test Case 2, Test Case 3, and Test Case 5 will be executed.

robot framework exclude tag test execution
 
Use Case 4: Execute all tests excluding ‘Smoke’ and ‘Regression’ Tags.

1
robot -d results -e Smoke -e Regression Tests/Tags.robot

-e Smoke -e Regression will ensure that all tests are executed except the ones with the ‘Smoke’ and ‘Regression’ Tags. So from our test, only Test Case 5 will be executed.

robot framework exclude multiple tag test execution
 
Use Case 5: Execute all tests with ‘Sanity’ Tag. All of the above examples were focussed to just one Test suite. In case we want to execute all test suites in the project we will just replace Tests/Tags.robot with Tests. ‘Tests’ is the folder where we have all our Test Suites (or robot files).

1
robot -d results -i Sanity Tests

Apply Tags to entire robot framework project
 
Another way to apply tags to the test cases is by using Force Tags in the Settings section. All test cases in a test case file with this setting will always get the specified tag(s). If it is used in the test suite initialization file, all test cases in sub-test suites get these tags. So let’s add a Forced Tag Testersdock into our test suite and also add a test case(Test Case 6) without any tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
*** Settings ***
Documentation  Tags in Robot Framework
Force Tags  Testersdock

*** Variables ***

*** Test Cases ***
Test Case 1
    [tags]  Smoke
    Log To Console  This is test case 1

Test Case 2
    [tags]  Regression
    Log To Console  This is test case 2

Test Case 3
    [tags]  Regression
    Log To Console  This is test case 3

Test Case 4
    [tags]  Smoke
    Log To Console  This is test case 4

Test Case 5
    [tags]  Sanity
    Log To Console  This is test case 5

Test Case 6
    Log To Console  This is test case 6

*** Keywords ***

robot test suite with force tags

Now let’s execute the test suite with our newly added Forced tag Testersdock using the command:

1
robot -d results -i Testersdock Tests/Tags.robot

As you can see above irrespective of whether individual test cases had tags or not, all of the test cases were executed. Also, the tags applied to each test case will continue to work as before.
 
Do check out 🙂

Github: https://github.com/alapanme/Robot-Framework
All Robot Framework Articles: https://testersdock.com/robot-framework-tutorial/