2011-03-14 19 views
1

我必須測試幾個銷售相同的東西的網站,但他們有另一個模板。如何將參數輸入到Selenium RC TestSuite?

所以我想與運行提供了一些輸入參數每個MainTestClass,讓我們說:

Java的罐子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

我還沒有在Python上工作,但可以使用TestNG中提供的數據提供程序類的東西 - htt p://testng.org/doc/documentation-main.html#parameters-dataproviders – Tarun 2011-03-15 07:03:17

回答

0

現在,我用這個解決方案:

  1. 創建它運行測試用例的測試套件:

進口單元測試 從Flights.FlightsTestCases進口FlightsTestCases 進口SYS 從Flights.FlightTemplate進口FlightTemplate

DEF套件():

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類似:

類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) 
+0

這個答案需要格式化,但我找不到如何編輯它:/ – user278618 2011-06-02 14:15:49

1

嘗試添加INIT方法MainTestCases,像這樣:

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,像so--

def suite(template_id): 
    testcasenames = unittest.defaultTestLoader.loadTestsFromTestCase(MainTestCases) 
    suite = [] 
    for case in testcasename: 
     suite.append(MainTestCases(case, template_id) 
    return suite 

然後在,而不是unittest.m ain(),請執行以下操作:

  1. 解析命令行參數。您可能需要考慮argparse(2.7+)或optparse(2.6及更早版本)模塊。它們功能強大,但通過查看示例很容易。
  2. 創建和運行套房:unittest.TextTestRunner()運行(套件(template_id))