In this chapter, we will discuss 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 the top-level directory.
The following is the basic command line usage of test discovery −
Python –m unittest discover
The interpreter tries to load all modules containing tests from the current directory and inner directories recursively. Other command-line options are −
Sr.No. | Options & Description |
---|---|
1 | -v, –verbose output |
2 | -s, –start-directory Directory to start discovery (. default) |
3 | -p, –pattern Pattern to match test files (test*.py default) |
4 | -t, –top-level-directory directory Top-level directory of the project (defaults to start directory) |
For example, in order to discover the tests in modules whose names start with ‘assert’ in the ‘tests’ directory, the following command line is used −
C:\python27>python –m unittest –v –s "c:\test" –p "assert*.py"
Test discovery loads test 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.
Next Topic – Click Here
Pingback: UnitTest Framework - Assertion - Adglob Infosystem Pvt Ltd