有人能帮我解决Selenium Pytest框架问题吗?我试着在谷歌上搜索。但什么也帮不了我。为这个长问题道歉。想确保我对问题有一个清晰的了解。
我在下面的字典"中有员工测试数据my_test_data";。下面是我想要实现的场景,针对每个员工在一次运行中(py.test-v-s(。要求单次运行的原因是,我想从每个测试套件中获取事务ID,并发送一封合并的电子邮件。例如,如果测试为5名员工运行,那么将生成5个唯一的事务ID,我通过使用selenium检查web元素来获得这些事务ID。
- 登录门户
- 输入个人信息
- 输入教育信息
- 输入工作经验信息
- 提交
- 获取会话ID并在其上附加"会话ID";session_id";列表
- 注销
因此,如果我有5名员工,那么在";session_id";列表一旦对所有5名员工进行了所有测试,我就会获取会话ID,并发送合并的电子邮件以及其他一些额外信息。
我试着用fixture中的参数化(params=[quot;employee_1",quot;employee_1"…quot;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
对于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订单的订购插件