python - Unittest + Selenium - __init__() 采用 1 个位置参数,但给出了 2 个

标签 python unit-testing selenium inheritance super

我是第一次使用 unittest 和 selenium,但这也是我在 python 中的第一个更大的代码。我找到了这个问题的一些答案,但是对于具有 __ init __ 继承和方法 super().__ init __() 的类没有任何解释

长话短说 我有一个接一个继承的类。创建 chrome 实例的第一个类 StartInstance 继承自 unittest.TestCase,问题在于继承和 super().init() 在其他类中,因为当我删除它时测试正常开始

一切看起来像:

class StartInstance(unittest.TestCase):
    @classmethod
    def setUpClass(cls): pass

class A(StartInstance):
    def __init__(self):
        super().__init__() adding variables etc to init

class B(A):
    def __init__(self): 
        super().__init__() adding variables etc to init

class C(A):
    def __init__(self):
        super().__init__() adding variables etc to init

class PrepareTests(B, C, D):
    def all tests(self):
        self.tests_B
        self.tests_C
        self.tests_D

 class Tests(PrepareTests):
    def test_click:
        click()
        all_tests()


 #and finally somewhere a test runner
 suite = loader.loadTestsFromTestCase(Tests)
 runner.run(suite())

#when i run this i get this error and it only raises when i 
add classes with their own init
#heh TL;DR almost as long as normal text sorry :(

全部:

完整错误信息:

Traceback (most recent call last):
 File "xyz\main_test.py", line 24, 
 in <module>
   runner.run(suite())
  File "xyz\main_test.py", line 11, 
in suite
   about_us = loader.loadTestsFromTestCase(AboutUs)
 File 
"xyz\Python36\lib\unittest\loader.py", line 92, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
 File "xyz\Python36\lib\unittest\suite.py", line 24, in __init__
  self.addTests(tests)
 File "xyz\Python36\lib\unittest\suite.py", line 57, in addTests
   for test in tests:
 TypeError: __init__() takes 1 positional argument but 2 were given

那是我的代码:

我有一个 settings.py 文件,其中包含 settings.xyz["layer1"]["KEY"] 访问的字典中的变量

setup.py - selenium 的设置类

class StartInstance(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.get(settings.URLS['MAIN_URL'])
        cls.driver.implicitly_wait(2)
        cls.driver.maximize_window()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

ma​​in_tests_config.py - 下一层 - 现在是非常基本的配置

class MainTestConfig(StartInstance):
    def __init__(self):
        super().__init__()
        self.language = settings.TEST_LANGUAGE
        self.currency = settings.TEST_CURRENCY

header.py(还有几个这样的文件)- 下一层将代码从 config.py 翻译成类变量,继承自前面的类,因为它需要全局语言

class HeaderPath(MainTestConfig):
    def __init__(self):
        super().__init__()
        self.logo_path = settings.PAGE_PATHS["HEADER"]["LOGO"]
        self.business_path = settings.PAGE_PATHS["HEADER"]["BUSINESS"]

class HeaderText(MainTestConfig):
    def __init__(self):
        super().__init__()
        self.business_text = settings.PAGE_CONTENT[self.language]["HEADER"]["BUSINESS"]
        self.cart_text = settings.PAGE_CONTENT[self.language]["HEADER"]["CART"]

header_tests.py - 下一层,继承变量(HeadetText、HeaderPath、Urls(类 Urls,它是带有页面 url 变量的类))、语言(来自 MainTestConfig)和 selenium 驱动程序(来自第一个StartInstance 类),类的示例构建

class HeaderStaticTest(HeaderText, HeaderPath, Urls):
    def header_static(self):
        self.logo_display()
        self.method2()
        # etc..

    def logo_display(self):
        self.driver.find_element_by_xpath(self.logo_path)
    def self.method2(self):
        pass

static_display.py - 下一层,继承所有类的类,如前一个类,并使用其方法运行所有测试但不作为 test_

class StaticDisplay(HeaderStaticTest, HorizontalStaticTest, VerticalStaticTest):
    def static_display(self):
        self.header_static()
        self.horizontal_static()
        self.vertical_static()

test_about_us.py - 下一层,一个普通的 unittest 测试用例,它只继承前一个,但通常它继承我写的所有 prevous 类,现在我可以测试页面上的所有“静态 View ”当我点击按钮时不会改变

class AboutUs(StaticDisplay):
    def test_horizontal_menu_click(self):
        about_us_element = self.driver.find_element_by_id(self.hor_about_path)
        about_us_element.click()
        self.assertIn(
            self.about_url,
            self.driver.current_url
        )

    def test_check_static_after_horizontal(self):
        self.static_display()

(最后) ma​​in_cases.py - 出现此错误的运行程序,它仅在我使用自己的 init 添加类时出现...不知道如何修复它...请帮忙

def suite():
    loader = unittest.TestLoader()
    about_us = loader.loadTestsFromTestCase(AboutUs)
    return unittest.TestSuite([about_us])

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

正如我所说,新类中的这个新 def __ init __ 和 super().__ init __() 存在问题……我在哪里犯了错误?

当我开始这个测试用例时,我得到一个错误:

TypeError: __ init __() takes 1 positional argument but 2 were given 上面的完整错误消息,可能是需要的

有人可以帮帮我吗?

最佳答案

TestCase 实例采用可选的关键字参数 methodName;我猜 unittest 模块会在某个时刻在幕后显式地传递它。通常,当我对自己没有创建的类进行子类化时,我会使用这种模式;这应该可以解决您的问题:

class SubClass(SupClass):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

特别是当您没有将任何参数传递给您的 __init__ 方法时,以这种方式传递参数是避免出现错误的好方法。如果你确实想将一些自定义的东西传递给你的 __init__ 方法,你可以这样做:

class SubClass(SupClass):
    def __init__(self, myarg, *args, **kwargs):
       super().__init__(*args, **kwargs)
       # do something with your custom argument

关于python - Unittest + Selenium - __init__() 采用 1 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47445137/

相关文章:

unit-testing - 根据规范运行单个测试

unit-testing - Grails String.encodeAsBase64() 在 Spock 测试中失败

c# - Selenium 与 PhantomJs 等到页面完全加载?

python - 从 MediaWiki 的 API 维基文本中提取 Python 中的模板参数

python - Openpyxl load_workbook 和保存时间太长

python - 如何在 matplotlib 中的 Pandas 条形图上添加一条线?

python - 使用 Mock 在 Python 单元测试中模拟函数内部的函数

python - argparse - 为什么代码在没有被调用的情况下被执行?

java - Selenium:如何提示用户输入并使用输入值?

testing - 我如何以编程方式控制 Selenium RC 的启动和停止