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?

0

Hi Gharabedian! Let's say I have a file:

# test_sample.py
import unittest


class TestTrue(unittest.TestCase):
def test_true(self):
self.assertTrue(True)

there're few ways to run this file as a test and not as a pure Python script:

  1. Right click on the test_sample.py in a project tree and select "Run unittest in ...". This will create a special Test Run Configuration for this file and execute it.
  2. Right click inside a file or on a test class or on a test method and pick "Run unittest in ...". This will also create a Test Run Configuration according to the context. E.g. if you right click on the test method PyCharm will run only it.
  3. Manually create a Test Run Configuration with Run | Edit Configurations | Add | Python tests there you can select different frameworks and specify the target e.g. file or folder, additional parameters and so on.

Notes:

  • Make sure there's no existing Run Configuration for the file before you try to right click and pick "Run unittest in ..." - if such configuration exists PyCharm will not suggest to "Run unittest in ..." but will select an existed Run Configuration instead.
  • Make sure your test file name starts with test_.

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

2
Avatar
Permanently deleted user

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!

0
Avatar
Permanently deleted user

I'm having a bit of trouble controlling which tests Pycharm runs. My project is structured like this:

    project
        tests
            integration
                test_class_name1.py
                test_class_name2.py
            unit
                test_class_name1.py
                test_class_name2.py

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.

1

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.

0

请先登录再写评论。