@@ -15,8 +15,11 @@ class TestWithDefaults:
1515 def model (self , tmp_path ):
1616 config_file = str (data_folder / "config_file_test.txt" )
1717 exe_file = Path (tmp_path ) / "STEMUUS_SCOPE"
18+
1819 # create dummy exe file
19- f = open (exe_file , "x" )
20+ with open (exe_file , "x" , encoding = "utf8" ) as dummy_file :
21+ dummy_file .close ()
22+
2023 yield StemmusScope (config_file , exe_file )
2124
2225 @pytest .fixture
@@ -41,23 +44,23 @@ def test_setup(self, model_with_setup):
4144 # matlab log dir
4245 assert os .environ ['MATLAB_LOG_DIR' ] == str (model .configs ["InputPath" ])
4346
44- @patch ("subprocess.Popen " )
45- def test_run (self , mocked_popen , model_with_setup ):
47+ @patch ("subprocess.run " )
48+ def test_run (self , mocked_run , model_with_setup ):
4649
4750 actual_cfg_file = f"{ data_folder } /directories/input/XX-dummy_2022-07-11-1200_config.txt"
4851 output = (
4952 f"b'Reading config from { actual_cfg_file } \n \n "
5053 "The calculations start now \r \n The calculations end now \r '"
5154 )
52- mocked_popen . return_value . communicate . return_value = ( output , "error" )
53- mocked_popen .return_value .wait . return_value = 0
55+
56+ mocked_run .return_value .stdout = output
5457
5558 model , cfg_file = model_with_setup
5659 result = model .run ()
5760
5861 expected = [f"{ model .exe_file } { cfg_file } " ]
59- mocked_popen .assert_called_with (
60- expected , preexec_fn = os . setsid , stdout = subprocess .PIPE , stderr = subprocess .PIPE , shell = True
62+ mocked_run .assert_called_with (
63+ expected , stdout = subprocess .PIPE , stderr = subprocess .PIPE , shell = True , check = True ,
6164 )
6265
6366 # output of subprocess
@@ -102,22 +105,22 @@ def test_configs(self, model_with_setup):
102105 actual = config_io .read_config (cfg_file )
103106 assert actual == model .configs
104107
105- @patch ("subprocess.Popen " )
106- def test_run (self , mocked_popen , model_with_setup , tmp_path ):
108+ @patch ("subprocess.run " )
109+ def test_run (self , mocked_run , model_with_setup , tmp_path ):
107110 actual_cfg_file = f"{ tmp_path } /input/dummy_2022-07-11-1200_config.txt"
108111 output = (
109112 f"b'Reading config from { actual_cfg_file } \n \n "
110113 "The calculations start now \r \n The calculations end now \r '"
111114 )
112- mocked_popen . return_value . communicate . return_value = ( output , "error" )
113- mocked_popen .return_value .wait . return_value = 0
115+
116+ mocked_run .return_value .stdout = output
114117
115118 model , cfg_file = model_with_setup
116119 result = model .run ()
117120
118121 expected = [f"{ model .exe_file } { cfg_file } " ]
119- mocked_popen .assert_called_with (
120- expected , preexec_fn = os . setsid , stdout = subprocess .PIPE , stderr = subprocess .PIPE , shell = True
122+ mocked_run .assert_called_with (
123+ expected , stdout = subprocess .PIPE , stderr = subprocess .PIPE , shell = True , check = True ,
121124 )
122125
123126 # output of subprocess
0 commit comments