Reputation: 97818
I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory.
PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box specifically for py.test. But that's the complete extent of their documentation on the subject, and I can't find this alleged dialog box anywhere.
If I right-click the directory in the Project tool window, I'm supposed to see a "Create <name>" menu item, but the only menu item starting with "Create" is "Create Run Configuration". Okay, maybe the documentation is just wrong, and "Create Run Configuration" does sound promising. Unfortunately, the only two items in its submenu are "Unittests in C:\mypath..." and "Doctests in C:\mypath...", neither of which applies -- I'm using neither unittest nor doctest. There is no menu item for py.test.
If I open my test_sample.py and right-click in the editor window, I do get the promised "Create <name>" menu items: there's "Create 'Unittests in test_sa...'...", followed by "Run 'Unittests in test_sa...'" and "Debug 'Unittests in test_sa...'". So again, it's all specific to the unittest framework; nothing for py.test.
If I do try the menu items that say "unittest", I get a dialog box with options for "Name", "Type", a "Tests" group box with "Folder" and "Pattern" and "Script" and "Class" and "Function", etc. This sounds exactly like what's documented as the dialog to add a configuration for Python Unit Test, and not like the "Name" and "Test to run" and "Keywords" options that are supposed to show up in the configuration for py.test dialog. There's nothing inside the dialog to switch which test framework I'm adding.
I'm using PyCharm 1.5.2 on Windows with Python 3.1.3 and pytest 2.0.3. I can successfully run py.test
on my tests from the command line, so it's not something simple like pytest not being installed properly.
How do I configure PyCharm to run my py.test tests?
Upvotes: 209
Views: 176205
Reputation: 417
I came across with a bit another case but the same error (I can't past the error here cuz already fixed it).
Check that you really have pytest
installed inside your venv.
I have pytest
installed globally and it's confused me.
If pip is saying that pytest is already installed - check what kind of pip it is (global pip or pip of venv).
which python
should return the path to your venv python.
Then python -m pip install pytest
Upvotes: 0
Reputation: 22356
There is a PyCharm documentation: Run/Debug Configuration: pytest available as of SEP 2020.
Upvotes: 0
Reputation: 97288
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.
Upvotes: 306
Reputation: 4178
Open preferences windows (Command key + "," on Mac):
1.Tools
2.Python Integrated Tools
3.Default test runner
Upvotes: 1
Reputation: 2669
In pycharm 2019.2, you can simply do this to run all tests:
For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html
Upvotes: 17
Reputation: 3975
Enable Pytest for you project
Upvotes: 1
Reputation: 812
PyCharm 2017.3
Preference -> Tools -> Python integrated Tools
- Choose py.test
as Default test runner
.Preference -> Languages&Frameworks -> Django
- Set tick on Do not use Django Test runner
Run/Debug configuration
, otherwise tests will be run with those older configurations. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
Upvotes: 79
Reputation: 7304
With 2018.3 it appears to automatically detect that I'm using pytest, which is nice, but it still doesn't allow running from the top level of the project. I had to run pytest
for each tests
directory individually.
However, I found that I could choose one of the configurations and manually edit it to run at the root of the project and that this worked. I have to manually choose it in the Configurations drop-down - can't right click on the root folder in the Project pane. But at least it allows me to run all tests at once.
Upvotes: 0
Reputation: 41945
Here is how I made it work with pytest 3.7.2
(installed via pip) and pycharms 2017.3
:
edit configurations
py.test
target
=python
and the unnamed field below to tests
. 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_project/tests
).foobar_test.py
file: (no imports needed):def test_foobar(): print("hello pytest") assert True
Upvotes: 22
Reputation: 7986
I'm using 2018.2
I do Run -> Edit Configurations... Then click the + in the upper left of the modal dialog. Select "python tests" -> py.test Then I give it a name like "All test with py.test"
I select Target: module name and put in the module where my tests are (that is 'tests' for me) or the module where all my code is if my tests are mixed in with my code. This was tripping me up.
I set the Python interpreter.
I set the working directory to the project directory.
Upvotes: 2
Reputation: 9089
find this thread when I hit the same question and found the solution pycharm version:2017.1.2 go to "Preferences" -> "Tools" -> "Python Integrated Tools" and set the default test runner from right side panel as py.test solve my problem
Upvotes: 3
Reputation: 10260
With a special Conda python setup which included the pip install for py.test plus usage of the Specs addin (option --spec) (for Rspec like nice test summary language), I had to do ;
1.Edit the default py.test to include option= --spec , which means use the plugin: https://github.com/pchomik/pytest-spec
2.Create new test configuration, using py.test. Change its python interpreter to use ~/anaconda/envs/ your choice of interpreters, eg py27 for my namings.
3.Delete the 'unittests' test configuration.
4.Now the default test config is py.test with my lovely Rspec style outputs. I love it! Thank you everyone!
p.s. Jetbrains' doc on run/debug configs is here: https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test
Upvotes: 0
Reputation: 20520
It's poorly documented to be sure. Once you get add a new configuration from defaults, you will be in the realm of running the "/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py" script. It's not documented and has its own ideas of command line arguments.
You can:
Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.
Upvotes: 6
Reputation: 21925
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.
Upvotes: 21