Hi Michael, On Fri, Feb 16, 2007 at 21:58 -0600, Michael Hipp wrote: > In my previous question I asked about testing applications that use a > publish-subscribe mechanism. I've done a simple app and tester to prove I can > make it work. I'm posting it here in case it might help someone else. I'd > also > like to invite critique as this is my first attempt at using py.test. (I'll > be > doing a *lot* of this when I test my real application.) > > Thanks, > Michael > > --- The app being tested -------------------------------- > # FILE: myapp.py > > from wx.lib.pubsub import Publisher > > myTestTopic = "my_test_topic" # A publisher topic > > def DoSomething(): > # Send a message with a tuple of data > Publisher.sendMessage(myTestTopic, (1,2,3)) > --- The app being tested -------------------------------- > # FILE: test_myapp.py > > from wx.lib.pubsub import Publisher # wxPython publish-subscribe module > from myapp import DoSomething, myTestTopic # myapp.py > > # If someone can tell me how to not need these module-level > # globals I'm all ears > gotTestTopic = False # Indicates we received the test topic message > testTopicData = None # The data we got with the test topic message > > def setup_module(module): > '''This sets certain state specific to the execution of this module.''' > # Subscribe to the test topic message > Publisher.subscribe(TestTopicReceiver, myTestTopic)
a few notes: * i have no clue about wxpython :) * I prefer to have test and code-being-tested separate from each other, so myTestTopic would be better to live in the testfile. * i guess you can avoid module globals by using test classes (see e.g. http://codespeak.net/py/dist/test.html#id17) maybe something like (untested): class TestMyApp: def setup_method(self, method): self.topic = "my_test_topic" # + method.__name__ self.received = [] Publisher.subscribe(self.topic, self.received.append) def teardown_method(self, method): # don't know if wx has it: Publisher.unsubscribe(self.topic, self.received.append) def test_dosomething(self): assert not self.received DoSomething() assert len(self.received) == 1 msg = self.received[0] assert msg.topic == self.topic assert len(msg.data) == 3 # and so one hope it helps, have fun, holger > def test_DoSomething(): > assert not gotTestTopic # Make sure there is no trickery > assert testTopicData is None > DoSomething() # This is "myapp" > assert gotTestTopic # Now we should have it > assert len(testTopicData) == 3 # Verify the data also > > def TestTopicReceiver(msg): > '''Receives the test topic message and sets global vars to be read in > the test routine.''' > global gotTestTopic, testTopicData > if msg.topic[0] == myTestTopic: # Make sure it's really our topic > gotTestTopic = True > testTopicData = msg.data # The data payload in the msg > > --- End ---- > _______________________________________________ > py-dev mailing list > py-dev@codespeak.net > http://codespeak.net/mailman/listinfo/py-dev > -- merlinux GmbH Steinbergstr. 42 31139 Hildesheim http://merlinux.de tel +49 5121 20800 75 (fax 77) _______________________________________________ py-dev mailing list py-dev@codespeak.net http://codespeak.net/mailman/listinfo/py-dev