using wx in unittests

I’ve made a set of unit tests for some image manipulations, each manipulation takes an input image and should have a single desired output image.

My plan is to have the desired output image saved in a results folder so that when the tests run and generate an output image, I can compare the generated image data with the corresponding image’s data in the results folder.

This is where wx comes in.

In the unit test if there’s a corresponding image in the results folder it will compare it and move on. Initially though my results folder will be empty so if the corresponding image isn’t there I’m popping a single, simple wx frame with the input and output images side by side with some labels explaining what the manipulation was.

The user can then press an OK or FAIL button depending if the output is correct. If they click ok it saves it to the results folder and the user will never need to manually check the output of that test again.

All of this is working fine, however I get an exception when trying to initialise the second frame in the tests (the program will load one frame just fine)

This is a simple version showing the error:

import unittest

import wx

class TestCase(unittest.TestCase):

def test_something(self):

“”“This test tests a series of things and needs the user to validate the result visually”""

for i in xrange(3):

app= wx.App(False)

frame= wx.Frame(None)

frame.Show()

app.MainLoop()

if name == “main”:

unittest.main()

which throws “PyNoAppError: The wx.App object must be created first!” after closing the first frame.

I thought initially that maybe you can’t create an app twice in the same program but from the looks of this, you definitely can.

Why does my version not work? Is there some magic about putting wx.App in the setUp method that makes it work?

Is it possible to make the version above work?

I could refactor the code so that only one frame is shown per test method so I can put wx.App in the setUp method but it would be difficult and a lot of work as I’ve already written everything, only this part isn’t working.

The reason it would be difficult is because the loop in the test method is dynamic, and I’d need to do some rather complicated meta coding to loop over these dynamic loops making test methods for the test class at run time.

Thanks and hope I’ve provided enough info!

import unittest
import wx

class TestCase(unittest.TestCase):

 def test\_something\(self\):
        """This test tests a series of things and needs the user to validate the result visually"""
        for i in xrange\(3\):
               app= wx\.App\(False\)
               frame= wx\.Frame\(None\)
               frame\.Show\(\)

if __name__ == "__main__":
unittest.main()

You only ever should have one application - i.e. one program in a single program, but the application can have more than one frame so you need:
import unittest
import wx

class TestCase(unittest.TestCase):

    def test_something(self):
        """This test tests a series of things and needs the user to validate the result visually"""
for i in xrange(3):
            frame= wx.Frame(None)
            frame.Show()
            app.MainLoop()

if __name__ == "__main__":
app= wx.App(False) unittest.main() app.MainLoop()

Gadget/Steve

import unittest

import wx

class TestCase(unittest.TestCase):

def test_something(self):
       """This test tests a series of things and needs the user to validate the result visually"""
       for i in xrange(3):
              app= wx.App(False)
              frame= wx.Frame(None)
              frame.Show()

if name == “main”:

unittest.main()

You only ever should have one application - i.e. one program in a single program, but the application can have more than one frame so you need:
import unittest

import wx

class TestCase(unittest.TestCase):

def test_something(self):

    """This test tests a series of things and needs the user to validate the result visually"""

    for i in xrange(3):

frame= wx.Frame(None)

        frame.Show()

        app.MainLoop()

if name == “main”:

app= wx.App(False) unittest.main() app.MainLoop()

Gadget/Steve

Ah that works perfectly! thanks!

Out of interest do you know why does making multiple Apps in the setUp method work? but not in the same method like I tried?

It would be nice to be able to understand why it doesn’t work.

For instance this works fine:

import unittest

import wx

class TestCase(unittest.TestCase):

def setUp(self):

self.app= wx.App(False)

def test_something(self):

frame= wx.Frame(None)

frame.Show()

self.app.MainLoop()

def test_something_else(self):

frame= wx.Frame(None)

frame.Show()

self.app.MainLoop()

if name == “main”:

unittest.main()

···

On 19 November 2012 06:09, Steve Barnes gadgetsteve@live.co.uk wrote:

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Paul Wiseman wrote:

Ah that works perfectly! thanks!

Out of interest do you know why does making multiple Apps in the setUp
method work? but not in the same method like I tried?

The example link you posted (the unit testing page on the wxPython wiki)
creates exactly one wx.App in the setUp method, and the setUp method is
called exactly once by the unittest manager.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Paul Wiseman wrote:

Ah that works perfectly! thanks!

Out of interest do you know why does making multiple Apps in the setUp

method work? but not in the same method like I tried?

The example link you posted (the unit testing page on the wxPython wiki)

creates exactly one wx.App in the setUp method, and the setUp method is

called exactly once by the unittest manager.

Ah, your right I didn’t noticed that the first time around- it does work if you add more example tests though, which makes the test runner call setUp more than once

···

On 19 November 2012 17:51, Tim Roberts timr@probo.com wrote:

Tim Roberts, timr@probo.com

Providenza & Boekelheide, Inc.

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en