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 t
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
Enable Pytest for you project
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
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.
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.
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