简体   繁体   中英

Running unittest discover ignoring specific directory

I'm looking for a way of running python -m unittest discover , which will discover tests in, say, directories A, B and C. However, directories A, B and C have directories named dependencies inside each of them, in which there are also some tests which, however, I don't want to run.

Is there a way to run my tests satisfying these constraints without having to create a script for this?

I ran into the same problem and was eventually able to find these handy arguments to pass to unittest discover that resolved my issue.

It is documented here: https://docs.python.org/2/library/unittest.html#test-discovery

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

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

So I modified my command to be:

python -m unittest discover -s test

since all of the tests I actually want to run are in the one module, test. You could also use the -p to in theory match regex that only hits your tests, ignoring all of the rest it may find.

I've managed to do it this way (In *NIX):

find `pwd` -name '*_test.py' -not -path '*unwanted_path*' \
  | xargs python3 -m unittest -v

That is, the tests are discovered by find , which allows for options like path pattern exclusions, then they're passed to the unittest command as argument list.

Note that I had to switch to find pwd , where usually I can write find . , since relative paths in the form ./xxx aren't accepted by unittest (module not found).

It would seem that python -m unittest descends into module directories but not in other directories.

It quickly tried the following structure

temp
  + a
  - test_1.py
  + dependencies
    - test_a.py

With the result

>python -m unittest discover -s temp\a
test_1
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

However, if the directory is a module directory (containing a file __init__.py ) the situation is different.

temp
+ a
  - __init__.py
  - test_1.py
  + dependencies
    - __init__.py
    - test_a.py

Here the result was

>python -m unittest discover -s temp\a
test_a
.test_1
.
----------------------------------------------------------------------
Ran 2 tests in 0.009s

OK

The usefulness of this answer for you now depends on whether it is acceptable for your folder dependencies not to be a module directory.

EDIT: After seeing your comment

Would using pytest be an option? This test runner has many command arguments, one specifically to exclude tests.

See Changing standard (Python) test discovery

From their site

Ignore paths during test collection

You can easily ignore certain test directories and modules during collection by passing the --ignore=path option on the cli. pytest allows multiple --ignore options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM