[Fixed]-Django test discovery does not find non django test cases

1👍

Debugging

Is your utils dir a valid python package (has an __init__.py?).
And, are the modules under it importable? E.g can you do the following:

cd root_dir/
python -c "from src.utils import test_nondjango1"

If the above raises an ImportError, making it importable is one way you can make the tests “discoverable”. Another way would be to specify the directory explicitly when running the tests:

python manage.py tests src.wit src/utils/

This provides two labels to the test runner: src.wit which a full Python dotted path and src/utils/ which is a relative directory.

Explanation

[Django docs state] that it “unittest module’s built-in test discovery“. The first paragraphs in the linked docs say:

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

Test discovery is implemented in TestLoader.discover(), but can also
be used from the command line…

Just to clarify further, the discover() method docs says:

discover(start_dir, pattern=’test*.py’, top_level_dir=None)

Find all the test modules by recursing into subdirectories from the
specified start directory, and return a TestSuite object containing
them. Only test files that match pattern will be loaded. (Using shell
style pattern matching.) Only module names that are importable (i.e.
are valid Python identifiers) will be loaded.

All test modules must be importable from the top level of the project.
If the start directory is not the top level directory then the top
level directory must be specified separately.

If importing a module fails, for example due to a syntax error, then
this will be recorded as a single error and discovery will continue.
If the import failure is due to SkipTest being raised, it will be
recorded as a skip instead of an error.

Leave a comment