Tuesday, July 29, 2014

Nose and test generators

In my code I have a directory of small python modules that act as plugins. They can be called by a main program as directed. Each plugin, therefore, has a standard interface and needs to work with the main program. I wanted to setup an automated test that would run the main program, load each plugin in turn and run to make sure there was no crash. It turns out that nose has a very elegant way of doing this.

My test module for this turns out to be (with a little paraphrasing and removal of details):

from nose.plugins.skip import SkipTest


def find_plugin_models():
  return a_list_of_the_modules_loaded

def check_plugin(model):
  if an_important_precondition_is_not_met:
    #http://stackoverflow.com/questions/1120148/disabling-python-nosetests
    raise SkipTest('I can not run this test')
  run_a_test()
  assert thing_is_ok()


#http://stackoverflow.com/questions/19071601/how-do-i-run-multiple-python-test-cases-in-a-loop
def test_all_found_plugins():
  """Integration test on automatically found mutation plugin"""
  for model in find_plugin_models():
    check_plugin.description = 'Testing {:s} automatically'.format(model.__name__)
    yield check_plugin, model

Note the cool ability to set a .description attribute that allows you to print what you like when the test runs.

No comments:

Post a Comment