testingautomated-testsrobotframeworkteardown

How to set multi-level test setup/teardown in robot framework


I have some robot test cases separated in directories. The directory hierarchy is:

ParentTestDirectory
    |__ ChidTestDirectoryOne
        |__ TestOne.robot
    |__ ChidTestDirectoryTwo
        |__ TestTwo.robot
    |__ __init__.robot

Content of __init__.robot:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from __init__.robot
Test Teardown       LOG TO CONSOLE   Test teardown from __init__.robot

Content of TestOne.robot:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from TestOne.robot
Test Teardown       LOG TO CONSOLE   Test teardown from TestOne.robot
*** Test Cases ***
Test One
    LOG TO CONSOLE   This is Test One!

Content of TestTwo.robot:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from TestTwo.robot
Test Teardown       LOG TO CONSOLE   Test teardown from TestTwo.robot
*** Test Cases ***
Test Two
    LOG TO CONSOLE   This is Test Two!

I have a runner written in python which uses robot runner module; this is the result of running test cases with command sudo python run.py --testsuit scenarios.ParentTestDirectory:

==============================================================================
Scenarios                                                                     
==============================================================================
Scenarios.ParentTestDirectory                                                 
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne                            
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne.TestOne                    
==============================================================================
Test One                                                              Test setup from TestOne.robot
.This is Test One!
.Test teardown from TestOne.robot
Test One                                                              | PASS |
------------------------------------------------------------------------------
Scenarios.ParentTestDirectory.ChidTestDirectoryOne.TestOne            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne                    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo                            
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo.TestTwo                    
==============================================================================
Test Two                                                              Test setup from TestTwo.robot
.This is Test Two!
.Test teardown from TestTwo.robot
Test Two                                                              | PASS |
------------------------------------------------------------------------------
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo.TestTwo            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo                    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory                                         | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Scenarios                                                             | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================

As you see, it just runs the latest test setup/teardown. I want it to run test setups/teardowns from parent directories too and it should be executed prior to child's. In other words I want the parent setup to be run for each test case separately before it's own setup. Can I achieve this with robot framework capabilities?


Solution

  • A test case can only have a single setup. When you put Test Setup in the settings for a suite as a whole, that defines a default test setup. If a child suite or an individual test defines a test setup, it will be run instead of the suite level setup defined higher up.

    In the section titled Initialization files in the robot framework user guide, it says this (with emphasis added by me):

    Test Setup, Test Teardown, Test Timeout

    Set the default value for test setup/teardown or test timeout to all test cases this directory contains. Can be overridden on lower level. Support for defining test timeout in initialization files was added in Robot Framework 2.7.

    If you want the suite to define a test setup that is run in addition to the setup specified by each test, you should put that code into a custom keyword and let each test call that keyword as part of its setup.