17

I'm new to pytest and am trying to write testing modules for my application. My directory structure is:

broker/
broker/tests
broker/tests/conftest.py
broker/tests/test_db.py
broker/db.py

I want to test the db.py module.

I configure pycharm to use pytest as my test runner. When I run the test_db.py in pycharm I get:

/Users/cbogdon/virtualenv/platinum-onboard/bin/python /Users/cbogdon/coding/platinum-onboard/broker/tests/test_db.py

Process finished with exit code 0

It's almost like pycharm is not executing the pytest. Even if I right click on the green arrow to the left of one of my testing functions a menu appears and shows I can click: "Run 'pytest for test_db.py::TestDBFunctions::test_valid_db'"

If I run it at the command line using:

python -m pytest --setup-show tests/test_db.py 

I get the appropriate test output.

 python -m pytest --setup-show tests/test_db.py 
========================================================== test session starts ===========================================================
platform darwin -- Python 3.6.1, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /Users/cbogdon/coding/platinum-onboard/broker, inifile:
collected 4 items                                                                                                                        

tests/test_db.py 
        tests/test_db.py::TestDBFunctions::test_uuid.
        tests/test_db.py::TestDBFunctions::test_invalid_dbF
        tests/test_db.py::TestDBFunctions::test_valid_db.
        tests/test_db.py::TestDBFunctions::test_already_created_database.

================================================================ FAILURES ================================================================
____________________________________________________ TestDBFunctions.test_invalid_db _____________________________________________________

self = <test_db.TestDBFunctions object at 0x102606438>

    def test_invalid_db(self):
>       ret = db.initialize_database()
E       TypeError: initialize_database() missing 1 required positional argument: 'dbname'

tests/test_db.py:14: TypeError
=================================================== 1 failed, 3 passed in 0.08 seconds ==================================================

Is there something special I need to do in PyCharm?

Sorry for the newbie question, but I just can't figure this out one bit!

ChrisBogDog
  • 231
  • 1
  • 2
  • 6

5 Answers5

26

Please make sure that Settings > Tools > Python Integrated Tools > Default test runner is set to pytest & clean cache and restart pycharm (especially if this setting was changed in past)enter image description here

(image courtesy of Brandon Braner)

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Slam
  • 8,112
  • 1
  • 36
  • 44
  • The Default Test Runner was set. I did clean cache and restart pycharm and I get the same result. It isn't showing any test output! – ChrisBogDog Jan 29 '19 at 14:02
  • 4
    Changing the default test runner like this worked for me. Be aware though that if you already run that class before, you also need to delete the run configuration which got created! Only then will PyCharm automatically create a new pytest configuration. Otherwise it will just try to use the old run configuration, which is a default python run config and therefore does not execute your tests – Ursin Brunner Jan 05 '21 at 10:38
6

I found the answer: I had to:

In the Run/Debug Configurations window (Ctrl+Alt+F10 or go to Run > Edit configurations..) click on the + button found on the upper left corner. Go to Python tests and select py.test. Click OK button.

On the left pane you can see Python tests is created. In the right pane for the Name text box give the value as py.test in module_name.py You should always prefix the Python test file name with the words py.test in.

For the Target text box specify the path to the Python test file.

For the Working directory text box specify the path to the directory which has the Python test files.

Click on OK button.

For whatever this worked and now it works!

elebur
  • 465
  • 1
  • 5
ChrisBogDog
  • 231
  • 1
  • 2
  • 6
  • 1
    Please provide a screenshot - it's a bit tricky to follow your steps – WestCoastProjects Dec 06 '22 at 18:28
  • To open _Run/Debug Configurations_ press **Ctrl+Alt+F10** or go to **Run > Edit configurations..** – elebur Aug 22 '23 at 18:53
  • Also, it helps me to remove all existing test configurations from `Run/Debug Configurations` as mentioned here https://stackoverflow.com/a/48074735/19813684 – elebur Aug 22 '23 at 19:06
2

If the other solutions don't work, make sure to set the content root correctly so that your code is properly indexed and tests can be detected. PyCharm -> Preferences -> Project -> Project Structure -> Add Content Root -> Select the root folder as content root.

mooocow
  • 21
  • 1
1

I tried the other two answers and realized there is one more step to go, if you previously set nose or other tests type as default.

Open Run/Debug Configurations > Python tests > click + button > Pytest > In target: choose the Module name > Ctrl+Space so it could do the code completion for each unit test.

screenshot here

Liry Chen
  • 11
  • 1
1

I had a similar issue where I could run my tests perfectly in the terminal but it failed to run using PyCharm. It turns out that if you select your test directory and run it, PyCharm automatically sets the test directory as the working directory as opposed to the root, which caused import issues in my conftest.py. Hence, this broke my tests when using PyCharm as opposed to the terminal.

Solution: manually change your Working Directory to the root

Imaging showing steps to change working directory

Khalil
  • 175
  • 2
  • 8