deactivate a test method in Cmd mode

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

deactivate a test method in Cmd mode

Post by bernd »

I have some unit test method setup which does some ViewProvider test. This method should only run in Gui mode. At the moment I do:

Code: Select all

def test_some_viewprovider(self):
    if FreeCAD.GuiUp:
        100 lines of code
is there some some smarter way without indent?
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: deactivate a test method in Cmd mode

Post by bernd »

Ha found it myself, sorry for the noise ...

Code: Select all

def test_some_viewprovider(self):
    if not FreeCAD.GuiUp:
        return
        
    100 lines of code
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: deactivate a test method in Cmd mode

Post by vocx »

bernd wrote: Thu May 21, 2020 10:10 am

Code: Select all

    if not FreeCAD.GuiUp:
        return
Personally, I would add a message saying that the test could not be performed because the interface does not exist, and thus it will automatically pass. Then the reader will at least know that the test was attempted.

A better way to handle this would be to move this test to a separate collection of tests, which all require or expect the graphical interface.

All workbenches have an Init.py and InitGui.py. So, the unit tests that don't require the graphical interface can be added to Init.py, and those that require it, should be added to InitGui.py. Then they will only run when the graphical interface exist.

Code: Select all

freecadcmd -t 0  # Runs only the unit tests in Init.py
freecad -t 0     # Runs all unit tests in Init.py and InitGui.py
This is what we did in Draft. In the past, all unit tests were defined in TestDraft.py which was called from InitGui.py. But essentially none of them required the GUI, so we created two sets, TestDraft.py and TestDraftGui.py. The first, with the majority of tests, is initialized from Init.py, and only a few in InitGui.py.
Last edited by vocx on Fri May 22, 2020 6:23 pm, edited 1 time in total.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: deactivate a test method in Cmd mode

Post by wmayer »

bernd wrote: Thu May 21, 2020 10:10 am Ha found it myself, sorry for the noise ...
This is one way to do it. Another is to move tests concerning the GUI to a separate file (e.g. FemTestsGui.py) and then add a line like this to your InitGui.py

Code: Select all

FreeCAD.__unit_test__ += [ "FemTestsGui" ]
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: deactivate a test method in Cmd mode

Post by bernd »

thanks guys for the hints.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: deactivate a test method in Cmd mode

Post by bernd »

wmayer wrote: Fri May 22, 2020 6:42 am ... move tests concerning the GUI to a separate file (e.g. FemTestsGui.py) and then add a line like this to your InitGui.py

Code: Select all

FreeCAD.__unit_test__ += [ "FemTestsGui" ]
git commit 803da57e11
Post Reply