gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:40:08 25 4
gpt4 key购买 nike

我正在尝试将参数传递给单元测试子类方法。请原谅我的无知 - 我几天前才开始用 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)

最佳答案

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

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

关于尝试将参数传递给unittest子类时出现Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com