/unit_tests/test_plugin_manager.py

https://bitbucket.org/jpellerin/nose/ · Python · 74 lines · 57 code · 15 blank · 2 comment · 4 complexity · b90d9057d20e7826d993748c668f2826 MD5 · raw file

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