Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run all tests from subdirectories in Python

I am at my wits end with trying to get all my unittest to run in Python. I have searched about 30 different posts and the unit test documentation but still cannot figure it out.

First I have two test classes that I can run each individually and all the tests pass:

File: unittest.subfolder1.TestObject1.py

class TestObject1(unittest.TestCase):
  def test_case1(self):
    ...some code...
    ...some assertions...

if __name__ == '__main__':
  unittest.main()

File: unittest.subfolder2.TestObject2.py

class TestObject2(unittest.TestCase):
  def test_case1(self):
    ...some code...
    ...some assertions...

if __name__ == '__main__':
  unittest.main()

Starting in the top level directory above 'unittest' I am trying to us unittest.discover to find and run all my tests:

import unittest

loader = unittest.TestLoader()
suite = loader.discover('unittest')
unittest.TextTestRunner().run(suite)

When I do this I get the error `ModuleNotFoundError: No module named 'subfolder1.TestObject1'

What am I doing wrong?

like image 927
EliSquared Avatar asked Jun 22 '17 19:06

EliSquared


People also ask

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

You must have an empty (or otherwise) __init__.py file in your test directory (must be named test/ ) Your test files inside test/ match the pattern test_*. py . They can be inside a subdirectory under test/ , and those subdirs can be named as anything.

How do I run a test suite in Python?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

Which function in unittest will run all your tests?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.


1 Answers

A good approach is to run all the tests in a subdirectory from the command line. In order to find the following files "TestObject1.py, TestObject2.py, ..." in subdirectories, you can run the following command in the command line:

python -m unittest discover -p 'Test*.py'

Additionally, the __init__.py is required within the import and module directories: Python unittest discovery with subfolders

The import unittest is required in the files unittest.subfolder1.TestObject1.py and unittest.subfolder2.TestObject2.py

It is also possible to define explicitly the directory where the discovery starts with the -s parameter:

python -m unittest discover [options]

-s directory     Directory to start discovery ('.' default)
-p pattern       Pattern to match test files ('test*.py' default)

In case you are using unittest2, it comes with a script unit2. The command line usage is:

unit2 discover unit2 -v test_module
like image 177
Rene B. Avatar answered Sep 20 '22 12:09

Rene B.