0

I am attempting to run a subset of tests as part of a build; but I've been unable to run individual tests with python3 -m unittest <test>.

I have tried to follow the documentation and also read through: Running unittest with typical test directory structure, but I am still having no luck.

The directory structure is like this:

new_project
├── new_project
├──── __init__.py
├──── pipeline.py
├── regression_tests
├──── __init__.py
├──── helpers.py
├──── test_pipeline.py
└──── test_connectivity.py

Running python3 -m unittest discover at the new_project level runs all tests fine; but trying to run a subset fails now matter how I try to cut it:

python3 -m unittest regression_tests

With the result: Ran 0 tests in 0.000s

python3 -m unittest regression_tests.test_pipeline
python3 -m unittest regression_tests.test_pipeline.TestClass
python3 -m unittest regression_tests.test_pipeline.TestClass.test_method
--- or ---
python3 -m unittest regression_tests/test_pipeline.py

With the Errors: AttributeError: 'module' object has no attribute 'test_pipeline'

Finally for completeness this also fails (as I would expect as the PYTHONPATH would be incorrectly set):

cd regression_tests
python3 -m unittest test_pipeline

With the Error: ImportError: No module named 'regression_tests' (The error is thrown on the line from regression_tests.helpers import helper_method.)

I don't think I'm doing anything that's non-standard here. Why is it that unittest discover works, but that unittest <test> fails?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David258
  • 697
  • 2
  • 6
  • 15

1 Answers1

0

To run the sub-directory test cases successfully, you need to specify a directory using the below command.

python -m unittest discover -s directoryname -p "test_*"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sadia Arif
  • 231
  • 2
  • 9
  • This works for simple cases so should tide me over. It doesn't solve the underlying problem however which is why `python -m unittest ` without discover doesn't work the way it says it should in the docs. In my more general case this is not going to scale easily as I'm going to have to jump through a few hoops to get the regex for `-p "test_*"` to cover all cases. – David258 Aug 21 '17 at 15:27
  • python -m unittest works for all test cases contained in a file so if you want to run all test cases of a sub-directory you should use discover keyword. – Sadia Arif Aug 22 '17 at 06:49
  • The problem I'm having is it *doesn't* work; I am trying to run only some tests, but I get errors whenever I try. The docs say: `Examples: python3 -m unittest test_module # run tests from test_module python3 -m unittest module.TestClass # run tests from module.TestClass python3 -m unittest module.Class.test_method # run specified test method ` but as above I get an attribute error when I try this – David258 Aug 22 '17 at 12:18
  • You should define from helpers import helper_method instead of from regression_tests.helpers import helper_method. – Sadia Arif Aug 24 '17 at 09:02