Adr*_*eja 1 python inheritance selenium unit-testing super
我第一次使用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 :(
Run Code Online (Sandbox Code Playgroud)
所有:
完整错误讯息:
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
Run Code Online (Sandbox Code Playgroud)
那就是我的代码:
我有一个settings.py文件,其中包含由settings.xyz [“ layer1”] [“ KEY”]访问的字典中的变量
setup.py-硒的安装类
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()
Run Code Online (Sandbox Code Playgroud)
main_tests_config.py-下一步-现在非常基本的配置
class MainTestConfig(StartInstance):
def __init__(self):
super().__init__()
self.language = settings.TEST_LANGUAGE
self.currency = settings.TEST_CURRENCY
Run Code Online (Sandbox Code Playgroud)
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"]
Run Code Online (Sandbox Code Playgroud)
header_tests.py-下一层,继承变量(HeadetText,HeaderPath,Urls(类Urls,它是具有页面url变量的类)),语言(来自MainTestConfig)和硒驱动程序(来自第一类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
Run Code Online (Sandbox Code Playgroud)
static_display.py-下一层,该类继承具有上一类测试的所有类,并使用其方法运行所有测试,但不作为test_
class StaticDisplay(HeaderStaticTest, HorizontalStaticTest, VerticalStaticTest):
def static_display(self):
self.header_static()
self.horizontal_static()
self.vertical_static()
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
(最终) 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())
Run Code Online (Sandbox Code Playgroud)
正如我说的那样,新类中的这个新def __ init __和super().__ init __()在某处有问题……我在哪里弄错?
当我开始这个测试用例时,我得到一个错误:
TypeError:__ init __()接受1个位置参数,但上面给出了2个完整的错误消息,可能是需要的
有谁可以帮助我吗?
TestCase实例带有一个可选的关键字参数methodName; 我猜想unittest模块会在某个时候将其显式传递给幕后。通常,当我对类进行子类化时,我不会自己使用这种模式。这应该可以解决您的问题:
class SubClass(SupClass):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
特别是当您不向__init__方法传递任何参数时,以这种方式传递参数是避免收到错误的好方法。如果确实要将自定义内容传递给您的__init__方法,则可以执行以下操作:
class SubClass(SupClass):
def __init__(self, myarg, *args, **kwargs):
super().__init__(*args, **kwargs)
# do something with your custom argument
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |