2012-10-14 49 views
1

我試圖將參數傳遞給unittest子類方法。請原諒我的無知 - 我幾天前纔開始使用Python進行編碼。我可以顯然只是硬編碼子類本身的參數,但這會消除與其他用戶名/密碼組合的重用。當我運行下面的run_tests.py時,我得到錯誤「TypeError:runTest()只需要3個參數(4給定)」。嘗試將參數傳遞給unittest子類時的Python TypeError

這裏是run_tests.py:

from selenium import webdriver 
import unittest 
from testcases import login 


def my_suite(): 

    suite = unittest.TestSuite() 
    suite.addTest (login.Login().runTest("username1", "password1", "page title")) 
    return suite 


if __name__ == '__main__': 
    runner = unittest.TextTestRunner() 
    runner.run(my_suite()) 

在這裏被測試用例/ basetestcase.py:

from selenium import webdriver 
import unittest 

class BaseTestCase (unittest.TestCase): 

def setUp(self): 
    self.driver = webdriver.Firefox() 

    self.driver.implicitly_wait(30) 
    self.base_url = "http://website" 

def tearDown(self): 
    self.driver.quit() 

在這裏被測試用例/ login.py

import common_page_elements 
from basetestcase import BaseTestCase 

class Login (BaseTestCase): 
    def runTest(username, password, verification): 
     """ Test logging in. """ 
     driver = self.driver 
     driver.get(self.base_url) 
     driver.find_element_by_id(common_page_elements.textfield_username).clear() 
     driver.find_element_by_id(common_page_elements.textfield_username).send_keys(username) 
     driver.find_element_by_id(common_page_elements.textfield_password).clear() 
     driver.find_element_by_id(common_page_elements.textfield_password).send_keys(password) 
     driver.find_element_by_name(common_page_elements.button_submit).click()   

     self.assertTrue(verification in self.driver.title) 

回答

3

由於runTest已成爲一個類的方法,你將不得不包括self參數:

class Login (BaseTestCase): 
    def runTest(self, username, password, verification): 
       ^^^^