带有测试套件和测试用例的 TestSuite

发布于 2024-11-28 18:23:19 字数 1172 浏览 2 评论 0原文

我需要制作一个大的 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()

谢谢!

I need to make a big python suitecase consisted of other suitcases and testcase which I have already made to execute together.

How do I do this?

For example, here there is a suitecase (suiteFilter.py) which I want to add:

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())

And a testcase structure (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()

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-12-05 18:23:19

您可以提供一些附加信息,例如程序/测试用例和套件的结构。我的方法是为每个模块定义一个 suite() 。所以我对 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 中。

You could give some additional information like the structure of your program / test cases and suites. The way I do it is define a suite() for each module. So I have say for UserServiceTest module:

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()   

Then I have a main test for each package:

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)

You can do this the recursevly until the root of your project. So main test from package A will gather all module in package A + main test from subpackages of package A and so on. I was assuming you-re using unittest since you didn't give any additional details but I think this structure can be applied to other python testing frameworks as well.


Edit: Well I'm not quite sure I fully understand your problem, but from what I can understand you want to add both the suite defined in suiteFilter.py and the testcase defined in Invoice.py in the same suite? If so why not just do in a mainTest.py for example:

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())

You can add tests and suites all the same to a test_suite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文