Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Syntax and Test Cases

Tags, Setup and Teardown

Use tags to group and select tests, and use suite and test setup and teardown to prepare and clean up. Learn the execution order and teardown behavior.

Two features turn a pile of tests into a manageable suite. Tags label tests so you can select, exclude and report on groups. Setup and teardown run preparation and cleanup keywords around tests and suites, with rules that guarantee cleanup happens even after a failure.

Tags

A tag is a short label. Tags are case-insensitive, and spaces and underscores in them are ignored, so Smoke Test and smoke_test are the same tag. There are several ways to attach them.

ROBOTFRAMEWORK
*** Settings ***
Test Tags         shop    regression

*** Test Cases ***
Add item to cart
    [Tags]    smoke    cart
    Add Item    book

Remove item from cart
    [Tags]    cart    -regression
    Remove Item    book

Export orders
    [Tags]    slow    robot:skip
    Export All Orders
  • Test Tags in Settings adds tags to every test of the file.
  • [Tags] adds tags to one test. Since Robot 6.0 a tag that starts with - removes a tag inherited from Test Tags. In the example "Remove item from cart" ends up with shop and cart but not regression.
  • Inside a running test, Set Tags and Remove Tags change tags dynamically.

You use tags to select tests (--include, --exclude, see Command-Line Options), to collect statistics in the report, and to mark known issues.

Reserved tags

Tags starting with robot: have special meaning:

Tag Effect
robot:skip The test is skipped
robot:exclude The test is not run and not shown in results
robot:no-dry-run The test or keyword is not run in --dryrun
robot:continue-on-failure A failing step does not stop the test
robot:stop-on-failure Cancels continue-on-failure for that part
robot:private A keyword that may only be used in its own file (Robot 6.0 and later)

Do not invent your own tags starting with robot:.

Tag naming tips

Use a short, consistent vocabulary: smoke, regression, api, ui, slow, and an issue-tracker pattern such as bug-1234. A pattern like bug-* is easy to select. Keep a list of allowed tags in the project documentation; misspelled tags quietly create a second group.

Setup and teardown

A setup prepares the world; a teardown cleans up. There are two levels, each with a setting in the Settings section.

ROBOTFRAMEWORK
*** Settings ***
Suite Setup       Start Test Server
Suite Teardown    Stop Test Server
Test Setup        Log In As Admin
Test Teardown     Log Out

*** Test Cases ***
Create user
    [Documentation]    Uses the file-level setup and teardown.
    Create User    ann

Check public page
    [Setup]       NONE
    [Teardown]    Take Screenshot
    Open Public Page
    Page Should Contain    Welcome
  • Suite Setup runs once before the first test of the suite; Suite Teardown runs once after the last.
  • Test Setup and Test Teardown run around every test. A test can override them with [Setup] and [Teardown]; the value NONE switches the inherited one off.
  • A setup or teardown is exactly one keyword. To run several, create a user keyword (see User Keywords) or use the BuiltIn keyword Run Keywords, with AND separating them.

What happens when they fail

Situation Result
Suite Setup fails All tests in the suite fail without running; Suite Teardown still runs
Test Setup fails The test body is not run, the test fails, its teardown still runs
A test step fails The test fails; its teardown still runs
A teardown keyword fails The test fails (even if its steps passed); steps in a teardown that is a user keyword continue to run
Suite Teardown fails All tests in that suite are marked failed

The key promise: a teardown runs whenever its setup was attempted. That is why teardown is the right place for closing browsers, deleting temporary data and stopping servers.

Information available in teardown

Inside a test teardown these built-in variables are set: ${TEST NAME}, ${TEST STATUS} (PASS or FAIL) and ${TEST MESSAGE}. In a suite teardown, ${SUITE STATUS} and ${SUITE MESSAGE} are available. So a teardown can react to failure:

ROBOTFRAMEWORK
*** Keywords ***
Log Out And Capture Evidence
    Run Keyword If Test Failed    Take Screenshot
    Log Out

Run Keyword If Test Failed and Run Keyword If Test Passed are BuiltIn keywords made for teardowns.

Execution order

sequenceDiagram
    participant S as Suite
    participant T1 as Test 1
    participant T2 as Test 2
    S->>S: Suite Setup
    S->>T1: Test Setup
    T1->>T1: steps
    T1->>S: Test Teardown
    S->>T2: Test Setup
    T2->>T2: steps
    T2->>S: Test Teardown
    S->>S: Suite Teardown

Setup and teardown at test level wrap every test; suite level wraps them all once.

Stand-in 1: the lifecycle

The blocks below are not Robot Framework. This stand-in prints the order of calls for a suite of two tests with setups and teardowns, including a failing step and a failing suite setup, so you can check the rules in the table.

Python Stand-in: setup and teardown order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in 2: combining tags

How does a test end up with its final tags? Take the file's Test Tags, add the test's [Tags], remove any tag written with a leading -, and compare case-insensitively with spaces and underscores ignored.

Python Stand-in: final tags of a test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Putting cleanup at the end of the test body instead of in a teardown. If an earlier step fails, the cleanup never runs.
  • Doing heavy work in Test Setup that most tests do not need. Prefer Suite Setup, or give those tests their own [Setup].
  • Treating tags as a replacement for suites. Use directories and files for structure, tags for cross-cutting categories.
  • Writing [Tags] in a test and expecting it to replace Test Tags. It adds to them; use -tag to remove.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A test step fails. Does the test teardown still run?

    Teardowns exist for cleanup, so they run whether the test passed or failed.
  2. 2. How do you remove an inherited tag regression from one test?

    Since Robot 6.0 a tag starting with a minus sign removes the matching tag set by Test Tags.
  3. 3. How do you disable the file-level Test Setup for a single test?

    Giving the test its own [Setup] overrides the inherited one, and NONE means no setup at all.
  4. 4. What does the tag robot:skip do?

    Tests with robot:skip get the SKIP status. robot:exclude, by contrast, removes the test from the run entirely.

Summary

  • Tags label tests; use Test Tags, [Tags] and -tag removal, and select with --include and --exclude.
  • robot: tags are reserved (robot:skip, robot:exclude, robot:continue-on-failure...).
  • Suite Setup/Teardown run once; Test Setup/Teardown wrap each test; [Setup] and [Teardown] override them, NONE disables.
  • Teardowns always run, so put cleanup there; ${TEST STATUS} and ${TEST MESSAGE} are available inside.
  • A failing suite setup fails every test in the suite.

Next: Control Flow: IF, FOR, WHILE and TRY