1

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 Automatically Unit Test Example Code, but I assume python best practices will differ from C#

1
  • Is it ideal to have file 'projectname' under 'projectname?' Commented Oct 24, 2012 at 19:09

2 Answers 2

3

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."

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.