Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm and unittest won't run

Tags:

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()
like image 534
drgn Avatar asked Nov 27 '13 19:11

drgn


People also ask

How do I run unittest in PyCharm?

If no specific test runner is installed, PyCharm uses unittest. To explicitly set the required test runner in the project settings, press Ctrl+Alt+S to open the IDE settings and select Tools | Python Integrated Tools, and then select the target test runner from the Default test runner list.

How do I run unittest tests?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

How do I run BDD test cases in PyCharm?

The procedure of running tests is the same as for the other testing frameworks: Open the desired feature feature in the editor, or select it in the Project tool window. Do one of the following: Right-click the selected file or folder, and choose Run <feature file name> on the context menu of the selection.


4 Answers

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 
like image 69
binarysubstrate Avatar answered Oct 23 '22 11:10

binarysubstrate


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

enter image description here

like image 35
Games Brainiac Avatar answered Oct 23 '22 11:10

Games Brainiac


Definitely a pycharm thingy, repeating from above,

  • Run --> Edit Configurations.
  • select the instances of the test, and press the red minus button.
like image 26
shlomoa Avatar answered Oct 23 '22 12:10

shlomoa


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.

like image 27
MPacho Avatar answered Oct 23 '22 11:10

MPacho