11import sys
22from random import uniform
3- from os .path import join , isfile
3+ from os import makedirs
4+ from os .path import join , isfile , isdir
45import mock
56
67import pytest
1011
1112from pyleecan .GUI .Dialog .DMachineSetup .DMachineSetup import DMachineSetup
1213from pyleecan .GUI .Dialog .DMachineSetup .SPreview .SPreview import SPreview
13- from Tests import TEST_DATA_DIR as data_test
14+ from Tests import TEST_DATA_DIR as data_test , save_gui_path
1415from pyleecan .definitions import MAIN_DIR
1516from pyleecan .Functions .load import load_matlib
1617
4849
4950
5051class TestSPreview (object ):
51- @pytest .fixture
52- def setup (self ):
53- """Run at the begining of every test to setup the gui"""
5452
53+ @classmethod
54+ def setup_class (cls ):
55+ """Start the app for the test"""
56+ print ("\n Start Test TestSPreview" )
5557 if not QtWidgets .QApplication .instance ():
56- self .app = QtWidgets .QApplication (sys .argv )
58+ cls .app = QtWidgets .QApplication (sys .argv )
5759 else :
58- self .app = QtWidgets .QApplication .instance ()
60+ cls .app = QtWidgets .QApplication .instance ()
5961
62+ def setup_method (self ):
63+ """Run at the begining of every test to setup the gui"""
6064 # MatLib widget
6165 material_dict = load_matlib (matlib_path = matlib_path )
62- widget = DMachineSetup (material_dict = material_dict , machine_path = machine_path )
66+ self . widget = DMachineSetup (material_dict = material_dict , machine_path = machine_path )
6367
64- yield {"widget" : widget }
65-
66- self .app .quit ()
68+ @classmethod
69+ def teardown_class (cls ):
70+ """Exit the app after the test"""
71+ cls .app .quit ()
6772
6873 @pytest .mark .parametrize ("test_dict" , load_preview_test )
69- def test_load (self , setup , test_dict ):
74+ def test_load (self , test_dict ):
7075 """Check that you can load a machine"""
7176 assert isfile (test_dict ["file_path" ])
7277
@@ -75,20 +80,46 @@ def test_load(self, setup, test_dict):
7580 "PySide2.QtWidgets.QFileDialog.getOpenFileName" , return_value = return_value
7681 ):
7782 # To trigger the slot
78- setup [ " widget" ] .b_load .clicked .emit ()
83+ self . widget .b_load .clicked .emit ()
7984
8085 # Check load MachineType
81- assert type (setup [ " widget" ] .w_step ) is SPreview
86+ assert type (self . widget .w_step ) is SPreview
8287 # Check the table
8388 assert (
84- setup [ " widget" ] .w_step .tab_machine .tab_param .rowCount () == test_dict ["Nrow" ]
89+ self . widget .w_step .tab_machine .tab_param .rowCount () == test_dict ["Nrow" ]
8590 )
8691 for ii , content in enumerate (test_dict ["table" ]):
8792 assert (
88- setup [ " widget" ] .w_step .tab_machine .tab_param .item (ii , 0 ).text ()
93+ self . widget .w_step .tab_machine .tab_param .item (ii , 0 ).text ()
8994 == content [0 ]
9095 )
9196 assert (
92- setup [ " widget" ] .w_step .tab_machine .tab_param .item (ii , 1 ).text ()
97+ self . widget .w_step .tab_machine .tab_param .item (ii , 1 ).text ()
9398 == content [1 ]
9499 )
100+ # Check Draw FEMM
101+ FEMM_dir = join (save_gui_path , "Draw_FEMM" )
102+ if not isdir (FEMM_dir ):
103+ makedirs (FEMM_dir )
104+ femm_path = join (FEMM_dir , self .widget .machine .name + ".fem" )
105+ assert not isfile (femm_path )
106+
107+ return_value = (
108+ femm_path ,
109+ "FEMM (*.fem)" ,
110+ )
111+ with mock .patch (
112+ "PySide2.QtWidgets.QFileDialog.getSaveFileName" , return_value = return_value
113+ ):
114+ self .widget .w_step .tab_machine .b_FEMM .clicked .emit ()
115+ assert isfile (femm_path )
116+
117+ if __name__ == "__main__" :
118+ a = TestSPreview ()
119+ a .setup_class ()
120+ a .setup_method ()
121+ for ii , test_dict in enumerate (load_preview_test ):
122+ print (ii )
123+ a .test_load (test_dict )
124+ # a.test_load(load_preview_test[0])
125+ print ("Done" )
0 commit comments