Reputation: 434
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?
Upvotes: 14
Views: 10770
Reputation: 35
Project Structure
.test
folder and select hit. Mark
it as Tests
by clicking the Tests
option in bar at the top of the window.You should now be able to run all tests in subfodlers (at any level within the tests folder) by right clicking.
Upvotes: 1
Reputation: 803
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)
Upvotes: 12
Reputation: 6119
It is really a pity that PyChorm does not support this while IntelliJ does. So I use a rather hacky workaround. I have created an "all_tests" folder. Then I have symlinked all test folders of all my modules into this one. From here it is straight forward, just right click and run all tests.
Note that windows also supports symlinks its called mklink
however it is not easily possible to support windows and linux at the same time.
Upvotes: 1
Reputation: 1034
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".
Upvotes: 1
Reputation: 5822
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)
Upvotes: 3
Reputation: 69
You can use
python -m unittest discover -s project_directory -p "*_test.py"
or
python -m unittest discover project_directory "*_test.py"
Details in unittest document “Test Discovery” chapter
Upvotes: -3