繁体   English   中英

如何为整个测试套件应用参数化,而不是将其用于 pytest Selenium 中的单个测试用例

[英]How to apply parameterization for whole test suite rather than using it for individual single test case in pytest Selenium

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

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

  1. 登录门户
  2. 输入个人信息
  3. 输入教育信息
  4. 输入工作经验信息
  5. 提交
  6. 使用“session_id”列表获取 Session ID 和 append。
  7. 登出

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

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

例子:

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

预期执行顺序:

员工 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

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

首先,将所有测试用例移动到单个 class 下,以便按顺序执行测试功能。 就像是:

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 并按顺序继承其中的所有其他类:

class TestUser(TestPersonalInformation, TestEducationInformation, TestWorkExperienceInformation):
    pass

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

暂无
暂无

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

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