Debug School

rakesh kumar
rakesh kumar

Posted on

Explain test discovery in unittest with example

Unittest supports simple test discovery. In order to be compatible with test discovery, all of the test files must be modules or packages importable from the top-level directory of the project (this means that their filenames must be valid identifiers).

Test discovery is implemented in, but can also be used from the command line. The basic command-line usage is:

cd project_directory
python -m unittest discover
Enter fullscreen mode Exit fullscreen mode

The discover sub-command has the following options:

-v, --verbose
Verbose output

-s, --start-directory directory
Directory to start discovery (. default)

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

-t, --top-level-directory directory
Enter fullscreen mode Exit fullscreen mode

Top level directory of project (defaults to start directory)

The -s, -p, and -t options can be passed in as positional arguments in that order. The following two command lines are equivalent:

python -m unittest discover -s project_directory -p "*"
python -m unittest discover project_directory "*"
Enter fullscreen mode Exit fullscreen mode

As well as being a path it is possible to pass a package name, for example myproject.subpackage.test, as the start directory. The package name you supply will then be imported and its location on the filesystem will be used as the start directory.

Testing discovery in Python's unittest framework is a process that automatically identifies and runs tests within a project or directory. You can use test discovery to find and execute test cases without having to specify them individually. Here's a checklist for performing test discovery in a project using unittest, along with an example and expected output:

Checklist for Test Discovery:

  1. Organize your tests into Python modules and classes that subclass unittest.TestCase.
  2. Place your test modules within the project directory.
  3. Use the unittest test discovery mechanism to locate and run the tests.
  4. Run the test discovery command from the project directory . Example:

Suppose you have organized your test modules and classes within a project directory like this:

└── ...
Enter fullscreen mode Exit fullscreen mode

Each of the test_*.py modules contains test classes with test methods, as shown in previous examples.

Run Test Discovery:

To discover and run all tests within the project, follow these steps:

Open your command line or terminal.

Navigate to the root directory of your project where the test modules are located:

cd /path/to/project
Enter fullscreen mode Exit fullscreen mode

Run the test discovery command:

python -m unittest discover
Enter fullscreen mode Exit fullscreen mode

Expected Output:

The test discovery command will search for and run all tests within the project and its subdirectories. The output will provide information about the discovered tests, which test cases were run, and whether they passed or failed. Here's an example of the expected output:

test_addition (test_math.TestMathOperations) ... ok
test_subtraction (test_math.TestMathOperations) ... ok
test_string_concatenation (test_strings.TestStringOperations) ... ok
test_string_length (test_strings.TestStringOperations) ... ok

Ran 4 tests in 0.003s

Enter fullscreen mode Exit fullscreen mode

In this example, four tests were discovered, and all of them passed, as indicated by "ok" for each test. The summary at the end shows that four tests were run, and they all passed ("OK").

Test discovery in unittest is a convenient way to automatically locate and execute tests within a project or directory, making it easier to maintain and execute your test suite as your project grows.


Top comments (0)