繁体   English   中英

如何使用unittest在python硒中重复运行测试?

[英]How to repeat running tests in python selenium using unittest?

下面显示了我的测试课程。 当我一次调用它python3 main.py时,这是一个很好的测试。 如果我希望此测试运行100次,则会出现问题。 怎么做? 当我尝试通过pytest打电话时,有这样的警告:

    main.py::TestLoginPage::test_login_invalid_credentials
  /home/-----/.local/lib/python3.5/site-packages/pytest_repeat.py:31: UserWarning: Repeating unittest class tests not supported
    "Repeating unittest class tests not supported")

main.py::TestLoginPage::test_login_valid_credentials
  /home/-----/.local/lib/python3.5/site-packages/pytest_repeat.py:31: UserWarning: Repeating unittest class tests not supported
    "Repeating unittest class tests not supported")

这是我的测试课程-test_login_page.py

import unittest
from selenium import webdriver
from tests.test_driver import TestDriver
from pages.login_page import LoginPage

class TestLoginPage(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.page_url = LoginPage.login_url
        cls.webdriver = webdriver
        TestDriver.setUp(cls, cls.page_url)
        cls.page = LoginPage(cls.webdriver)
        cls.option = 1

    def __init_test_method(self):
        # [TS001].Check if page is loaded correctly
        self.assertTrue(self.page.check_page_loaded_correctly)
        # [TS002].Check button contains 'login' text
        self.assertEqual(self.page.get_button_text(), 'Login')

    # TC001
    def test_login_valid_credentials(self):
        """Test login with valid credentials"""
        self.__init_test_method()

        # [TS003].Clear form, fill with correct data and submit
        self.assertEqual(self.page.login_process(self.option), 'Complexes')

    # TC002 (invalid password) & TC003 (invalid username)
    def test_login_invalid_credentials(self):
        """Test login with invalid credentials"""
        for option in range(2, 4):
            self.__init_test_method()
            self.assertTrue(self.page.login_process(option))

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

这是主要的,我从控制台运行所有测试-main.py

    import unittest
    import os
    import sys
    cwd = os.getcwd()
    sys.path.append(cwd)
    from tests.test_login_page import TestLoginPage

    if __name__ == '__main__':

        unittest.main()

如果要反复运行测试,建议您使用ddt或数据驱动的测试。 您可以在此处找到完整使用的文档。

您将使用ddt装饰测试类,并使用file_data或data装饰测试方法以传递唯一的测试参数。

这样,您就可以为每次测试的迭代提供新的测试参数,并且可以循环多次并运行相同的测试方法,无论您需要多少次。

这是我如何使用它的一个例子:

from ddt import ddt, file_data

@ddt
class MyTestClass(unittest.TestCase):

    @file_data('test_parameter_file.json')
    def some_test_method1(self, test_package):
        Some test method

我在test_parameter_file.json文件中具有不同的测试参数,并且我的测试方法针对文件中的每个参数运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM