[py-dev] Testing publish-subscribe apps

Michael Hipp Michael at Hipp.com
Sat Feb 17 04:58:26 CET 2007


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)

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 ----



More information about the Pytest-dev mailing list