summaryrefslogtreecommitdiff
path: root/unit_tests/test_plugin_manager.py
blob: b99dbea4d7b1dd5d10c3103ce1b36bc7bba49d4b (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import unittest2 as unittest
from nose import case
from nose.plugins import Plugin, PluginManager


class Plug(Plugin):
    def loadTestsFromFile(self, path):
        class TC(unittest.TestCase):
            def test(self):
                pass
        return [TC('test')]
    def addError(self, test, err):
        return True

class Plug2(Plugin):
    def loadTestsFromFile(self, path):
        class TCT(unittest.TestCase):
            def test_2(self):
                pass
        return [TCT('test_2')]
    def addError(self, test, err):
        assert False, "Should not have been called"

class Plug3(Plugin):
    def loadTestsFromModule(self, module):
        raise TypeError("I don't like to type")

class Plug4(Plugin):
    def loadTestsFromModule(self, module):
        raise AttributeError("I am missing my nose")

class BetterPlug2(Plugin):
    name = 'plug2'


class TestPluginManager(unittest.TestCase):

    def test_proxy_to_plugins(self):
        man = PluginManager(plugins=[Plug(), Plug2()])

        # simple proxy: first plugin to return a value wins
        self.assertEqual(man.addError(None, None), True)

        # multiple proxy: all plugins that return values get to run
        all = []
        for res in man.loadTestsFromFile('foo'):
            print res
            all.append(res)
        self.assertEqual(len(all), 2)

    def test_iter(self):
        expect = [Plug(), Plug2()]
        man = PluginManager(plugins=expect)
        for plug in man:
            self.assertEqual(plug, expect.pop(0))
        assert not expect, \
               "Some plugins were not found by iteration: %s" % expect

    def test_plugin_generative_method_errors_not_hidden(self):
        import nose.failure
        pm = PluginManager(plugins=[Plug3(), Plug4()])
        loaded = list(pm.loadTestsFromModule('whatever'))
        self.assertEqual(len(loaded), 2)
        for test in loaded:
            assert isinstance(test, nose.failure.Failure), \
            "%s is not a failure" % test

    def test_plugin_override(self):
        pm = PluginManager(plugins=[Plug2(), BetterPlug2()])
        self.assertEqual(len(pm.plugins), 1)
        assert isinstance(pm.plugins[0], BetterPlug2)

if __name__ == '__main__':
    unittest.main()