UnitTest Framework - Test Discovery



The TestLoader class has a discover() function. Python testing framework uses this for simple test discovery. In order to be compatible, modules and packages containing tests must be importable from top level directory.

The following is the basic command line usage of test discovery −

Python –m unittest discover

Interpreter tries to load all modules containing test from current directory and inner directories recursively. Other command line options are −

Sr.No. Options & Description
1

-v, --verbose

Verbose output

2

-s, --start-directory

directory Directory to start discovery (. default)

3

-p, --pattern

pattern Pattern to match test files (test*.py default)

4

-t, --top-level-directory

directory Top level directory of project (defaults to start directory)

For example, in order to discover the tests in modules whose names start with 'assert' in 'tests' directory, the following command line is used −

C:\python27>python –m unittest –v –s "c:\test" –p "assert*.py"

Test discovery loads tests by importing them. Once test discovery has found all the test files from the start directory you specify, it turns the paths into package names to import.

If you supply the start directory as a package name rather than a path to a directory then discover assumes that whichever location it imports from is the location you intended, so you will not get the warning.

Advertisements