Gab*_*ada 5 python selenium unit-testing test-suite testcase
我需要制作一个大型的python套件包,其中包括我已经制作的其他手提箱和测试盒.
我该怎么做呢?
例如,这里有一个我要添加的suitecase(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())
Run Code Online (Sandbox Code Playgroud)
和一个测试用例结构(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()
Run Code Online (Sandbox Code Playgroud)
谢谢!
Bog*_*dan 12
您可以提供一些其他信息,例如程序/测试用例和套件的结构.我这样做是为每个模块定义一个套件().所以我对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()
Run Code Online (Sandbox Code Playgroud)
然后我对每个包进行了主要测试:
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)
Run Code Online (Sandbox Code Playgroud)
您可以将recursevly直到项目的根目录.因此,来自包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())
Run Code Online (Sandbox Code Playgroud)
您可以将测试和套件添加到test_suite中.