To use pytest as the test runner, I found a bunch of things that weren’t working officially, so I tried to write this blog post to understand the right way of doing it.
Configuring pytest as the test runner
To configure pytest, you need to do the following:
- Go to the Preferences menu ( Within Mac OSX, click on the PyCharm name in the menu bar and then select preferences)
- Click on Tools->Python Integrated Tools
- Under the testing section, select “pytest” under the default test runner field.
Create a new testing file within a project
- Open up or create a new project in PyCharm
- Create or open a python file (which represents the application you want to test). For example, “
- Make a directory a directory called
within the current project
- Create a simple
testfile. Ensure that the name begins or ends with test(ie., test_*.py or *_test.py
- For example:
import testfile import pytest class TestFunctions(object): def test_num1(self): assert True == True
This next section is the one part that I missed that caught me off guard.
Edit a Configuration for the testing module
to Run-> Edit Configurations
- On the top left of the window, click on the ‘+’ module to add a new configuration.
- Select the “Python Tests” menu followed by “
- Select the testing file that we want to test with
pytestin both the “Working Directory” and “Script Path”
- The following window shows a very simple example.
Run Testing Module
- From the Run menu select the Run choice
- Select the name of the module that we want to test
- PyCharm will now show the testing output.
- For example: