Writing Python Unit Tests

openWNS uses the standard Python unit test framework.

Quickstart

Each of your test module can have an arbitrary name. However, it must be inside a directory called tests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest
import openwns.simulator

class ModuleMock:
    def __init__(self, plugin):
        self.__plugin__ = plugin

class ModulesTests(unittest.TestCase):

    def setUp(self):
        self.testee = openwns.simulator.Modules()
        self.testee.foo = ModuleMock("moduleA")
        self.testee.bar = ModuleMock("moduleB")

    def testLen(self):
        self.assertEqual(len(self.testee), 2)

    def testAccess(self):
        self.assertEqual(self.testee[0].__plugin__, "moduleA")
        self.assertEqual(self.testee[1].__plugin__, "moduleB")
        self.assertEqual(self.testee[-1].__plugin__, "moduleB")

    def testIter(self):
        foo = []
        for it in self.testee:
            foo.append(it)

        self.assertEqual(foo[0].__plugin__, "moduleA")
        self.assertEqual(foo[1].__plugin__, "moduleB")

    #def testDenyDoubleAttributeAddition(self):
    #    def provokeException():
    #        self.testee.foo = ModuleMock("moduleC")
    #    self.assertRaises(Exception, provokeException)

    def testGetUnknownModule(self):
        def provokeException():
            tmp = self.testee.baz
            self.assertRaises(Exception, provokeException)

        try:
            provokeException()
        except Exception, e:
            self.assertEqual("baz not available.\nAvailable modules are: foo, bar", str(e))

    #def testModuleAlreadyRegistered(self):

    #    def provokeException():
    #        self.testee.baz = ModuleMock("moduleC")
    #        self.testee.zab = ModuleMock("moduleC")
    #    self.assertRaises(Exception, provokeException)