PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/unit_tests/test_plugin_manager.py

https://bitbucket.org/jpellerin/nose/
Python | 74 lines | 57 code | 15 blank | 2 comment | 8 complexity | b90d9057d20e7826d993748c668f2826 MD5 | raw file
 1import unittest
 2from nose import case
 3from nose.plugins import Plugin, PluginManager
 4
 5
 6class Plug(Plugin):
 7    def loadTestsFromFile(self, path):
 8        class TC(unittest.TestCase):
 9            def test(self):
10                pass
11        return [TC('test')]
12    def addError(self, test, err):
13        return True
14
15class Plug2(Plugin):
16    def loadTestsFromFile(self, path):
17        class TCT(unittest.TestCase):
18            def test_2(self):
19                pass
20        return [TCT('test_2')]
21    def addError(self, test, err):
22        assert False, "Should not have been called"
23
24class Plug3(Plugin):
25    def loadTestsFromModule(self, module):
26        raise TypeError("I don't like to type")
27
28class Plug4(Plugin):
29    def loadTestsFromModule(self, module):
30        raise AttributeError("I am missing my nose")
31
32class BetterPlug2(Plugin):
33    name = 'plug2'
34
35
36class TestPluginManager(unittest.TestCase):
37
38    def test_proxy_to_plugins(self):
39        man = PluginManager(plugins=[Plug(), Plug2()])
40
41        # simple proxy: first plugin to return a value wins
42        self.assertEqual(man.addError(None, None), True)
43
44        # multiple proxy: all plugins that return values get to run
45        all = []
46        for res in man.loadTestsFromFile('foo'):
47            print res
48            all.append(res)
49        self.assertEqual(len(all), 2)
50
51    def test_iter(self):
52        expect = [Plug(), Plug2()]
53        man = PluginManager(plugins=expect)
54        for plug in man:
55            self.assertEqual(plug, expect.pop(0))
56        assert not expect, \
57               "Some plugins were not found by iteration: %s" % expect
58
59    def test_plugin_generative_method_errors_not_hidden(self):
60        import nose.failure
61        pm = PluginManager(plugins=[Plug3(), Plug4()])
62        loaded = list(pm.loadTestsFromModule('whatever'))
63        self.assertEqual(len(loaded), 2)
64        for test in loaded:
65            assert isinstance(test, nose.failure.Failure), \
66            "%s is not a failure" % test
67
68    def test_plugin_override(self):
69        pm = PluginManager(plugins=[Plug2(), BetterPlug2()])
70        self.assertEqual(len(pm.plugins), 1)
71        assert isinstance(pm.plugins[0], BetterPlug2)
72
73if __name__ == '__main__':
74    unittest.main()