How do I configure PyCharm to run py.test tests?
PyCharm 2017.3
Preference -> Tools -> Python integrated Tools
- Choosepy.test
asDefault test runner
.- If you use Django
Preference -> Languages&Frameworks -> Django
- Set tick onDo not use Django Test runner
- Clear all previously existing test configurations from
Run/Debug configuration
, otherwise tests will be run with those older configurations. - To set some default additional arguments update py.test default configuration.
Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you'll get the py.test option to create tests instead of the unittest one.
I think you need to use the Run/Debug Configuration item on the toolbar. Click it and 'Edit Configurations' (or alternatively use the menu item Run->Edit Configurations). In the 'Defaults' section in the left pane there is a 'py.test' item which I think is what you want.
I also found that the manual didn't match up to the UI for this. Hope I've understood the problem correctly and that helps.
Here is how I made it work with pytest 3.7.2
(installed via pip) and pycharms 2017.3
:
- Go to
edit configurations
- Add a new run config and select
py.test
- In the run config details, you need to set
target
=python
and the unnamed field below totests
. It looks like this is the name of your test folder. Not too sure tough. I also recommend the-s
argument so that if you debug your tests, the console will behave properly. Without the argument pytest captures the output and makes the debug console buggy.
- My tests folder looks like that. This is just below the root of my project (
my_project/tests
).
- My
foobar_test.py
file: (no imports needed):
def test_foobar(): print("hello pytest") assert True
- Run it with the normal run command