6
6
Blackbox tests for twister's command line functions - those requiring testplan.json
7
7
"""
8
8
9
- import importlib
10
9
from unittest import mock
11
10
import os
12
11
import pytest
13
- import sys
14
12
import json
15
13
16
14
# pylint: disable=no-name-in-module
17
- from conftest import ZEPHYR_BASE , TEST_DATA , suite_filename_mock
15
+ from conftest import TEST_DATA , suite_filename_mock
18
16
from twisterlib .testplan import TestPlan
19
17
from twisterlib .error import TwisterRuntimeError
18
+ from twisterlib .twister_main import main as twister_main
20
19
21
20
22
21
class TestTestPlan :
23
22
TESTDATA_1 = [
24
- ('dummy.agnostic.group2.a2_tests.assert1' , SystemExit , 4 ),
23
+ ('dummy.agnostic.group2.a2_tests.assert1' , None , 4 ),
25
24
(
26
25
os .path .join ('scripts' , 'tests' , 'twister_blackbox' , 'test_data' , 'tests' ,
27
26
'dummy' , 'agnostic' , 'group1' , 'subgroup1' ,
@@ -39,17 +38,6 @@ class TestTestPlan:
39
38
(False , 7 ),
40
39
]
41
40
42
- @classmethod
43
- def setup_class (cls ):
44
- apath = os .path .join (ZEPHYR_BASE , 'scripts' , 'twister' )
45
- cls .loader = importlib .machinery .SourceFileLoader ('__main__' , apath )
46
- cls .spec = importlib .util .spec_from_loader (cls .loader .name , cls .loader )
47
- cls .twister_module = importlib .util .module_from_spec (cls .spec )
48
-
49
- @classmethod
50
- def teardown_class (cls ):
51
- pass
52
-
53
41
@pytest .mark .parametrize (
54
42
'test, expected_exception, expected_subtest_count' ,
55
43
TESTDATA_1 ,
@@ -65,24 +53,21 @@ def test_subtest(self, out_path, test, expected_exception, expected_subtest_coun
65
53
['-p' ] * len (test_platforms ), test_platforms
66
54
) for val in pair ]
67
55
68
- with mock .patch .object (sys , 'argv' , [sys .argv [0 ]] + args ), \
69
- pytest .raises (expected_exception ) as exc :
70
- self .loader .exec_module (self .twister_module )
71
-
72
- if expected_exception != SystemExit :
73
- assert True
74
- return
75
-
76
- with open (os .path .join (out_path , 'testplan.json' )) as f :
77
- j = json .load (f )
78
- filtered_j = [
79
- (ts ['platform' ], ts ['name' ], tc ['identifier' ]) \
80
- for ts in j ['testsuites' ] \
81
- for tc in ts ['testcases' ] if 'reason' not in tc
82
- ]
83
-
84
- assert str (exc .value ) == '0'
85
- assert len (filtered_j ) == expected_subtest_count
56
+ if expected_exception :
57
+ with pytest .raises (expected_exception ):
58
+ twister_main (args )
59
+ else :
60
+ return_value = twister_main (args )
61
+ with open (os .path .join (out_path , 'testplan.json' )) as f :
62
+ j = json .load (f )
63
+ filtered_j = [
64
+ (ts ['platform' ], ts ['name' ], tc ['identifier' ]) \
65
+ for ts in j ['testsuites' ] \
66
+ for tc in ts ['testcases' ] if 'reason' not in tc
67
+ ]
68
+
69
+ assert return_value == 0
70
+ assert len (filtered_j ) == expected_subtest_count
86
71
87
72
@pytest .mark .parametrize (
88
73
'filter, expected_count' ,
@@ -98,11 +83,8 @@ def test_filter(self, out_path, filter, expected_count):
98
83
['-p' ] * len (test_platforms ), test_platforms
99
84
) for val in pair ]
100
85
101
- with mock .patch .object (sys , 'argv' , [sys .argv [0 ]] + args ), \
102
- pytest .raises (SystemExit ) as exc :
103
- self .loader .exec_module (self .twister_module )
86
+ assert twister_main (args ) == 0
104
87
105
- assert str (exc .value ) == '0'
106
88
import pprint
107
89
with open (os .path .join (out_path , 'testplan.json' )) as f :
108
90
j = json .load (f )
@@ -132,11 +114,7 @@ def test_integration(self, out_path, integration, expected_count):
132
114
['-p' ] * len (test_platforms ), test_platforms
133
115
) for val in pair ]
134
116
135
- with mock .patch .object (sys , 'argv' , [sys .argv [0 ]] + args ), \
136
- pytest .raises (SystemExit ) as exc :
137
- self .loader .exec_module (self .twister_module )
138
-
139
- assert str (exc .value ) == '0'
117
+ assert twister_main (args ) == 0
140
118
141
119
with open (os .path .join (out_path , 'testplan.json' )) as f :
142
120
j = json .load (f )
0 commit comments