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:

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

 

 

 

2
12 comments

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.

3
Avatar
Permanently deleted user

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.

0

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.

0
Avatar
Permanently deleted user

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.

0

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.

0
Avatar
Permanently deleted user

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.

15
Avatar
Permanently deleted user

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.

4

Hello, 

 

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. 

0

Brandon Braner, you saved my day ! Thanks ! I shared with my fellow "algorithme 1" classmates.

0

Currently running into a (maybe?) similar issue.

Edit Configurations > Edit configuration templates > pytest > Select "Use specified interpreter" ( Remote docker image )

Click gutter play icon, it incorrectly runs in *other* interpreter which is just a pipenv locally (the "SDK of module"). Have deleted previous run configurations, cleared caches, restarted. It does however respect pytest flags set in the config template. 

Yes, I have two interpreters. I use the local pipenv because Intellij IDEA doesn't do so well with keeping installed python packages up to date between the remote container and its notifications (routinely have to click "install missing dependencies" and it does nothing). 

pipenv environment for tracking local build

remote environment for running app/tests.

But my configuration aside, the "Use specified interpreter" is not respected and used when specified in the "Edit configuration templates" modal (project SDK is used). Specified pytest cli flags are used when set in template. So half works/half doesn't.

0

This issue is still persisting as this post advice is creating another pytest configuration worked for me, the 'pydev bugger: process 61985 is still connecting" error is still coming up, but about five seconds later the pytest code runs.

0

I used the same tools menu to solve a similar issue with the latest version of PyCharm. It was not using the right tests configuration template which I added through the run/test dialog, until I changed from `autodetect` to `pytest`. Thanks for pointing out that settings menu!

0

Please sign in to leave a comment.