Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Pycharm run all python unit tests recursively from tests folder

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?

like image 416
Martin of Hessle Avatar asked Dec 07 '18 08:12

Martin of Hessle


People also ask

How do I run all Python unit tests in a directory?

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>'.

Which function in unit test will run all of your tests in Python?

pytest. The pytest test runner supports the execution of unittest test cases. The actual benefit of the pytest is to writing pytest test cases.

How do I run unit tests in PyCharm?

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.


3 Answers

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)

like image 81
MaVCArt Avatar answered Sep 21 '22 14:09

MaVCArt


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)

like image 38
Karl Avatar answered Sep 20 '22 14:09

Karl


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".

like image 36
Zaid E. Avatar answered Sep 20 '22 14:09

Zaid E.