How to run a particular test in PyCharm - python

I have 60 functional tests in one file. I wrote them in Notepad++ and used py.test as the test framework. Today I decided to swap Notepad++ with PyCharm. I opened my file of functional tests in PyCharm and ran the tests from PyCharm, as you can see in the picture:
Now, after confirming that I could run all of the tests, I tried to run an individual test, for example test_login_with_extantUser_using_email. Logically, I right-clicked on the test, expecting a "run test" button or something similar to appear. But no such thing appeared. In fact, it appears that there is no way to run an individual test by simply right-clicking on it.
So my question is, how can I run an individual test? Must I set up a configuration for each one in the Edit Configurations menu? That would take a very long time, considering that I have 60 tests.

I have found the problem. It's a bug in Pycharm that needs to be fixed. I will explain the bug.
Right-clicking an individual test will not display the option to run the test if the test is not a member of a class that inherits from unittest.TestCase. This is true even if you are not using unittest, as in my case, in which I am using py.test.
When I made my py.test test classes inherit from unittest.TestCase, I got the option to run tests individually when I right-clicked on a test.
I have reported the bug to Pycharm. Time will tell if they fix it.
https://youtrack.jetbrains.com/issue/PY-26754

Base on pt.test doc you can specify particular test_case for running with -k (which means keywords obviously). In your case it should be like pytest -k "test_login_with_extantUser_using_email" if you running in CLI.
If you want to run particular test with Pycharm, you have to fill 'Keywords' field in Run/Debug Configurations.
Best Regards.

Related

How can I use debugger with unit test in Python with Pycharm?

I have the following problem :
I am doing some unit tests but the problem is that I cannot use the debugger I tried to click on "Debug namefile" using a breakpoint but it does not work. Alternatively, I tried to use tyhe following decorator #override_settings(DEBUG=True) but once again I had no result using this way.
I precise that it is only with unit tests I have this kind of problems. The other part of the code works well.
Could you help me please ?
PS: to do the unit test I imported TestCase from django.test.
Thank you very much !
I used to have same problem with PyCharm+Django when running python manage.py test from command line. I solved it by creating new configuration for test.
Mainly, you will need to fill "Script path" (path to manage.py) and "Parameters".
Then, run debug with that configuration and breakpoints in Django tests will work.
PyCharm menu:
Run / Edit configuration / Add Django test / Add Target and options.
For example, here is a mapping from command line to GUI fields,
Run just one test method
./manage.py test --keepdb animals.tests.AnimalTestCase.test_animals_can_speak
Target: animals.tests.AnimalTestCase.test_animals_can_speak
options: --keepdb
.
Ref, https://www.jetbrains.com/help/pycharm/2020.1/run-debug-configuration-django-test.html

PyCharm duplicated py.test tests assertions

Everything works fine with PyCharm and pytest, except if I have failing tests, then it duplicates the error output:
One of actual failures if the red one and other is white. This is really annoys, and I haven't found any way to disable such behaviour.
There is an option to disable log via py.test, however it will disable all logging.
Note: everything works as expected if I run python -m pytest test.py.
I think that is a feature not a bug. The top level is being emitted during the testing which allows you to review the failure before the testing is complete. The second copy of the results is the summary which effectively removes any of the text that was showing test progress.
You can easily view just part of the test output by clicking on the test hierarchy:
The duplicated output can be elimited by running pytest with the -q or --quiet parameter.
You can configure the parameter to be applied to all PyCharm pytest tests by setting it in Edit Configurations --> Templates --> Python Tests --> pytest --> Additional Arguments.
This will then apply those arguments to all new run configurations. If you have a bunch of existing test run configurations, deleting all of them and then re-generating them by running a test or tests using the gutter icon is the quickest way to reset the output.

How to force Pycharm to run all unit tests?

I have a single unit test file, and I can run tests within Pycharm. Fine. However, it seems that Pycharm skips tests based on some criteria regarding code change. Something like, if the code in a test method hasn't changed, and/or if the code that a particular test method is testing hasn't changed, it won't run the test. This has caused a lot of pain, and has let a bug fall through that wasn't caught until much later on. So my question is, how to stop Pycharm from automatically skipping tests and force it to run all tests?
Why is skipping tests the default behaviour anyway? This seems absolutely outrageous to me, but please correct me if I'm wrong.
EDIT
Oops my bad. I ran the tests by pressing the keyboard shortcut control-shift-R on my Mac OS, which normally runs a Python script, but it doesn't actually run the whole test file, and only runs a single test (where the caret is) instead. This (keyboard shortcut having different behaviours) is a little bit misleading in my opinion, but regardless, my description of the problem is wrong and I was wrong. Sorry!
Make sure all your test methods staring with a test_.
Usually I have 2 Tests configurations: All and Current.
tests folder is located in the root of PyCharm project
I use python path to specify certain test to run.
You can easily copy the reference to certain test

Pycharm/IntelliJ shows 0% coverage for pytest even though coverage was generated

I have a Python project and a tests task, set up to run pytest from the project's working directory.
Doing Run 'tests' with coverage from the Run menu successfully runs the tests, and the console results shows that coverage was measured - e.g. 53% cover for mws.py.
The automatically applied coverage (as on the right) is 0% for all files, I'm not sure why. I'm using IntelliJ 2017.2.2 EAP.
NB: there is a related five year old question here, but the top rated solution there doesn't apply. There is no error message in the results console in this case.
I had a similar issue, but the accepted solution here didn't solve it.
I had pytest automatically run coverage in its configuration file.
In PyCharm, I added a Run Configuration to run all my tests with pytest.
It seemed to work, and I saw all tests running and got their results to display in PyCharm's run window.
But soon I noticed there were two problems:
When I selected "Run with coverage" I got an error like "coverage results not found", and all files showed 0% coverage.
Breakpoints in tests were not hit when running test in Debug mode.
Both problems disappeared when I added --no-cov to the "Additional Arguments" passed to to pytest (this option is in the Run Configuration).
So It seems the fix was to tell pytest to not run coverage when running it from PyCharm. Both "Run with coverage" option and the Breakpoints in tests now work.
I think the problem lies in you use pytest-cov, so Pycharm cannot parse the result which is shown in text like 53% generated by pytest-cov;
So Changes option in pytest.ini to addopts = -s -v when you want to use Pycharm built in coverage tools.
In this command -v stands for verbosity and -s for disabling all output.
Take a look at my answer for other question about same issue: https://stackoverflow.com/a/45729723/1229510
Basically if you use symlinks - coverage display won't work.

Idea run/debug py.test single test not the whole suite

I'm creating a python test suite (using py.test). I'm coding the tests in Idea and I don't know how to debug a single test.
This is my setting of the debugger. It runs the whole testsuite. So I have to run all the tests before it gets to the one I'm trying to debug.
In your configuration, set:
Target to the relative path of one of your test files, i.e. testsuite/psa/test_psa_integration.py
Keywords to a keyword that identifies the test you are trying to run specifically. If tests are part of a class, Keywords should be something like: TestPsaIntegration and test_psa_integration_example
I don't use IntelliJ, but in PyCharm, you can easily debug tests without going through this tedious process of adding a Run/Debug configuration each time.
To do this with PyCharm, go to:
Preferences (or Settings) > Tools > Python Integrated Tools and set Default test runner to py.test.
Then, back in your file (i.e. test_psa_integration.py), you could just right-click anywhere within the code of a test, and select either Run 'py.test in ...' or Debug 'py.test in...' which will automatically create a new Run/Debug configuration as explained previously.
An alternative is adding --no-cov --capture=no into Additional Arguments. To make this automatic for other test file, add those into the Template part.

Categories

Resources