Recently I went through a process of breaking a large django project into
smaller installable applications. Each smaller component could be reused from
within any number of django projects, but wasn’t a django project
itself. One of the issues I encountered was “What do I do with the unit
tests?” Using the standard
./manage.py test no longer worked for me because
my settings where in the master project.
I had heard of py.test, so this seemed like an opportunity to see if some of the py.test magic would work for me. Admittedly, I didn’t do a large amount of searching around for additional testing frameworks or processes…this was an excuse to try out the project. :)
Installing py.test is easy. Because I wanted some additional features (DJANGO_SETTINGS_MODULE environment variable specifically), so I opted for the pytest-django module instead of the base pytest project.
To get my unit tests running, I needed to add a few additional things:
- a test settings file
- a conftest.py file
- a pytest.ini file
- a small amount of test package cleanup
test settings file
Created a very light settings file with only my database configuration
1 2 3 4 5 6
This was required to fix an issue with my settings file location.
1 2 3 4
As a convenience, instead of passing parameters on the commandline each time, py.test uses a pytest.ini file to pass these arguments to the test runner.
test package cleanup
py.test has smarter test resolution. To take advantage of these features, I did the following:
- Removed statements like
from mytests import *from the
- Changed the name of my tests to match test* format