Python Pycharm 和 unittest 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20251386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:01:07  来源:igfitidea点击:

Pycharm and unittest does not work

pythonpycharmpython-unittest

提问by drgn

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

我有 PYCharm 3.0.1 的问题,我无法运行基本的单元测试。

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

这是pycharm给我的所有东西

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)

问题是当我手动运行 python 文件时(使用 pycharm,作为脚本)

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

这是正常的,我故意让测试失败。我对这里发生的事情有点无能为力更多信息:设置->Python 集成工具->包需求文件:/src/test 默认测试运行程序:Unittests pyunit 1.4.1 已安装

Thank you for any kind of help.

感谢您的任何帮助。

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

编辑:同样的事情发生在 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()

采纳答案by Games Brainiac

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

这可能是因为您没有在设置对话框中正确设置测试框架。

enter image description here

在此处输入图片说明

回答by binarysubstrate

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

尽管原始海报的情况并非如此,但我想指出,导致这种情况的另一件事是不以“test”一词开头的测试函数。

class TestSet(unittest.TestCase):

    def test_will_work(self):
        pass

    def will_not_work(self):
        pass

回答by shlomoa

Definitely a pycharm thingy, repeating from above,

绝对是 pycharm 的东西,从上面重复,

  • Run --> Edit Configurations.
  • select the instances of the test, and press the red minus button.
  • 运行 --> 编辑配置。
  • 选择测试实例,然后按红色减号按钮。

回答by Fábio

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

使用 PyCharm 和大多数默认测试运行程序(py.test、nosetest、unittest 等)生成 html 报告的 4 个步骤:

  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 suitefor me. Therefore, remove all the if __name__ ... code. The file now only contains the TestGoodnessOfFitTestsclass. Now, additionally

  3. create a new main.pyin 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)

  1. 确保你给你的测试方法一个前缀“test”(如其他人之前所述),例如def test_run1()
  2. 测试报告包文档中广泛使用的示例代码是

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

    此代码通常位于包含所有单元测试和“主要”捕获器代码的测试类文件中。这段代码不断地Empty test suite为我产生警告。因此,删除所有 if __name__ ... code。该文件现在仅包含TestGoodnessOfFitTests该类。现在,另外

  3. 在测试类文件的同目录下新建一个main.py,使用如下代码:

     import unittest
     import HtmlTestRunner
     test_class = TestGoodnessOfFitTests()
     unittest.main(module=test_class, 
     testRunner=HtmlTestRunner.HTMLTestRunner(output='t.html'))
    
  4. 删除旧的运行配置,右键单击 main.py 并按Run 'main'。验证下的正确设置Preferences -> Python Integrated Tools -> Default test runner(在我的情况下 py.test 和鼻子工作)

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

回答by 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.

我有完全一样的问题。事实证明,识别单个测试的事实与文件名有关。就我而言,test_calculate_kpi.pyPyCharm 在重命名为 时未将其识别为测试的test_calculate_kpis.py,立即被识别。

回答by Kishor kumar R

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.

即使我有同样的问题,我也觉得工作区没有正确刷新。即使我做了文件->同步(Ctrl + Aly + y)。但这不是解决方案。我刚刚重命名了我的测试 python 文件名,然后我再次尝试执行代码,它开始工作正常。

回答by Steve Wehba

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.

我有同样的问题。该文件被命名test_exercise_detectors.py(注意复数“检测器”)并且它在一个名为test_exercise_detectors. 将文件名更改为test_exercise_detector.py(singular "detector") 解决了这个问题。