14

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?

2
  • With recursively, do you mean "run all tests in the 'test' directory"? Commented Dec 7, 2018 at 9:29
  • 2
    ... and below. Recursively descend the directories AND iterate every test in each directory of the entire tree. Commented Dec 7, 2018 at 10:29

6 Answers 6

12

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)

Sign up to request clarification or add additional context in comments.

2 Comments

This should be the accepted answer.
This only works for me at the level of 'supplier'. Right-clicking on 'billing' or 'tests' I get no option to run tests. :-/
3

In PyCharm, first set your default test runner enter image description here

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)

4 Comments

That is what I'm doing, it only runs the tests in the selected folder, it doesn't recursively descend the directories. /tests is actually empty, but contains 5 directories of tests and each of those includes several sub-folders as well.
What are your test files called? Are far as I know pycharm uses a pretty simple method to find tests. Something along the lines of looking for files/functions with "test" or "Test" in its name
I've added part of my folder structure, along with examples names to the question.
try renaming ClassName_tests.py to "test_ClassName.py"
1

This is what worked for me:

  1. Make a new shell script inside your parent directory. Say "run_tests.sh".

  2. 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"
    
  3. In pycharm configurations dropdown click "Edit Configurations" and add the shell script you just made. Name the configuration "All tests".

1 Comment

That is what I am doing as well, but this does not give you the nice test console at the bottom where you could see and eventually restart failing tests etc.
1

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.

Comments

1
  • Open up settings.
  • Navigate to project settings.
  • Select Project Structure.
  • Locate your top level 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.

Comments

-3

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

3 Comments

In the Python console that fails with error : File "<input>", line 1 python -m unittest discover -s project_directory -p "*_test.py" ^ SyntaxError: invalid syntax
These codes should be executed in terminal,and you need to change that a little.
This is not a correct answer for PyCharm. You should not be running your tests in the terminal!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.