24

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

It 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: <PROJECT_HOME>/src/test
  • Default test runner: Unittests
  • pyunit 1.4.1 Is installed

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()
bad_coder
  • 11,289
  • 20
  • 44
  • 72
drgn
  • 1,080
  • 1
  • 11
  • 21

8 Answers8

71

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
Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
18

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

enter image description here

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • 2
    After a JVM change,(Open to Sun) A couple reboot, and the usage of py.test instead of unittests (that still not work). I can run my test inside pycharm with the py.test framework. Also look like i had some deprecated package i had to update via the setuptools. – drgn Nov 28 '13 at 15:50
  • 6
    I am seeing what I think is the same base problem on pycharm 3.x. On the previous versions of pycharm my `if __name__ == '__main__'` block was executed. Now it is not. This messes up some tests when run under the debugger. –  Jan 11 '14 at 00:24
5

Definitely a pycharm thingy, repeating from above,

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

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.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
MPacho
  • 51
  • 1
  • 3
1

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... 
whiletrue
  • 10,500
  • 6
  • 27
  • 47
0

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.

Kishor kumar R
  • 195
  • 2
  • 17
0

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.

Steve Wehba
  • 161
  • 1
  • 6
0

Adding

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

fixed the issue for me.

AccessToken
  • 224
  • 4
  • 8