Python 单元测试 : testcase class with own constructor fails in standard library

标签 python unit-testing

<分区>

我有这个普通的普通单元测试,它按预期工作,只要我省略了构造函数。

import sys
import unittest

class Instance_test(unittest.TestCase):

  def __init__(self):
     super(Instance_test, self).__init__()
     self.attribute = "new"

  def test_something(self):
     pass

  def test_other(self):
     self.assertTrue(True)
     pass

  def setUp(self):
     pass

  def tearDown(self):
     pass

def suite():
  return unittest.makeSuite(Instance_test, "test")

def main():
  runner = unittest.TextTestRunner(sys.stdout)
  runner.run(suite())

if __name__ == "__main__":
   main()

在构造函数就位的情况下获取此回溯:


 Traceback (most recent call last):
   File "f:\gt\check.py", line 31, in  main()
   File "f:\gt\check.py", line 28, in main
      runner.run(suite())
   File "f:\gt\check.py", line 24, in suite
    return unittest.makeSuite(Instance_test, "test")
  File "C:\Python34\lib\unittest\loader.py", line 374, in makeSuite
    testCaseClass)
  File "C:\Python34\lib\unittest\loader.py", line 70, in loadTestsFromTestCase
    loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
  File "C:\Python34\lib\unittest\suite.py", line 24, in __init__
    self.addTests(tests)
  File "C:\Python34\lib\unittest\suite.py", line 60, in addTests
    for test in tests:
 TypeError: __init__() takes 1 positional argument but 2 were given

有什么问题吗?我还有什么办法让不同的 test_xxx 方法共享一个中心属性?

最佳答案

我会使用 unittest.TestCase 的 setUp() 和 tearDown() 方法来代替 init。除了使用 setUp 方法外,只需执行与您正在做的相同的事情。

关于Python 单元测试 : testcase class with own constructor fails in standard library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229770/

相关文章:

python - 使用 python 从多个目录复制特定文件

python - 如何在图像中检测到的 Blob 周围画一个红色圆圈?

Android:在仪器测试用例中创建目录

c++ - 如何将一个 exe 项目链接到另一个 exe 项目中的类

spring-boot - 单元测试 Spring Boot 应用服务层

python - 使用路径数组显示图像对

Java 等价于 Python 字典

python - 如何在不更改对象的情况下缩放 tkinter Canvas 窗口

c++ - Google Mock 使用 ON_CALL 返回一个实时元素

c# - 单元测试的值(value)