python - 值错误 : no such test method in <class 'myapp.tests.SessionTestCase' >: runTest

标签 python unit-testing

我有一个测试用例:

class LoginTestCase(unittest.TestCase):
    ...

我想在不同的测试用例中使用它:

class EditProfileTestCase(unittest.TestCase):
  def __init__(self):
    self.t = LoginTestCase()
    self.t.login()

这引发了:

ValueError: no such test method in <class 'LoginTest: runTest`

我查看了调用异常的单元测试代码,看起来测试不应该以这种方式编写。有没有一种标准的方法来编写你想要测试的东西,以便以后的测试可以重用它?或者有什么解决方法?

我现在向 LoginTest 添加了一个空的 runTest 方法,作为一种可疑的解决方法。

最佳答案

与“runTest”的混淆主要是基于这样的事实:

class MyTest(unittest.TestCase):
    def test_001(self):
        print "ok"

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

因此,该类中没有“runTest”,并且正在调用所有测试函数。但是,如果您查看基类“TestCase”(lib/python/unittest/case.py),您会发现它有一个默认为“runTest”的参数“methodName”,但它没有“def runTest"

class TestCase:
    def __init__(self, methodName='runTest'):

unittest.main 工作正常的原因在于它不需要“runTest”——您可以通过为子类中的所有方法创建一个 TestCase 子类实例来模仿该行为——只需提供名称作为第一个参数:

class MyTest(unittest.TestCase):
    def test_001(self):
        print "ok"

if __name__ == "__main__":
    suite = unittest.TestSuite()
    for method in dir(MyTest):
       if method.startswith("test"):
          suite.addTest(MyTest(method))
    unittest.TextTestRunner().run(suite)

关于python - 值错误 : no such test method in <class 'myapp.tests.SessionTestCase' >: runTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090479/

相关文章:

r - devtools::test()有效,但devtools::check()无效。为什么?

python - 从复杂对象覆盖 __init__

python - 无法从 python-ldap 连接到 Windows Server 2016 上的 ldaps

angular - 如何在 Angular 中测试来自 RXJS 的 map 和水龙头管道

node.js - MakeFile 使用 NPM 运行 Mocha 测试

visual-studio-2008 - 在VS2008中,有什么方法可以将单元测试与集成测试分开吗?

python - 使用 python 编译 caffe 的问题,对 `std::__cxx11::....' 的 undefined reference

Python:如何跨模块使用变量

Python Pandas DataFrame 根据周一至周日的每周定义将每日数据重新采样为一周?

unit-testing - 有没有获得 Apex 代码的代码覆盖率报告的好方法?