Pycharm and unittest does not work

你离开我真会死。 提交于 2019-11-30 00:38:27

问题


I have a problem with PYCharm 3.0.1 I can't run basic unittests.

Here is my code :

import unittest from MysqlServer import MysqlServer


class MysqlServerTest(unittest.TestCase):


    def setUp(self):
        self.mysqlServer = MysqlServer("ip", "username", "password", "db", port)


    def test_canConnect(self):
        self.mysqlServer.connect()
        self.fail()


if __name__ == '__main__':
    unittest.main()

Here is All the stuff pycharm give me

Unable to attach test reporter to test framework or test framework quit unexpectedly

Is also says

AttributeError: class TestLoader has no attribute '__init__'

And the event log :

2:14:28 PM Empty test suite

The problem is when i run manually the python file (with pycharm, as a script)

----------------------------------------------------------------------
Ran 1 tests in 0.019s

FAILED (failures=1)

Which is normal i make the test fail on purpose. I am a bit clueless on what is going on here more information : Setting->Python INtegrated Tools->Package requirements file: /src/test Default test runner: Unittests pyunit 1.4.1 Is installed

Thank you for any kind of help.

EDIT: Same thing happen with the basic usage from unitests.py

import unittest


class IntegerArithmenticTestCase(unittest.TestCase):
def testAdd(self):  ## test method names begin 'test*'
    self.assertEquals((1 + 2), 3)
    self.assertEquals(0 + 1, 1)

def testMultiply(self):
    self.assertEquals((0 * 10), 0)
    self.assertEquals((5 * 8), 40)


if __name__ == '__main__':
    unittest.main()

回答1:


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




回答2:


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



回答3:


Definitely a pycharm thingy, repeating from above,

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



回答4:


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.




回答5:


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... 



回答6:


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.




回答7:


I had the same problem. The file was named test_exercise_detectors.py (note the plural "detectors") and it was in a packed named test_exercise_detectors. Changing the name of the file to test_exercise_detector.py (singular "detector") fixed the problem.



来源:https://stackoverflow.com/questions/20251386/pycharm-and-unittest-does-not-work

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