I'm writing some unit tests that I'd like to debug in pycharm. Debugging works just fine. However, if I want to run the tests outside of debug mode, I get:
C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py
Testing started at 5:04 PM ...
Ran 1 test in 0.002s
Launching unittests with arguments python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py in D:/
FAILED (errors=1)
...
...
module = __import__(module_name)
ModuleNotFoundError: No module named 'test_misc'
However, I am able to get them to run the tests in the command line with:
"C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py
OR this:
python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py
You will notice that both of the above commands are copied and pasted directly from the PyCharm run output where the error occurs.
These commands work when run from any location within the top level project.
I am using unittest with default run configurations which are automatically populated on first run, for example:
With a standard project structure like:
project_folder
|
|- top_level_package/
| |
| |- __init__.py
| |- sub_packages/ (containing modules)
|
|-tests/
|- __init__.py
|- package_to_test/
|
|- __init__.py
|- test_module1.py
|- test_module2.py
How do I get unit tests to run in PyCharm outside of debug mode? Obviously I could just run them in debug mode, but then I'd have to remove all of my breakpoints.
my approach has been to rename package_to_test to package_to_test_t After it pycharm with default params starts to resolve the package names.
To run tests on command line I usually add in unittest classes before importing my packages (need to check number of os.path.dirname() calls depending the directory levels of your test.
script_directory = os.path.dirname(os.path.abspath(__file__))
projroot_directory = os.path.dirname(os.path.dirname(script_directory))
sys.path.extend([os.path.join(projroot_directory, 'top_level_package')])
project_folder
|
|- top_level_package/
| |
| |- __init__.py
| |- sub_packages/ (containing modules)
|
|-tests/
|- __init__.py
|- package_to_test_t/
|
|- __init__.py
|- test_module1.py
|- test_module2.py