【发布时间】:2018-05-06 19:23:27
【问题描述】:
我是第一次使用 unittest 和 selenium,但这也是我在 python 中的第一个更大的代码。我为这个问题找到了一些答案,但没有解释具有 __ init __ 继承和方法 super().__ init __() 的类
TL;DR 我有一个一个继承的类。创建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()
main_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 - 下一层,继承所有类的类,其测试与前一个类似,并使用它们的方法运行所有测试但不作为测试_
class StaticDisplay(HeaderStaticTest, HorizontalStaticTest, VerticalStaticTest):
def static_display(self):
self.header_static()
self.horizontal_static()
self.vertical_static()
test_about_us.py - 下一层,一个普通的单元测试测试用例,它只继承前一个测试用例,但通常它继承了我编写的所有以前的类,现在我可以测试页面上的所有“静态视图”当我点击按钮时不会改变
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()
(最后) main_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 __() 接受 1 个位置参数,但给出了 2 个 上面的完整错误消息,可能需要它
有人可以帮帮我吗?
【问题讨论】:
标签: python unit-testing selenium inheritance super