【问题标题】:How to repeat running tests in python selenium using unittest?如何使用unittest在python selenium中重复运行测试?
【发布时间】:2018-01-23 16:12:21
【问题描述】:

下面显示了我的测试类。当我调用它一次时,这是一个很好的测试 - 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()

【问题讨论】:

  • 你能澄清你在问什么吗?

标签: python selenium automated-tests pytest python-unittest


【解决方案1】:

如果您想反复运行测试,我建议您使用 ddt 或数据驱动测试。您可以找到完整使用文档here

您将使用 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 文件中有不同的测试参数,我的测试方法针对文件中的每个参数运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2021-09-20
    • 2020-12-03
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多