11import pytest
22import json
33import os
4- from hamcrest import assert_that , only_contains , any_of , ends_with
4+ from hamcrest import assert_that , contains_inanyorder , ends_with
55
66
7- @pytest .mark .parametrize ("strategy" , ["env_string" , "env_path" ])
87@pytest .mark .parametrize (
9- ["ids " , "expected_tests" ],
8+ ["plan_json " , "expected_tests" ],
109 [
10+ # by ids only
1111 (
1212 [{"id" : 1 }, {"id" : 2 }],
1313 ["test_number_one" , "test_number_two" ]
1414 ),
15+
16+ # by ids for multiply decorated test
1517 (
1618 [{"id" : 1 }, {"id" : 3 }, {"id" : 4 }],
1719 ["test_number_one" , "test_number_three" ]
1820 ),
21+
22+ # by wrong id
23+ (
24+ [{"id" : 1234 }],
25+ []
26+ ),
27+
28+ # by selectors only
29+ (
30+ [{"selector" : "test_number_one" }, {"selector" : "test_number_three" }],
31+ ["test_number_one" , "test_number_three" ]
32+ ),
33+
34+ # by selector for not decorated with id test
35+ (
36+ [{"selector" : "test_without_number" }],
37+ ["test_without_number" ]
38+ ),
39+
40+ # by id and selector for same test
41+ (
42+ [{"id" : 2 , "selector" : "test_number_two" }],
43+ ["test_number_two" ]
44+ ),
45+
46+ # by wrong selector
47+ (
48+ [{"selector" : "test_without_never" }],
49+ []
50+ ),
51+
52+ # without plan
1953 (
2054 None ,
2155 ["test_number_one" , "test_number_two" , "test_number_three" , "test_without_number" ]
2256 ),
2357 ]
2458)
25- def test_select_by_testcase_id_test (strategy , ids , expected_tests , allured_testdir ):
59+ def test_select_by_testcase_id_test (plan_json , expected_tests , allured_testdir , request ):
2660 """
2761 >>> import allure
2862
@@ -42,21 +76,24 @@ def test_select_by_testcase_id_test(strategy, ids, expected_tests, allured_testd
4276 >>> def test_without_number():
4377 ... pass
4478 """
45- allured_testdir .parse_docstring_source ()
4679
47- if strategy == "env_path" and ids :
48- py_path = allured_testdir .testdir .makefile (".json" , json .dumps (ids ))
80+ root_dir = request .config .rootdir .strpath
81+ test_dir = allured_testdir .testdir .tmpdir .strpath .replace (root_dir , "" )
82+ full_name_base_template = "{base}.test_select_by_testcase_id_test" .format (
83+ base = test_dir .strip (os .sep ).replace (os .sep , "." ))
84+
85+ if plan_json :
86+ for item in plan_json :
87+ if "selector" in item :
88+ item ["selector" ] = "{base}#{name}" .format (base = full_name_base_template , name = item ["selector" ])
89+ py_path = allured_testdir .testdir .makefile (".json" , json .dumps (plan_json ))
4990 os .environ ["AS_TESTPLAN_PATH" ] = py_path .strpath
50- elif strategy == "env_string" and ids :
51- os .environ ["AS_TESTPLAN_IDS" ] = "," .join ([str (item ["id" ]) for item in ids ])
5291 else :
5392 os .environ .pop ("AS_TESTPLAN_PATH" , None )
54- os .environ .pop ("AS_TESTPLAN_IDS" , None )
5593
94+ allured_testdir .parse_docstring_source ()
5695 allured_testdir .run_with_allure ()
57- test_cases = [test_case ["fullName" ] for test_case in allured_testdir .allure_report .test_cases ]
58- assert_that (test_cases , only_contains (
59- any_of (
60- * [ends_with (name ) for name in expected_tests ]
61- )
62- ))
96+
97+ executed_full_names = [test_case ["fullName" ] for test_case in allured_testdir .allure_report .test_cases ]
98+
99+ assert_that (executed_full_names , contains_inanyorder (* [ends_with (name ) for name in expected_tests ]))
0 commit comments