How to make py.test or nose look for tests inside all python files?

I have several small modules where tests are inside them, and py.test or nose does not look for them because they do not contain test in their file name.

How can I convince py.test or nose to look for tests inside all python files, recursively - '' ', including those that don't have test in file names'? '?

Inside the source files, I follow the standard naming convention: class testSomeName with def test_some_name methods.

If this is not possible, what other solution can I use to get the same result.

I don’t want to manually create a list of all the files containing the test, I want a solution that supports detection.

+4
source share
4 answers

You can also look at Nose , which will open the tests without using a fixed-name filename agreement.

You can bypass the regex used to filter files in the nose with the following code. Create a python module (i.e. my_nosetests.py )

 import nose from nose.plugins.base import Plugin class ExtensionPlugin(Plugin): name = "ExtensionPlugin" def options(self, parser, env): Plugin.options(self,parser,env) def configure(self, options, config): Plugin.configure(self, options, config) self.enabled = True def wantFile(self, file): return file.endswith('.py') def wantDirectory(self,directory): return True def wantModule(self,file): return True if __name__ == '__main__': includeDirs = ["-w", ".", ".."] nose.main(addplugins=[ExtensionPlugin()], argv=sys.argv.extend(includeDirs)) 

Now run my_nosetests.py as if you were running nosetests and you should have your tests. Keep in mind that you are actually loading all modules and looking for tests for them. Beware of any side effect of module loading.

+5
source

With py.test it is easy. Create a conftest.py file with this content:

 # content of conftest.py file at root of your project def pytest_collect_file(path, parent): if path.ext == ".py": return parent.Module(path, parent) 

This will expand the collection process to create a test “Module” node for each “.py” file. Entering this file in the conftest.py file makes it an extension for the project, which automatically loads if you enter:

 py.test 

For information purposes, you can also enter:

 py.test --collectonly 

To find out which tests and files are collected, for example, the output:

 <Directory 'morecollect'> <Module 'conftest.py'> <Directory 'pkg'> <Module 'test_x.py'> <Function 'test_hello2'> <Module 'x.py'> # this is collected because of our conftest.py extension <Function 'test_hello'> 

If necessary, you can also package the above conftest.py file as an installable plugin and make the extension available by installing the plugin. In this case, you do not need the conftest.py file at conftest.py .

+6
source

Put the file "setup.cfg" in the root of the project and it contains two lines:

 [pytest] python_files=*.py 

then py.test select tests from all *.py files. Here he explained: pytest docs

with nose:

 nosetests --all-modules 
+5
source

The documentation says that

By default, all directories that do not start with a period intersect looking for _ * test files. py and * _test.py. These Python files are imported under their package name.

Can you make sure this applies to your code?

Update

(Caveat Emptor: I have not tried / tested this) How about using hooks to collect directories and files?

py.test calls the following two main hooks for collecting files and directories:

 def pytest_collect_directory(path, parent): """ return Collection node or None for the given path. """ def pytest_collect_file(path, parent): """ return Collection node or None for the given path. """ 

Both return a node collection for the given path. All returned nodes from all hook implementations will participate in the collection and launch protocol. The parent object is the parent node and can be used to access command-line options through the parent.config object.

+1
source

All Articles