![](/img/trans.png)
[英]Python tox and py.test: how to run just a single test rather than the whole test suite
[英]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。
因此,如果我有 5 名員工,那么“session_id”列表中將提供 5 個唯一的會話 ID。 為所有 5 名員工運行所有測試后,我將獲取 session ID 並發送合並的 email 以及其他一些附加信息。
我嘗試通過夾具中的參數化來做到這一點(params = [“employee_1”,“employee_1”...“employee_n”]。但問題是,它針對給定數量的參數單獨運行每個測試。但我想要為每個參數運行的整個測試套件。
當我執行 py.test -v -s 時,對於參數中的每個員工,我想做這樣的事情。
test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py
test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py
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.