Search code examples
python-3.xwxpythonpytesttest-coverage

Testing wxPython widget without a display


I have this test case:

class MyNiftyPanelTest(unittest.TestCase):
    def setUp(self):
        self.app = wx.App()
        self.frame = wx.Frame(None)
        self.sut = SUT(self.frame, -1)

    […]

    def tearDown(self):
        wx.CallAfter(self.frame.Close)
        self.app.MainLoop()

Where SUT is a class inheriting from wx.Panel which does stuff. This works just fine when I run the tests on a console on my X11 display. No windows is shown but clearly things happen.

However, when I run it via tox (whether tox -r or using xvfb-run tox -v -r), I get the following error:

SystemExit: Unable to access the X Display, is $DISPLAY set properly

when calling self.app = wx.App().

Using

@pytest.mark.skipif(os.getenv('DISPLAY', False) == False,
                    reason="does not run without a DISPLAY")

I can skip those tests when running tox without a DISPLAY but that means that my CI tool (Jenkins) will always report the wrong test coverage.

How can I get this test to work on a headless display?


Solution

  • I think you need to pass $DISPLAY env var in tox.ini down to the pytest:

    [testenv]
    passenv = DISPLAY
    

    Or add to the list if you already have one.