Struggling with unittest discovery - how to structure my project test suite


P

Paul Moore

I'm trying to write a project using test-first development. I've been basically following the process from "Test-Driven Web Development with Python" (excellent book, by the way) but I'm writing a command line application rather than a web app, so I'm having to modify some bits as I go along. NotablyI am *not* using the django test runner.

I have my functional tests in a file in my project root at the moment - functional_tests.py. But now I need to start writing some unit tests and I need to refactor my tests into a more manageable structure.

If I create a "tests" directory with an __init__.py and "unit" and "functional" subdirectories, each with __init__.py and test_XXX.py files in them, then "python -m unittest" works (as in, it discovers my tests fine). But if I just want to run my unit tests, or just my functional tests, I can't seemto get the command line right to do that - I either get all the tests run,or none.

What's the best way of structuring my projects so that:

1. I can run all the tests easily on demand.
2. I can run just the functional or unit tests when needed.
3. I can run individual tests (or maybe just individual test modules, I don't have so many tests yet that I know how detailed I'll need to get!) without too much messing (and certainly without changing any source files!)

I know that tools like py.test or nose can probably do this sort of thing. But I don't really want to add a new testing tool to the list of things I have to learn for this project (I'm already using it to learn SQLAlchemy andcolander, as well as test-driven development, so I have enough on my platealready!)

I've looked around on the web for information - there's a lot available on writing the tests themselves, but surprisingly little on how to structure aproject for easy testing (unless I've just failed miserably to find the right search terms :))

Thanks for any help,
Paul
 
Ad

Advertisements

S

Serhiy Storchaka

20.12.13 16:47, Paul Moore напиÑав(ла):
What's the best way of structuring my projects so that:

1. I can run all the tests easily on demand.
2. I can run just the functional or unit tests when needed.

python -m unittest discover -s tests/functional
python -m unittest discover tests/functional
3. I can run individual tests (or maybe just individual test modules, I don't have so many tests yet that I know how detailed I'll need to get!) without too much messing (and certainly without changing any source files!)

python -m unittest discover -s tests/functional -p test_spam.py
python -m unittest discover tests/functional -p test_spam.py
python -m unittest discover tests/functional test_spam.py
 
P

Paul Moore

20.12.13 16:47, Paul Moore напиÑав(ла):


python -m unittest discover -s tests/functional
python -m unittest discover tests/functional

Hmm, I could have sworn I'd tried that. But you're absolutely right.

Thanks, and sorry for the waste of bandwidth...
Paul
 
Ad

Advertisements

T

Terry Reedy

20.12.13 16:47, Paul Moore напиÑав(ла):

It depends on your tradeoff between extra setup in the files and how
much you type each time you run tests.

I believe that if you copy Lib/idlelib/idle_test/__init__.py to
tests/__main__.py and add
import unittest; unittest.main()
then
python -m tests
would run all your tests. Lib/idlelib/idle_test/README.py may help explain.
python -m unittest discover -s tests/functional
python -m unittest discover tests/functional

Ditto for __main__.py files in each, so
python -m tests.unit (functional)
will work.
python -m unittest discover -s tests/functional -p test_spam.py
python -m unittest discover tests/functional -p test_spam.py
python -m unittest discover tests/functional test_spam.py

'discover' is not needed for single files. For instance,
python -m unittest idlelib.idle_test.test_calltips
works for me. One can extend that to test cases and methods.
python -m unittest idlelib.idle_test.test_calltips.Get_entityTest
and
python -m unittest
idlelib.idle_test.test_calltips.Get_entityTest.test_bad_entity

If you add to each test_xyz.py file
if __name__ == '__main__':
unittest.main(verbosity=2) # example of adding fixed option
then
python -m tests.unit.test_xyz
will run the tests in that file. (So does F5 in an Idle editor, which is
how I run individual test files while editing. I copy the boilerplate
from README.txt or an existing test_xyz.py file.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top