class TestScript (TestCase): __metaclass__ = MetaTestScript """ The scripted.TestScript class subclasses unittest.TestCase and allows you to define unit tests for your RapidSMS apps in the form of a 'conversational' script: from myapp.app import App as MyApp from rapidsms.tests.scripted import TestScript class TestMyApp (TestScript): apps = (MyApp,) testRegister = \""" 8005551212 > register as someuser 8005551212 < Registered new user 'someuser' for 8005551212! \""" testDirectMessage = \""" 8005551212 > tell anotheruser what's up?? 8005550000 < someuser said "what's up??" \""" This TestMyApp class would then work exactly as any other unittest.TestCase subclass (so you could, for example, call unittest.main()). """ apps = None def setUp (self): self.router = MockRouter() self.backend = Backend(self.router) self.router.add_backend(self.backend) if not self.apps: raise Exception( "You must define a list of apps in your TestScript class!") for app_class in self.apps: app = app_class(self.router) self.router.add_app(app) def tearDown (self): if self.router.running: self.router.stop() @classmethod def parseScript (cls, script): cmds = [] for line in map(lambda(x): x.strip(), script.split("\n")): if not line or line.startswith("#"): continue tokens = re.split(r'([<>])', line, 1) num, dir, txt = map(lambda (x):x.strip(), tokens) # allow users to optionally put dates in the number # 19232922@200804150730 if "@" in num: num, datestr = num.split("@") date = datetime.strptime(datestr, "%Y%m%d%H%M") else: date = datetime.now() cmds.append((num, date, dir, txt)) return cmds
def setUp(self): # setup a router with the email backend and echo app enabled. # also mark the inboxes of both accounts read self._read_all(CONF) self._read_all(CONF2) self.assertEqual(0, len(self._get_mail(CONF))) self.assertEqual(0, len(self._get_mail(CONF2))) router = MockRouter() backend = Backend(router) backend.configure(**CONF) router.add_backend(backend) router.add_app(EchoApp(router)) self.backend = backend self.router = router