Nose: check all modules in this package

I have a package with several subpackages, one of them for tests (named tests ). Since the name of the subpackage makes it clear that the modules it contains are test modules, I prefer not to pollute the module names with the nose of the test template, which should include them for testing. This is the setup I'm thinking of:

 - foo - __init__.py ... - bar - __init__.py ... - baz - __init__.py ... - tests - __init__.py - something.py 

Now, by default, the nose does not run the tests found in foo.tests.something . I know that the nose accepts the -i option to define regular expressions for additional material for finding tests. So nose -i something does the job here. However, I have a bunch of modules in the tests package and don't want to name them explicitly. nose -i tests\..* does not work, it looks like the nose matches only the base names of the modules. As a last resort, I could run nose --all-modules , but it also checks foo.bar and foo.baz - I would like to avoid this.

So, how can I teach my nose to search for tests in all modules within this package ( tests in my case)? I could write a plugin for this task, but I'm looking for a standard solution.

+4
source share
3 answers

You should be able to import files into your __init__.py and pick them up. I had this setting:

 - app: - __init__.py - models.py - views.py - tests: - __init__.py - models.py # contain tests - views.py # contain tests 

In the tests/__init__.py , I had the following:

 from app.tests.models import * from app.tests.views import * 

Which strikes one of the benefits of using regex to search for tests (nose target), but it worked.

You can also use the @istest decorator to add a separate def as a test if you want to avoid assigning methods to the regular expression. I did not try to do this for modules (py files) that also do not match the regular expression, but I doubt that it will work without the specified import.

Note. I moved away from importing into __init__.py and just prefixed my testing methods and file names with test_ and postfix of my classes with Test. I believe that this makes the code more self-describing, since even in the Test class there can be installation methods and auxiliary methods (for example, generators).

+3
source

If you name the files in the tests with the prefix test_ (i.e., rename something.py to test_something.py ), nose launch should start with them by default.

You say: โ€œI prefer not to pollute the module names with the test name, which the nose suggests including them for testing,โ€ but โ€œsomethingโ€ does not describe the file because the file is checking something. What is the problem with using a non-confusing standard way of naming your tests?

+4
source

with nose:

 nosetests --all-modules 

With py.test, this can be done by placing this in the setup.cfg file (configure file):

 [pytest] python_files=*.py 

check this document: pytest docs .

-1
source

All Articles