【问题标题】:TestSuite with testsuites and testcases带有测试套件和测试用例的 TestSuite
【发布时间】:2011-10-23 01:12:14
【问题描述】:

我需要制作一个包含其他手提箱和测试用例的大型 python 套件,我已经将它们一起执行。

我该怎么做?

例如,这里有一个我要添加的套件(suiteFilter.py):

import testFilter1
import testFilter2
import unittest
import sys

def suite():
    return unittest.TestSuite((\
        unittest.makeSuite(testFilter1.TestFilter1),
        unittest.makeSuite(testFilter2.TestFilter2),
        ))


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

还有一个测试用例结构(Invoice.py):

from selenium import selenium
import unittest, time, re
from setup_tests import filename, fileForNrTest, username, password, server_url
fileW=open(filename,'a')


class TestInvoice(unittest.TestCase):

    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", server_url)
        self.selenium.start()

    def test_invoice(self):
        sel = self.selenium
        [...] 

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


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

谢谢!

【问题讨论】:

    标签: python unit-testing selenium testcase test-suite


    【解决方案1】:

    您可以提供一些额外的信息,例如您的程序/测试用例和套件的结构。我这样做的方式是为每个模块定义一个套件()。所以我对 UserServiceTest 模块说:

    def suite():
        """
            Gather all the tests from this module in a test suite.
        """
        test_suite = unittest.TestSuite()
        test_suite.addTest(unittest.makeSuite(UserServiceTest))
        return test_suite
    
    if __name__ == "__main__":
        #So you can run tests from this module individually.
        unittest.main()   
    

    然后我对每个包都有一个主测试:

    def suite():
    """
        Gather all the tests from this package in a test suite.
    """
        test_suite = unittest.TestSuite()
        test_suite.addTest(file_tests_main.suite())
        test_suite.addTest(userservice_test.suite())
        return test_suite
    
    
    if __name__ == "__main__":
        #So you can run tests from this package individually.
        TEST_RUNNER = unittest.TextTestRunner()
        TEST_SUITE = suite()
        TEST_RUNNER.run(TEST_SUITE)
    

    您可以递归地执行此操作,直到项目的根目录。因此,包 A 的主测试将收集包 A 中的所有模块 + 包 A 的子包的主测试,依此类推。我假设你正在使用unittest,因为你没有提供任何额外的细节,但我认为这个结构也可以应用于其他 python 测试框架。


    编辑:嗯,我不太确定我完全理解你的问题,但据我所知,你想在同一个套件中添加在 suiteFilter.py 中定义的套件和在 Invoice.py 中定义的测试用例?如果是这样,为什么不直接在 mainTest.py 中进行操作:

    import unittest
    import suiteFilter
    import Invoice
    
    
    def suite()
        test_suite = unittest.TestSuite()
        test_suite.addTest(suiteFilter.suite())
        test_suite.addTest(unittest.makeSuite(Invoice))
    
    
    if __name__ == "__main__":
        result = unittest.TextTestRunner(verbosity=2).run(suite())
        sys.exit(not result.wasSuccessful())
    

    您可以将测试和套件都添加到 test_suite。

    【讨论】:

    • 谢谢,我已经提供了更多的信息
    • 非常感谢!你解决了我的问题!也许你可以帮我做另一件事:如果里面的一个套件或测试用例失败,我想完成这个大套件。目前,如果其中一个失败,则大套件继续下一个手提箱/测试箱。
    • unittest 有一个 -f 选项 (fail_fast) 选项,因此运行: python -m unittest -f mainTest.suite 来自上述 mainTest 类应该这样做。我不知道任何其他直接的选择。
    • @Bogdan 在这些测试中,我们将浏览器硬编码到 firefox,有没有办法 TestSuite 可以从命令行获取参数然后发送到所有 TestCase,以便我们可以在所需的浏览器上运行测试?你能看看这个问题,我真的很努力吗? stackoverflow.com/questions/37674801/…
    • @GabrielQuesada 如果你做到了,你能帮我吗?请看看这个问题stackoverflow.com/questions/37674801/…
    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2018-04-07
    相关资源
    最近更新 更多