Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does pycharm not recognize my pytest tests and show the test output?

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!

like image 733
ChrisBogDog Avatar asked Jan 29 '19 13:01

ChrisBogDog


People also ask

How do I run a test file in PyCharm?

Run or debug a test Press Alt+Shift+F10 to see the list of available run configurations or Alt+Shift+F9 for debug configurations. to the right of the list. Alternatively, select Run | Run Shift+F10 or Run | Debug Shift+F9 from the main menu.

How do I run single pytest in PyCharm?

PyCharm makes it easy to select just one test to run. In fact, there are several ways to do it: With the cursor anywhere in the test you want to focus on, right-click and choose to run that in the test runner. Right-click on the test in the test tool listing and choose to run it.


2 Answers

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)

like image 91
Slam Avatar answered Sep 17 '22 08:09

Slam


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.

like image 27
mooocow Avatar answered Sep 20 '22 08:09

mooocow