问题
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.
回答1:
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.
Buried at:
Preferences -> Tools -> Python Integrated Tools -> "Default test runner:"
回答2:
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
回答3:
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.
回答4:
In Pycharm 2018.1: restart, delete the existing run configrations - suddently right-click provides an option to run a single test. :-/
回答5:
Under pycharm 2017.2.3:
the key step:
- change the
default test runner(unittests)
to (nosetests
orpy.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:
回答6:
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.
来源:https://stackoverflow.com/questions/36079152/run-a-specific-unit-test-function-inside-pycharm-ide-5-0-4