how to tell PyCharm to run pytest on test files not doctests - python

In the test suite of a module, some test_foo.py files have helper functions containing doctests patterns in their docstrings. This causes PyCharm to offer Debug 'Doctests in test_foo' when selecting a function inside that file and trying via context-menu to debug just that function. It doesn't matter whether that function is before any actual test_...() function or at the end of the file, the mere presence of a single docstring with a doctests pattern causes PyCharm to exhibit this behavior.
If I just change all the >>> ... into />>> ... for example (to cripple the doctests search), then the behavior is as expected for a test file: context-menu on a function offers Debug 'pytest for test_foo.test_function'.
Is there a way to tell PyCharm to prioritize pytest over doctests if both are feasible?

Try Run -> Run ... menu option. The popup should have multiple choices including pytest
Please vote for the corresponding ticket in PyCharm's issue tracker https://youtrack.jetbrains.com/issue/PY-35985

Related

Better python unittest integration?

I'm using GNU Emacs 24.5.1 to work on Python code. I often want to run just a single unit test. I can do this, for example, by running:
test=spi.test_views.IndexViewTest.generate_select2_data_with_embedded_spaces make test
with M-X compile. My life would be simpler if I could give some command like "Run the test where point is", and have emacs figure out the full name of the test for me. Is possible?
Update: with the folowing buffer, I'd like some command which runs M-X compile with:
test=spi.test_views.IndexViewTest.test_unknown_button make test
where spi is the name of the directory test_views.py is in. Well, technically, I need to construct the python path to my test function, but in practice, it'll be <directory.file.class.function>.
This seems like the kind of thing somebody would have already invented, but I don't see anything in the python mode docs.
I believe you use the "default" python mode, while the so-called elpy mode (that I strongly recommend giving a try when doing Python developments within Emacs) seems to provide what you are looking for:
C-c C-t (elpy-test)
Start a test run. This uses the currently configured test runner to discover
and run tests. If point is inside a test case, the test runner will run exactly
that test case. Otherwise, or if a prefix argument is given, it will run all tests.
Extra details
The elpy-test function internally relies on the function (elpy-test-at-point), which appears to be very close to the feature you mentioned in the question.
See e.g. the code/help excerpt in the following screenshot:

pytest run new tests (nearly) first

I am using pytest. I like the way I call pytest (re-try the failed tests first, verbose, grab and show serial output, stop at first failure):
pytest --failed-first -v -s -x
However there is one more thing I want:
I want pytest to run the new tests (ie tests never tested before) immediately after the --failed-first ones. This way, when working with tests that are long to perform, I would get most relevant information as soon as possible.
Any way to do that?
This may not be directly what you are asking about, but, my understanding is that the test execution order is important for you when you create new tests during development.
Since you are already working with these "new" tests, the pytest-ordering plugin might be a good option to consider. It allows you to influence the execution order by decorating your tests with #pytest.mark.first, #pytest.mark.second etc decorators.
pytest-ordering is able to change the execution order by using a pytest_collection_modifyitems hook. There is also pytest-random-order plugin which also uses the same hook to control/change the order.
You can also have your own hook defined and adjusted to your specific needs. For example, here another hook is used to shuffle the tests:
Dynamically control order of tests with pytest
For anyone coming to this now, pytest added a --new-first option to run new tests before all other tests. It can be combined with --failed-first to run new and failed tests. For test-driven development, I've found it helpful to use these options with pytest-watch, which I described in my blog.

Add a pytest option that doesn't have dashes?

I have a lot of tests that take a long time to run. Luckily, the time these tests take is evenly distributed among tests for several subsystems of my project.
I'm using IPython's pytest magic commands. I'd like to be able to just say things like
pytest potato_peeler --donttesti18n --runstresstests
or
pytest garlic_squeezer --donttestsmell --logperfdata
but I can't add the garlic_squeezer and potato_peeler options the same way I do logperfdata et al because parser.addoption gets upset if the option name doesn't start with a --.
I know this seems like a tiny inconvenience, but I have a ton of people running these tests several times a day and I'd like the way they're invoked to make as much sense as possible, by emulating how you issue commands on a command line (command, thing you want to run the command on, then --flags.)
Is there a way have non-dashed options? (that doesn't involve writing a full-blown pytest plugin that overrides the option parsing?)
I was hoping to use the pytest_commandline_parse hook but you can't use that hook in a conftest.py, you have to write a full-blown plugin.
You can mark your tests and run only those with some mark.
For example:
import pytest
#pytest.mark.runstresstests
def test_stress_something():
pass
#pytest.mark.logperfdata
def test_something_quick():
pass
...
If you only want to run stress tests: pytest -m runstresstests
Full documentation at https://docs.pytest.org/en/latest/example/markers.html

Run a specific unit test function inside PyCharm IDE 5.0.4

I am trying to use PyCharm for unit testing (with unittest), and am able to make it work: the test runner nicely shows the list of test cases and nested test functions.
However, once the tests have been discovered, I cannot find any way to (re)run a specific test function: the only button available will run the whole list of tests, and right clicking on a single test function doesn't show any meaningful action for this purpose.
As you can imagine, it can take a long time unnecessarily when the purpose is to debug a single test.
How to achieve this? It is possible in Visual Studio for example, and seems like a basic feature so I assume I must be missing something.
Check the default test framework of the project...
You're perhaps used to 'unittest' being the default. Its enables me to put the cursor on the test definition and hit "SHIFT-CTRL-R" to run that one test.
The default seems to have changed to 'py.test' which has different behaviour and keyboard shortcuts. I'm on OSX so ymmv.
On Linux:
File -> Settings -> Tools -> Python Integrated Tools -> Testing / "Default Test Runner"
On OSX:
Preferences -> Tools -> Python Integrated Tools -> "Default test runner:"
With recent versions of PyCharm the availability of the 'right click' option seems intermittent.
One replacement is to go to Edit Configurations... and type the name of the class and method yourself. That's worked well for me, even if not quite as convenient
Under pycharm 2017.2.3:
the key step:
change the default test runner(unittests) to (nosetests or py.test), both ok.
then the IDE can run single test function now.
follow the steps of the below screenshots.
1. change settings:
2. run single test function:
3. run all test functions:
In Pycharm 2018.1: restart, delete the existing run configrations - suddently right-click provides an option to run a single test. :-/
Have you tried right clicking the test in the actual class? It should be possible to run the single test from there. I'd suggest a re-install if this is not available.
Please check whether you have the same test name repeated in two or more locations in the test fixture. I had the same problem and resolving the naming conflicts enabled me to right click on the test name and run it individually.
I had this problem with PyCharm 2018.3.
It seemed to be because I had a breakpoint in a strange place (at function declaration, instead of inside the function).
Clearing all the breakpoints seemed to restore the ability to debug individual tests

how to omit imports using .coveragerc in coverage.py?

I am using nosetests --with-coverage to test and see code coverage of my unit tests. The class that I test has many external dependencies and I mock all of them in my unit test.
When I run nosetests --with-coverage, it shows a really long list of all the imports (including something I don't even know where it is being used).
I learned that I can use .coveragerc for configuration purposes but it seems like I cannot find a helpful instruction on the web.
My questions are..
1) In which directory do I need to add .coveragerc? How do I specify the directories in .coveragerc? My tests are in a folder called "tests"..
/project_folder
/project_folder/tests
2)It is going to be a pretty long list if I were to add each in omit= ...
What is the best way to only show the class that I am testing with the unittest in the coverage report?
It would be nice if I could get some beginner level code examples for .coveragerc. Thanks.
The simplest way to direct coverage.py's focus is to use the source option, usually source=. to indicate that you only want to measure code in the current working tree.
You can also use the --cover-package=PACKAGE option. For example:
nosetests --with-coverage --cover-package=module_you_are_testing
See http://nose.readthedocs.org/en/latest/plugins/cover.html for details.

Categories