【问题标题】:How to: setting a testsuite in python如何:在 python 中设置测试套件
【发布时间】:2023-04-09 14:45:01
【问题描述】:

我知道这是一个有点傻的问题,但是使用下面提供的链接,我仍然无法创建测试套件。

我现在有两个测试用例(还会有更多),假设名称有:

class step1(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_case1(self):
[...]

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

和:

class step2(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_case2(self):
[...]

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

我要创建其他文件.py文件:testsuite,可以聚合test_case1、test_case2、test_case3...

我尝试过类似的方法,例如:

import unittest
import step1
import step2


def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.step1(test_case1))
    test_suite.addTest(unittest.step2(test_case2))

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

错误:AttributeError:'module'对象没有属性'step1'

【问题讨论】:

    标签: python unit-testing testing python-unittest


    【解决方案1】:

    您可以使用addTest() 并将TestCase 实例传递给它,您也会错过return 语句:

    def suite():
        test_suite = unittest.TestSuite()
        test_suite.addTest(step1())
        test_suite.addTest(step2())
        return test_suite
    

    或者,在一行中使用addTests()

    test_suite.addTests([step1(), step2()])
    

    【讨论】:

    • Probalby 它更好,但仍然得到: AttributeError: 'module' object has no attribute 'addTest'
    • @ti01878 哎呀,当然,修正了错误。谢谢。请注意,代码假定step1step2 是测试用例。
    • 好吧,我的代码太糟糕了,我必须再做两件事:1) stackoverflow.com/questions/19087189/… 和 2) stackoverflow.com/questions/18928826/…,但最终它可以工作 :) 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多