Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How can I convince py.test or nose to look for tests inside all python files, recursively - '''including the ones that do not have test in their filenames'''?

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

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

I do not want to manually create a list of all files containing the test, I want a solution that supports discovery.

like image 492
sorin Avatar asked Sep 08 '10 17:09

sorin


People also ask

Is there a way to specify which pytest tests to run from a file?

Running pytestWe can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

Can pytest run nose tests?

pytest has basic support for running tests written for nose.

How do I run all Python unit tests in a directory?

Run all Python tests in a directoryFrom the context menu, select the corresponding run command. If the directory contains tests that belong to the different testing frameworks, select the configuration to be used. For example, select Run 'All tests in: <directory name>' Run pytest in <directory name>'.


1 Answers

You can also have a look at Nose which will discover tests without having to use a fixed file name convention.

You can bypass the regexp used to filter files in 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 running. Be aware that you are in fact loading all modules and searching for tests in them. Beware of any side effect of module loading.

like image 150
Rod Avatar answered Sep 30 '22 17:09

Rod