Why does PyCharm not run pytest testing for my application?

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


I want to test the db.py module.

I configure pycharm to use pytest as my test runner ( I verified this). 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  

=================================================== 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!   I almost want to say maybe I hit an issue, but I can't be sure.    Any ideas on how to help?


I invalidated the cache and restarte to see if that made a difference and it diddn't.




Comment actions Permalink

Hi Chris,

It seems that PyCharm run the normal configuration instead of a test. Please try to manually create a pytest configuration in **Run > Edit configurations > "+" button > Python tests > pytest**

See screenshot: http://bit.ly/2HGMArX

Explicitly specify the path to your script and run it.

Comment actions Permalink

So that was in fact the issue.   It wasn’t clear because all the documentation didn’t specify that I manually had to create that profile for each test.   But now that I know, hopefully it will be easier in the future.

Comment actions Permalink

Well, you don't always have to create it manually, it should be created automatically when you click the green arrow. But in your case it doesn't do that for some reason.

Anyway, creating configurations manually is preferred as it gives you more control over what you are doing.

Comment actions Permalink

well, it seems that Chris's problem is also my problem. It doesn't seem to matter what caches I clear, what process i use in installation and reinstallation of pycharm, what tooling setups I use, it simply refuses to recognise Pytest in any useful way.

Comment actions Permalink

Hi Michael,

Could you clarify if you have this problem even with a new script with some simple pytest?
Make sure you don't have any run configurations for that script before running it as a pytest. Otherwise, PyCharm will pick that up.

Comment actions Permalink

I had this same problem and it turns out PyCharm had configured a unittest test runner prior to me installing pytest.
Two things I had to do, go to preferences and Tools > Python Integrated Tools and set Default test runner to pytest
Second go to edit configurations and delete the default test runner and add the pytest runner.

Comment actions Permalink

I had the same issue. Specifically, my problem was that if ran the test from its specific configuration (top right corner in the GUI window) everything worked as expected. However, when I was trying to execute the same test with the little green play button next to the function's code, the test wasn't really executed as in the original problem description.

The issue was that I already had a regular (non-test) run configuration for the module and pycharm kept running this instead of the test configuration, even though the tooltip suggested otherwise. 

TL;DR: make sure that you do not have a regular (non-test) run configuration for the entire module.

Comment actions Permalink



Please vote for the existing issue here https://youtrack.jetbrains.com/issue/PY-36395  in order to increase its priority and try the workaround provided https://youtrack.jetbrains.com/issue/PY-36395#focus=streamItem-27-3528432.0-0 


Apologies for the inconvenience. 


Please sign in to leave a comment.