Pycharm and unittest does not work

半城伤御伤魂 提交于 2019-11-30 17:11:26

This is probably because you did not set your testing framework correctly in the settings dialogue.

Although this wasn't the case with the original poster, I'd like to note that another thing that will cause this are test functions that don't begin with the word 'test.'

class TestSet(unittest.TestCase):

    def test_will_work(self):
        pass

    def will_not_work(self):
        pass

Definitely a pycharm thingy, repeating from above,

  • Run --> Edit Configurations.
  • select the instances of the test, and press the red minus button.
MPacho

I have the exact same problem. It turned out that the fact of recognizing an individual test was related to the file name. In my case, test_calculate_kpi.py, which PyCharm didn't recognize as a test when renamed to test_calculate_kpis.py, was immediately recognized.

4 steps to generate html reports with PyCharm and most default test runners (py.test, nosetest, unittest etc.):

  1. make sure you give your test methods a prefix 'test' (as stated before by others), e.g. def test_run1()
  2. the widespread example code from test report package‘s documentation is

    import unittest
    import HtmlTestRunner
    
    class TestGoodnessOfFitTests(unittest.TestCase):
    
     def test_run1(self):
      ...
    
    if __name__ == '__main__':
     unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='t.html'))
    

    This code is usually located in a test class file which contains all the unittests and the "main"-catcher code. This code continuously yielded the warning Empty test suite for me. Therefore, remove all the if __name__ ... code. The file now only contains the TestGoodnessOfFitTests class. Now, additionally

  3. create a new main.py in the same directory of the test class file and use the following code:

     import unittest
     import HtmlTestRunner
     test_class = TestGoodnessOfFitTests()
     unittest.main(module=test_class, 
     testRunner=HtmlTestRunner.HTMLTestRunner(output='t.html'))
    
  4. Remove your old run configurations, right-click on your main.py and press Run 'main'. Verify correct settings under Preferences -> Python Integrated Tools -> Default test runner (in my case py.test and nose worked)

Output:

Running tests... 
----------------------------------------------------------------------
 test_gaussian_dummy_kolmogorov_cdf_1 (tests.evaluation_tests.TestGoodnessOfFitTests) ... OK (1.033877)s

----------------------------------------------------------------------
Ran 1 test in 0:00:01

OK



Generating HTML reports... 

Even I had the same problem, I felt the workspace was not properly refreshed. Even I did File->Synchronize(Ctrl+Aly+y). But that wasn't the solution. I just renamed my test python file name and again I tried executing the code, it started worked fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!