【问题标题】:How to apply parameterization for whole test suite rather than using it for individual single test case in pytest Selenium如何为整个测试套件应用参数化,而不是将其用于 pytest Selenium 中的单个测试用例
【发布时间】:2021-11-27 14:33:40
【问题描述】:

有人可以帮我解决这个 Selenium Pytest 框架问题吗?我试着用谷歌搜索它。但没有任何帮助。为长长的问题道歉。想确保我能清楚地描述问题。

我在下面的字典“my_test_data”中有员工测试数据。以下是我想要在单次运行中为每个员工实现的场景(py.test -v -s)。要求单次运行的原因是,我想从每个测试套件中获取事务 ID 并发送合并电子邮件。例如,如果测试为 5 名员工运行,那么将生成 5 个唯一的事务 ID,我通过使用 selenium 检查 Web 元素来获取这些事务 ID。

  1. 登录门户
  2. 输入个人信息
  3. 输入教育信息
  4. 输入工作经历信息
  5. 提交
  6. 获取会话 ID 并将其附加到“session_id”列表中。
  7. 退出

所以,如果我有 5 名员工,那么“session_id”列表中将提供 5 个唯一的会话 ID。为所有 5 名员工运行所有测试后,我将获取会话 ID 并发送合并的电子邮件以及其他一些其他信息。

我尝试通过夹具中的参数化来做到这一点(params=["employee_1", "employee_1" ... "employee_n"]。但问题是,它针对给定数量的参数单独运行每个测试。但是我希望为每个参数运行整个测试套件。

示例:

当我执行 py.test -v -s 时,对于 params 中的每个员工,我都想做这样的事情。

预期执行顺序:

员工 1:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

员工 2:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

员工 3:

test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py

实际执行顺序:

test_login.py (For Employee 1)
test_login.py (For Employee 2)
test_login.py (For Employee 3)
test_personal_information.py (For Employee 1)
test_personal_information.py (For Employee 2)
test_personal_information.py (For Employee 3)

。 . .

my_test_data = {
    "employee_1": {
        "personal_information": "...",
        "education_information": "...",
        "work_experience_information": "...",
    },
    "employee_2": {"..."}
    # Till exmployee n
}

################ conftest.py ################
@pytest.fixture(scope="session", params=["employee_1", "employee_2", "employee_3", "employee_n"]) # Till employee n
def test_setup(request):
    driver = webdriver.Chrome()
    driver.get(url)
    session_id = []
    yield driver, request.param, session_id
    driver.quit()


################ test_login.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
    # Login action is performed here
    pass


################ test_personal_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.personal_information_page = PersonalInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_personal_information_page(self):
        self.personal_information_page.send_first_name(self.test_Data["personal_information"]["first_name"])
        self.personal_information_page.send_first_name(self.test_Data["personal_information"]["last_name"])
        # Few other personal informations are also sent
        self.personal_information_page.click_on_next_button()


################ test_education_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestEducationInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.education_information_page = EducationInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_education_information_page(self):
        self.education_information_page.send_highest_degree(self.test_Data["education_information"]["highest_degree"])
        self.education_information_page.send_university_name(self.test_Data["education_information"]["university_name"])
        # Few other education informations are also sent
        self.education_information_page.click_on_next_button()


################ test_work_experience_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestWorkExperienceInformation(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def class_setup(self, set_driver):
        self.work_experience_page = WorkExperienceInformationPage(drier=test_setup[0])
        self.test_Data = test_setup[1]

    def test_work_experience_page_page(self):
        self.work_experience_page.send_work_experience(self.test_Data["work_experience_information"]["work_experience"])
        self.work_experience_page.send_current_organization_name(self.test_Data["work_experience_information"]["current_organization_name"])
        # Few other work experience informations are also sent
        self.work_experience_page.submit()


################ test_logout.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
    # Logout action is performed here
    pass

【问题讨论】:

    标签: python selenium pytest


    【解决方案1】:

    对于pytest,我能想到的东西很少。请检查它们是否同样适用于unittest

    首先,将所有测试用例移到一个类下,以便按顺序执行测试功能。比如:

    class TestUser:
    
        def test_login(self, test_setup):
            pass
    
        def test_personal_information(self, test_setup):
            pass
    
        def test_education_information(self, test_setup):
            pass
    
        def test_work_experience_information(self, test_setup):
            pass
    
        def test_logout(self, test_setup):
            pass
    

    或者,创建一个单独的类并继承其中的所有其他类按顺序

    class TestUser(TestPersonalInformation, TestEducationInformation, TestWorkExperienceInformation):
        pass
    

    或者,(对于 pytest)使用像 pytest-order 这样的排序插件

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 2017-10-02
      • 2021-09-13
      • 2020-05-14
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多