views:

41

answers:

2

I'm in the process of writing a python module that includes some samples. These samples aren't unit-tests, and they are too long and complex to be doctests. I'm interested in best practices for automatically checking that these samples run.

My current project layout is pretty standard, except that there is an extra top level makefile that has build, install, unittest, coverage and profile targets, that delegate responsibility to setup.py and nose as required.

projectname/
    Makefile
    README
    setup.py
    samples/
        foo-sample
        foobar-sample
    projectname/
        __init__.py
        foo.py
        bar.py
    tests/
        test-foo.py
        test-bar.py

I've considered adding a sampletest module, or adding nose.tools.istest decorators to the entry-point functions of the samples, but for a small number of samples, these solutions sound a bit ugly.

This question is similar to http://stackoverflow.com/questions/301365/automatically-unit-test-example-code, but I assume python best practices will differ from C#

+3  A: 

Can't you just do:

if __name__ == "__main__":
  run_tests()

in the module code? This way, it will only run if the module is called as a stand-alone progran, not when it's imported into other code.

unwind
The problem is that some of the modules have multiple samples, and that code in those samples isn't related to the code in the module
Andrew Walker
+3  A: 

Most of the time, I just use unittest for testing my examples and functional tests. It isn't unit testing, but a lot of what I do with the unittest module isn't. To quote Knuth, "A computer doesn’t mind if its programs are put to purposes that don’t match their names."

Mike Graham