Skip to content

ExtendingTestify

Rhett Garber edited this page Aug 26, 2010 · 2 revisions

Extending Testify

Testify has a plugin system that makes it easy to add in certain types of functionality without patching testify directly.

Included Plugins

Testify comes with a few plugins that demonstrate the plugin system, as well as provide some useful functionality.

JSON Reporting

Adds a test result reporter to log test results to a file in JSON format.

Includes additional options --json-results, --json-results-logging and --extra-json-info

Code Coverage

Uses the python coverage package to generate coverage reports for your test cases

Profiling

Uses cProfile to generate a profiling data dump of each test method. These data files can then be used with normal cProfile tools to analyze the performance of your code.

Running plugins

Testify by default includes in plugins within the testify/plugins/ directory.

Additional plugins can be included by providing them in the TESTIFY_PLUGIN_PATH environment variable during test execution.

Writing your own plugin

A plugin has access to a few different integration points. Testify will look at each module, and if a specially named function is provided, it will call it certain points during test execution.

Command Line Options

Implement the add_command_line_options(parser) function.

For example:

def add_command_line_options(parser):
  parser.add_option("--do-awesome-stuff", dest="do_awesome_stuff", action="store_true", default=False)

Test Case Preparation

After a test case class is instantiated, but before it is run, a plugin can gain access to the object and push whatever buttons it wants. Plugins can do this by implementing the prepare_test_case(options, test_case) function.

For example:

def prepare_test_case(options, test_case):
  if options.do_awesome_stuff
    test_case.awesome = True

Test Case Reporting

A plugin can provide extra reporting hooks via the use of TestReporter objects. (see test_reporter.py)

For example:

def build_test_reporters(options):
  if options.log_awesome_stuff
    return [AwesomeReporter()]
  else:
    return []

Test Case Running

A plugin can wrap the execution of a test method by implementing the run_test_case(options, test_case, runnable) function.

The plugin is expected to at some point during the execution of this function, run the argument runnable.

For example:

def run_test_case(options, test_case, runnable):
  try:
    print "About to run test"
    return runnable()
  finally:
    print "All done with test"