Can't run unit test in Test Runner
已回答
I am new to the unit test framework in pycharm. I created a simple unit test driver that uses "unittest". However, I can't seem to invoke it in the Test Runner utility. When I run it, it simply runs as any other python program would run and outputs information in the "Run" window. I feel like there is something I should do in the "Run/Debug Configuration", but I'm not sure what. What do I need to do to get my unit test to run in Test Runner?
请先登录再写评论。
Hi Gharabedian! Let's say I have a file:
there're few ways to run this file as a test and not as a pure Python script:
Notes:
Has it helped? Please feel free to ask if my description was unclear. We also have Testing topic described in documentation, please take a look: https://www.jetbrains.com/help/pycharm/2017.1/testing.html
Yeah, the instructions were very good. The "Run unittest in..." was not showing up. My assumption is because a configuration already existed. I went back and deleted the configuration. Then I manually created the unit test via Run | Edit Configurations | Add | Python Tests
Worked great!
I'm having a bit of trouble controlling which tests Pycharm runs. My project is structured like this:
If I right-click on "integration" or "unit" and select "Run unit tests...", then everything works fine.
Also, if I right-click on a particular test class name and select "Run unit tests..." everything works fine.
Where I run into trouble is when I click on a particular test method within one of the test classes. If I do that and select "Run unit tests..." then Pycharm runs all the unit tests for that class, not just the one test method I have clicked into. The same thing happens if I click on a method and type ⇧⌃R.
I think the problem is related to how I have Defaults/Python tests/Unittests configured in the Run/Debug Configuration. I have "Working directory" set to the path to my "tests" folder (see hierarchy, above), and "Target" is set to "Path" with no path specified. Is this how it should be set?
If I edit the configuration for testing, let's say, "integration.test_class_name1.test_method", then I can get it to work if I run the configuration direction. But it still does not work if I right click on "test_method".
What am I doing wrong in terms of running an individual unit test method?
Thanks.
Hi Swehba! I believe it fails because of "Working directory" option in defaults. PY-25586 ticket seems to cover this case. If there's no specific reason to have this options set - please remove it.