Running unittest discover ignoring specific directory

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).