Skip to content

4.2. List test cases

Goal

  • Write a new Python script which lists the test cases in a given suite.
    • Create a new folder named tools in the tests folder and save your script into that folder as test_case_list.py.
    • The script should accept the path to the suite as a command line argument (the path can point to a folder or to a .robot file).
    • The script should iterate through every suite in the given path.

Solution

Hints

It is recommended to use argparse module from the Python standard library to process command line arguments.

Click here to learn more about argparse module.

You can use TestSuiteBuilder class from robot.running package to parse the test data in the given path.

Click here to learn more about how to use the TestSuiteBuilder class.

Solution: tests/tools/test_case_list.py
 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import argparse
from robot.api import TestSuiteBuilder


def get_command_line_arguments():
    parser = argparse.ArgumentParser(
        prog='Test Case List',
        description='Lists the test cases in a specific test suite.'
    )
    parser.add_argument(
        '-v', '--version',
        action='version',
        version=f'{parser.prog} 1.0.0'
    )
    parser.add_argument(
        '-s', '--suite',
        type=str,
        help='Path to the root folder (or file) of the test suite.'
    )
    return parser.parse_args()


def list_test_cases_from_suite(parent_suite, full_path_to_suite):
    for test_case in parent_suite.tests:
        print(f'Test Case: {full_path_to_suite}.{test_case.name}')
        print(f'Tags: {test_case.tags}')
        print(f'Documentation: {test_case.doc}\n-')


def visit_test_suites(parent_suite, full_path_to_suite):
    list_test_cases_from_suite(parent_suite, full_path_to_suite)

    for suite in parent_suite.suites:
        new_path = f'{full_path_to_suite}.{suite.name}'
        if suite.tests:
            list_test_cases_from_suite(suite, new_path)
        if suite.suites:
            visit_test_suites(suite, new_path)


def main():
    root_suite = TestSuiteBuilder().build(CLI_ARGS.suite)
    print(f'Root Suite: {root_suite.name}\n-')
    visit_test_suites(root_suite, root_suite.name)


if __name__ == '__main__':
    CLI_ARGS = get_command_line_arguments()
    main()

Results

Inside the tests folder, execute the following command.

1
python ./tools/test_case_list.py --suite .

The output should be something like this:

Root Suite: Tests
-
Test Case: Tests.01-Greetings.01-Greetings.Original Greetings
Tags: [ubuntu]
Documentation: This test case checks that the Print Your Name keyword works as expected.
-
Test Case: Tests.01-Greetings.01-Greetings.Greetings Again
Tags: [centos]
Documentation: This test case proves that we can import variables from resource files.
-
...

You can also list the test cases of a single .robot file.

1
python ./tools/test_case_list.py --suite ./02-classroom/01-lists.robot
The output should be something like this:

Root Suite: 01-Lists
-
Test Case: 01-Lists.Greetings Everyone
Tags: [loop]
Documentation: This test case verifies the functionality of 'Print Multiple Names' keyword.
-
Test Case: 01-Lists.Students
Tags: []
Documentation: This test case logs the names from students.txt to console.
-

Last update: August 30, 2020