简体   繁体   English

无法在python中实例化unittest.Testcase的子类

[英]can't instantiate child classes of unittest.Testcase in python

i'm iterating over a text file. 我正在迭代一个文本文件。

each line in the file text file is the name of a test. 文件文本文件中的每一行都是测试的名称。

i am trying to instantiate the test class but i keep getting this error: 我试图实例化测试类,但我不断收到此错误:

ValueError: no such test method in <class 'login_to_blog'>: runTest

the code where i'm doing that is here: 我正在做的代码在这里:

    test_name = line.replace("\n", "") #name of test file, class, and method _must_ be shared.
    module = __import__(test_name)
    test_class = getattr(module, test_name)
    suite.addTest(test_class())

here is login_to_blog: 这是login_to_blog:

from selenium import selenium
import unittest, time, re

class login_to_blog(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://blog/")
        self.selenium.start()

    def test_login_to_blog(self):
        sel = self.selenium
        sel.open("/")
        sel.type("signin_username", "jim")
        sel.type("signin_password", "jones")
        sel.click("//input[@value='Signin']")
        sel.wait_for_page_to_load("30000")
        try: self.failUnless(sel.is_text_present("your blog posts"))
        except AssertionError, e: self.verificationErrors.append(str(e))

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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

it's important to note that these tests run by them selves successfully via command line. 重要的是要注意这些测试由他们自己通过命令行成功运行。

any idea how i can instantiate them and run them manually from within python code? 任何想法我如何实例化它们并从python代码中手动运行它们?

Looking at the PyUnit Suite Documentation , it says: 查看PyUnit Suite文档 ,它说:

When creating an instance we must specify the test method it is to run. 创建实例时,我们必须指定要运行的测试方法。 We do this by passing the method name in the constructor: 我们通过在构造函数中传递方法名称来完成此操作:

    defaultSizeTestCase = WidgetTestCase("testDefaultSize")
    resizeTestCase = WidgetTestCase("testResize")

Further down, I think the thing you're looking for is: 再往下,我觉得你要找的东西是:

Since it is a common pattern to create a TestCase subclass with many similarly named test functions, there is a convenience function called makeSuite provided in the unittest module that constructs a test suite that comprises all of the test cases in a test case class:- 由于创建具有许多类似命名的测试函数的TestCase子类是一种常见模式,因此在unittest模块中提供了一个名为makeSuite的便捷函数,它构造了一个包含测试用例类中所有测试用例的测试套件: -

   suite = unittest.makeSuite(WidgetTestCase,'test')

So you want: 所以你要:

suite = unittest.makeSuite(test_class, 'test')
result = unittest.TestResult()
suite.run(result)

or something like that. 或类似的东西。

Many people expect that they can create a testsuite and that they can add a full class based on unittest.TestCase and it will automatically run all "test*"-functions in it. 许多人希望他们可以创建一个测试套件,他们可以添加一个基于unittest.TestCase的完整类,它将自动运行所有“test *” - 函数。 That's because unittest.main() will do so. 那是因为unittest.main()会这样做。 In reality however, each TestCase-class will only call one single method - have a look at the source code for unittest.TestCase at lib/python/unittest/case.py 但实际上,每个TestCase类只调用一个方法 - 查看lib / python / unittest / case.py中unittest.TestCase的源代码。

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

and that's where the error comes from as the base class TestCase does not provide a default implementation of "def runTest". 而这就是错误来自于基类TestCase不提供“def runTest”的默认实现。 If you want to mimic the behaviour of unitest.main then you need to create one instance of your test class FOR EACH method that you want to get executed 如果你想模仿unitest.main的行为,那么你需要创建一个你希望执行的测试类FOR EACH方法的一个实例

test_name = line.replace("\n", "") #name of test file, class, and method _must_ be shared.
module = __import__(test_name)
test_class = getattr(module, test_name)
for method in dir(test_class):
    if method.startswith("test"):
        suite.addTest(test_class(method))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM