66Blackbox tests for twister's command line functions - simple does-error-out or not tests
77"""
88
9- import importlib
10- from unittest import mock
119import os
1210import pytest
13- import sys
1411import re
12+ import sys
13+ from unittest import mock
1514
1615# pylint: disable=no-name-in-module
17- from conftest import ZEPHYR_BASE , TEST_DATA , suite_filename_mock
16+ from conftest import TEST_DATA , suite_filename_mock
1817from twisterlib .testplan import TestPlan
19- from twisterlib .error import TwisterRuntimeError
18+ from twisterlib .twister_main import main as twister_main
2019
2120
2221class TestError :
@@ -26,17 +25,17 @@ class TestError:
2625 os .path .join ('scripts' , 'tests' , 'twister_blackbox' , 'test_data' , 'tests' ,
2726 'dummy' , 'agnostic' , 'group1' , 'subgroup1' ,
2827 'dummy.agnostic.group1.subgroup1' ),
29- SystemExit
28+ ( 0 , '' )
3029 ),
3130 (
3231 None ,
3332 'dummy.agnostic.group1.subgroup1' ,
34- TwisterRuntimeError
33+ ( 1 , 'No testsuites found at the specified location...' )
3534 ),
3635 (
3736 os .path .join (TEST_DATA , 'tests' , 'dummy' ),
3837 'dummy.agnostic.group1.subgroup1' ,
39- SystemExit
38+ ( 0 , '' )
4039 )
4140 ]
4241 TESTDATA_2 = [
@@ -50,24 +49,13 @@ class TestError:
5049 )
5150 ]
5251
53- @classmethod
54- def setup_class (cls ):
55- apath = os .path .join (ZEPHYR_BASE , 'scripts' , 'twister' )
56- cls .loader = importlib .machinery .SourceFileLoader ('__main__' , apath )
57- cls .spec = importlib .util .spec_from_loader (cls .loader .name , cls .loader )
58- cls .twister_module = importlib .util .module_from_spec (cls .spec )
59-
60- @classmethod
61- def teardown_class (cls ):
62- pass
63-
6452 @pytest .mark .parametrize (
65- 'testroot, test, expected_exception ' ,
53+ 'testroot, test, expected_return ' ,
6654 TESTDATA_1 ,
6755 ids = ['valid' , 'invalid' , 'valid' ]
6856 )
6957 @mock .patch .object (TestPlan , 'TESTSUITE_FILENAME' , suite_filename_mock )
70- def test_test (self , out_path , testroot , test , expected_exception ):
58+ def test_test (self , out_path , testroot , test , expected_return , capsys ):
7159 test_platforms = ['qemu_x86' , 'intel_adl_crb' ]
7260 args = []
7361 if testroot :
@@ -77,13 +65,14 @@ def test_test(self, out_path, testroot, test, expected_exception):
7765 ['-p' ] * len (test_platforms ), test_platforms
7866 ) for val in pair ]
7967
80- with mock .patch .object (sys , 'argv' , [sys .argv [0 ]] + args ), \
81- pytest .raises (expected_exception ) as exc :
82- self .loader .exec_module (self .twister_module )
68+ expected_return_code , expected_message = expected_return
69+
70+ return_code = twister_main (args )
71+ captured = capsys .readouterr ()
8372
84- if expected_exception == SystemExit :
85- assert str ( exc . value ) == '0'
86- assert True
73+ assert return_code == expected_return_code
74+ if expected_message :
75+ assert expected_message in captured . err
8776
8877 @pytest .mark .parametrize (
8978 'switch, expected' ,
@@ -106,18 +95,16 @@ def test_overflow_as_errors(self, capfd, out_path, switch, expected):
10695 if switch :
10796 args += [switch ]
10897
109- with mock .patch .object (sys , 'argv' , [sys .argv [0 ]] + args ), \
110- pytest .raises (SystemExit ) as sys_exit :
111- self .loader .exec_module (self .twister_module )
98+ return_code = twister_main (args )
11299
113100 out , err = capfd .readouterr ()
114101 sys .stdout .write (out )
115102 sys .stderr .write (err )
116103
117104 print (args )
118105 if switch :
119- assert str ( sys_exit . value ) == '1'
106+ assert return_code == 1
120107 else :
121- assert str ( sys_exit . value ) == '0'
108+ assert return_code == 0
122109
123110 assert re .search (expected , err )
0 commit comments