尝试将参数传递给unittest子类时的Python TypeError

时间:2012-10-14 19:30:34

标签: python unit-testing subclass

我正在尝试将参数传递给unittest子类方法。请原谅我的无知 - 几天前我才开始用Python编写代码。我显然可以硬编码子类本身的参数,但这将消除其与其他用户名/密码组合的重用。当我在下面运行run_tests.py时,我得到错误“TypeError:runTest()正好接受3个参数(给定4个)”。

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

这是testcases / 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()

这是testcases / 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)

1 个答案:

答案 0 :(得分:3)

由于runTest已成为类方法,因此您必须包含self参数:

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