I am using pyramids framework for large project and I find it messy to have all my tests in one tests.py file. So I have decided to create directory that would contain files with my tests. Problem is, I have no idea, how to tell pyramids, to run my tests from this directory.
I am running the tests using python setup.py test -q.
But this of course do not work, after I have moved my tests into tests directory. What to do, to make it work?
First, you need to make sure
testsis not just a directory, but a Python package by creating an__init__.pyin it.You also need to make sure you name the modules in your
testspackagetest_something.py.Most test runners, as part of their test discovery, look for a module or package named
tests, modules in that package starting withtest_and expect method names for test methods (onTestCasesubclasses) to start withtest_.The unittest module describes test runners as:
There are plenty of different testing frameworks and hence test runners out there, most extending
unittestin some way and looking forunittest.TestCasesubclasses. They may do different types of test discovery, present the results in a different way or gather code coverage while the tests are being run.As for relative imports: You should really try to avoid these. They make it harder to move code around (as you just noticed) and decrease the readability of the imports (where does what code get imported from?). Just use
from myproject.views import my_view– it’s a lot clearer where things live