Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2020 Intel Corporation |
| 3 | # |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
Aastha Grover | cf72fe8 | 2020-06-01 16:08:20 -0700 | [diff] [blame] | 5 | |
Anas Nashif | 9318e6c | 2020-12-07 12:29:36 -0500 | [diff] [blame] | 6 | '''Common fixtures for use in testing the twister tool.''' |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import pytest |
| 11 | |
| 12 | ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") |
Anas Nashif | 9318e6c | 2020-12-07 12:29:36 -0500 | [diff] [blame] | 13 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister")) |
| 14 | from twisterlib import TestSuite, TestInstance |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 15 | |
| 16 | @pytest.fixture(name='test_data') |
| 17 | def _test_data(): |
| 18 | """ Pytest fixture to load the test data directory""" |
Anas Nashif | 9318e6c | 2020-12-07 12:29:36 -0500 | [diff] [blame] | 19 | data = ZEPHYR_BASE + "/scripts/tests/twister/test_data/" |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 20 | return data |
| 21 | |
Aastha Grover | cf72fe8 | 2020-06-01 16:08:20 -0700 | [diff] [blame] | 22 | @pytest.fixture(name='testcases_dir') |
| 23 | def testcases_directory(): |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 24 | """ Pytest fixture to load the test data directory""" |
Anas Nashif | 9318e6c | 2020-12-07 12:29:36 -0500 | [diff] [blame] | 25 | return ZEPHYR_BASE + "/scripts/tests/twister/test_data/testcases" |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 26 | |
Aastha Grover | cf72fe8 | 2020-06-01 16:08:20 -0700 | [diff] [blame] | 27 | @pytest.fixture(name='class_testsuite') |
| 28 | def testsuite_obj(test_data, testcases_dir, tmpdir_factory): |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 29 | """ Pytest fixture to initialize and return the class TestSuite object""" |
| 30 | board_root = test_data +"board_config/1_level/2_level/" |
| 31 | testcase_root = [testcases_dir + '/tests', testcases_dir + '/samples'] |
Aastha Grover | cf72fe8 | 2020-06-01 16:08:20 -0700 | [diff] [blame] | 32 | outdir = tmpdir_factory.mktemp("sanity_out_demo") |
Aastha Grover | 5948ab6 | 2020-05-11 15:38:37 -0700 | [diff] [blame] | 33 | suite = TestSuite(board_root, testcase_root, outdir) |
| 34 | return suite |
Spoorthy Priya Yerabolu | 9ac30b5 | 2020-05-21 04:17:40 -0700 | [diff] [blame] | 35 | |
Aastha Grover | 8213a15 | 2020-06-16 12:15:47 -0700 | [diff] [blame] | 36 | @pytest.fixture(name='all_testcases_dict') |
| 37 | def testcases_dict(class_testsuite): |
| 38 | """ Pytest fixture to call add_testcase function of |
| 39 | Testsuite class and return the dictionary of testcases""" |
Spoorthy Priya Yerabolu | 9ac30b5 | 2020-05-21 04:17:40 -0700 | [diff] [blame] | 40 | class_testsuite.SAMPLE_FILENAME = 'test_sample_app.yaml' |
| 41 | class_testsuite.TESTCASE_FILENAME = 'test_data.yaml' |
| 42 | class_testsuite.add_testcases() |
| 43 | return class_testsuite.testcases |
| 44 | |
Aastha Grover | 8213a15 | 2020-06-16 12:15:47 -0700 | [diff] [blame] | 45 | @pytest.fixture(name='platforms_list') |
| 46 | def all_platforms_list(test_data, class_testsuite): |
| 47 | """ Pytest fixture to call add_configurations function of |
| 48 | Testsuite class and return the Platforms list""" |
Spoorthy Priya Yerabolu | 9ac30b5 | 2020-05-21 04:17:40 -0700 | [diff] [blame] | 49 | class_testsuite.board_roots = os.path.abspath(test_data + "board_config") |
| 50 | suite = TestSuite(class_testsuite.board_roots, class_testsuite.roots, class_testsuite.outdir) |
| 51 | suite.add_configurations() |
| 52 | return suite.platforms |
Aastha Grover | 8213a15 | 2020-06-16 12:15:47 -0700 | [diff] [blame] | 53 | |
| 54 | @pytest.fixture |
| 55 | def instances_fixture(class_testsuite, platforms_list, all_testcases_dict, tmpdir_factory): |
| 56 | """ Pytest fixture to call add_instances function of Testsuite class |
| 57 | and return the instances dictionary""" |
| 58 | class_testsuite.outdir = tmpdir_factory.mktemp("sanity_out_demo") |
| 59 | class_testsuite.platforms = platforms_list |
| 60 | platform = class_testsuite.get_platform("demo_board_2") |
| 61 | instance_list = [] |
| 62 | for _, testcase in all_testcases_dict.items(): |
| 63 | instance = TestInstance(testcase, platform, class_testsuite.outdir) |
| 64 | instance_list.append(instance) |
| 65 | class_testsuite.add_instances(instance_list) |
| 66 | return class_testsuite.instances |