我必须测试几个销售相同内容的网站,但他们有另一个模板。
所以我想运行每个MainTestClass并给出一些输入参数,比方说:
java -jar SeleniumServerStandalone-2.0b2.jar -port 5555(template_id = 5)
有可能吗?
class MainTestCases(unittest.TestCase):
def setUp(self):
#self.template_id=template_id I want something like that
self.verificationErrors = []
self.selenium = selenium("localhost", 5555, "*chrome", "http://www.google.com/")
time.sleep(5)
self.selenium.start()
def test_test1(self):
if self.template_id==1:
...
elif self.template_id==2:
...
def test_test2(self):
if self.template_id==1:
...
elif self.template_id==2:
...
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
答案 0 :(得分:1)
尝试向MainTestCases添加 init 方法,如下所示:
class MainTestCases(unittest.TestCase):
def __init__(self, methodName, template_id):
super(MainTestCases, self).__init__(self, methodName)
self.template_id = templateId
def setUp(self):
... and so on...
由于这种自定义,您需要手动构建测试套件,因为每个测试用例都必须使用template_id进行实例化,如此 -
def suite(template_id):
testcasenames = unittest.defaultTestLoader.loadTestsFromTestCase(MainTestCases)
suite = []
for case in testcasename:
suite.append(MainTestCases(case, template_id)
return suite
然后在 main 中,而不是unittest.main(),执行:
答案 1 :(得分:0)
现在,我使用这个解决方案:
导入unittest 来自Flights.FlightsTestCases进口FlightsTestCases 导入系统 来自Flights.FlightTemplate导入FlightTemplate
def suite():
testSuite= unittest.TestSuite()
testSuite.addTest(FlightsTestCases('test_test1'))
FlightsTestCases.www_address='http://testpage.pl/'
FlightsTestCases.flight_template=FlightTemplate.Test
#FlightsTestCases.www_address='http://productionpage.pl/'
#FlightsTestCases.flight_template=FlightTemplate.Production
return testSuite
if __name__ == "__main__":
result = unittest.TextTestRunner(verbosity=2).run(suite())
sys.exit(not result.wasSuccessful())
将set_up更改为:
class FlightsTestCases(unittest.TestCase): www_address =无 flight_template =无 xml_report_generator =无
def setUp(self):
self.verificationErrors = []
if self.www_address == None:
self.selenium = selenium("localhost", 5555, "*chrome", "http://testpage.pl/")
else:
self.selenium = selenium("localhost", 5555, "*chrome", self.www_address)