4
votes

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?

3

3 Answers

5
votes

First, you need to make sure tests is not just a directory, but a Python package by creating an __init__.py in it.

You also need to make sure you name the modules in your tests package test_something.py.

Most test runners, as part of their test discovery, look for a module or package named tests, modules in that package starting with test_ and expect method names for test methods (on TestCase subclasses) to start with test_.

The unittest module describes test runners as:

A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

There are plenty of different testing frameworks and hence test runners out there, most extending unittest in some way and looking for unittest.TestCase subclasses. 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

1
votes

I have finally found the way to do it. I have just created directory named tests, put my tests inside it and created empty file __init__.py. I needed to fix relative imports, or it make strange errors like:

AttributeError: 'module' object has no attribute 'tests'

I do not really understand what is going on, and what is the nosetest role here, but it works.

If someone is able to explain this problematics deeper, it would be nice.

0
votes

Try using nose
http://nose.readthedocs.org/en/latest/
If all your test files start with "Test", nosetests should pick them all up and run them.