I'm from Java background, so I've organised my unit tests into to separate parallel test Hierarchy that reflects the structure of my main project. I'm using PyCharm in place of Intellij or Eclipse. In both of these I IDEs I could choose any package under test and run all unittests recursively under this namespace.
Test Structure
+ tests
+ billing
+ supplier
+ ClassName_tests.py - file
- TestClassName - class
- test_one() - functions
- test_two() - functions
+ config
...
+ invoicing
...
Is this possible with Python and/or PyCharm? Currently I need to run the the tests each namespace/module individually Do I have to define something in PyCharm or Python.
I've read and tried this setup but it runs the all the tests in the the selected folder, not recursively. How to force Pycharm to run all unit tests?
Run all Python tests in a directoryFrom the context menu, select the corresponding run command. If the directory contains tests that belong to the different testing frameworks, select the configuration to be used. For example, select Run 'All tests in: <directory name>' Run pytest in <directory name>'.
pytest. The pytest test runner supports the execution of unittest test cases. The actual benefit of the pytest is to writing pytest test cases.
Right-click a test file or test class in the Project tool window or open it in the editor, and right-click the background. From the context menu, select Run <class name>/Run <filename> or Debug.... For a test method, open the class in the editor and right click anywhere in the method.
Just posting an answer here because I was stuck on this for a while... Every other answer here kind of dances around the subject, but the solution to your case is relatively simple:
When Pycharm discovers tests in a directory, it treats subdirectories as modules whithin which to discover tests.
This means these folders need to contain an __init__.py
file if you want them to be discoverable.
In other words:
Your example:
+ tests
+ billing
+ supplier
+ ClassName_tests.py - file
+ ... (omitted the rest for brevity)
Should turn into:
+ tests
+ billing
- __init__.py
+ supplier
- __init__.py
- ClassName_tests.py - file
+ ...
After that, right-clicking the "tests" directory and running all tests in it should run all your tests, including the ones in subfolders, but only for subfolders that can be treated as modules (e.g. contain an __init__.py
file)
In PyCharm, first set your default test runner
Now right-click on your "test" folder. There should be a "run py.test" (or similar, depending on which test you chose) option. That's it, nothing more needed.
(EDIT: This works in Professional Edition. I can't confirm whether this works in Community Edition or not)
This is what worked for me:
Make a new shell script inside your parent directory. Say "run_tests.sh".
Write shell code to run tests wherever you want (as YuhaoQI suggested). If you want to run all tests in the project:
python -m unittest discover -s ./ -p "test_*.py"
or If you want to run all tests in one directory "tests":
python -m unittest discover -s tests -p "test_*.py"
In pycharm configurations dropdown click "Edit Configurations" and add the shell script you just made. Name the configuration "All tests".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With