1

我正在尝试将参数传递给 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())

这是测试用例/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)
4

1 回答 1

3

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

class Login (BaseTestCase):
    def runTest(self, username, password, verification):
                ^^^^
于 2012-10-14T19:31:56.877 回答