網(wǎng)站規(guī)劃書(shū)包括哪些方面公司官網(wǎng)怎么制作
前言
在編寫(xiě)自動(dòng)化測(cè)試用例的時(shí)候經(jīng)常會(huì)遇到需要編寫(xiě)流程性測(cè)試用例的場(chǎng)景,一般流程性的測(cè)試用例的測(cè)試步驟比較多,我們?cè)跍y(cè)試用例中添加詳細(xì)的步驟會(huì)提高測(cè)試用例的可閱讀性。
allure提供的裝飾器@allure.step()是allure測(cè)試報(bào)告框架非常有用的功能,它能幫助我們?cè)跍y(cè)試用例中對(duì)測(cè)試步驟進(jìn)行詳細(xì)的描述。
@allure.step的使用例子:
實(shí)現(xiàn)一個(gè)購(gòu)物的場(chǎng)景:1.登錄;2.瀏覽商品;3.將商品加入到購(gòu)物車(chē)中;4.下單;5.支付訂單;
# file_name: test_allure_step.pyimport pytest
import allure@allure.step
def login():"""執(zhí)行登錄邏輯:return:"""print("執(zhí)行登錄邏輯")@allure.step
def scan_good():"""執(zhí)行瀏覽商品邏輯:return:"""print("執(zhí)行瀏覽商品邏輯")@allure.step
def add_good_to_shopping_car():"""將商品添加到購(gòu)物車(chē):return:"""print("將商品添加到購(gòu)物車(chē)")@allure.step
def generator_order():"""生成訂單:return:"""print("生成訂單")@allure.step
def pay():"""支付訂單:return:"""print("支付訂單")def test_buy_good():"""測(cè)試購(gòu)買(mǎi)商品:步驟1:登錄步驟2:瀏覽商品步驟3:將商品加入到購(gòu)物車(chē)中步驟4:下單步驟5:支付:return:"""login()scan_good()add_good_to_shopping_car()generator_order()pay()with allure.step("斷言"):assert 1if __name__ == '__main__':pytest.main(['-s', 'test_allure_step.py'])
執(zhí)行命令:
> pytest test_allure_step.py --alluredir=./report/result_data> allure serve ./report/result_data
查看測(cè)試報(bào)告展示效果:
從報(bào)告中可以看到,我們事先通過(guò)@allure.step()定義好的步驟都展示在測(cè)試用例test_buy_good()下了。
@allure.step支持嵌套,step中調(diào)用step
# file_name: steps.pyimport allure@allure.step
def passing_step_02():print("執(zhí)行步驟02")pass
測(cè)試用例:
# file_name: test_allure_step_nested.pyimport pytest
import allurefrom .steps import passing_step_02 # 從外部模塊中導(dǎo)入@allure.step
def passing_step_01():print("執(zhí)行步驟01")pass@allure.step
def step_with_nested_steps():"""這個(gè)步驟中調(diào)用nested_step():return:"""nested_step()@allure.step
def nested_step_with_arguments(arg1, arg2):pass@allure.step
def nested_step():"""這個(gè)步驟中調(diào)用nested_step_with_arguments(),并且傳遞參數(shù):return:"""nested_step_with_arguments(1, 'abc')def test_with_imported_step():"""測(cè)試@allure.step()支持調(diào)用從外部模塊導(dǎo)入的step:return:"""passing_step_01()passing_step_02()def test_with_nested_steps():"""測(cè)試@allure.step()支持嵌套調(diào)用step:return:"""passing_step_01()step_with_nested_steps()passing_step_02()if __name__ == '__main__':pytest.main(['-s', 'test_allure_step_nested.py'])
執(zhí)行命令:
pytest test_allure_step_nested.py --alluredir=./report/result_dataallure serve ./report/result_data
查看測(cè)試報(bào)告展示效果:
從上面的結(jié)果中可以看到:
- @step可以先保存到其他模塊中,在測(cè)試用例中需要用到的時(shí)候?qū)刖涂梢粤?#xff1b;
- @step也支持在一個(gè)step中嵌套調(diào)用其他的step;嵌套的形式在測(cè)試報(bào)告中以樹(shù)形展示出來(lái)了;
@allure.step支持添加描述且通過(guò)占位符傳遞參數(shù)
# file_name: test_allure_step_with_placeholder.pyimport pytest
import allure@allure.step('這是一個(gè)帶描述語(yǔ)的step,并且通過(guò)占位符傳遞參數(shù):positional = "{0}",keyword = "{key}"')
def step_title_with_placeholder(arg1, key=None):passdef test_step_with_placeholder():step_title_with_placeholder(1, key="something")step_title_with_placeholder(2)step_title_with_placeholder(3, key="anything")if __name__ == '__main__':pytest.main(['-s', 'test_allure_step_with_placeholder.py'])
執(zhí)行命令:
pytest test_allure_step_with_placeholder.py --alluredir=./report/result_dataallure serve ./report/result_data
查看測(cè)試報(bào)告展示效果:
從上面的執(zhí)行結(jié)果中可以看到,@allure.step()是支持輸入描述的,并且支持通過(guò)占位符向描述中傳遞參數(shù)。
在conftest.py文件中定義@allure.step
conftest.py文件:
# file_name: conftest.pyimport pytest
import allure@pytest.fixture()
def fixture_with_conftest_step():conftest_step()@allure.step("這是一個(gè)在conftest.py文件中的step")
def conftest_step():pass
測(cè)試用例:
# file_name: test_allure_step_in_fixture_from_conftest.pyimport pytest
import allure@allure.step
def passed_step():passdef test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):passed_step()if __name__ == '__main__':pytest.main(['-s', 'test_allure_step_in_fixture_from_conftest.py'])
執(zhí)行命令:
pytest test_allure_step_in_fixture_from_conftest.py --alluredir=./report/result_dataallure serve ./report/result_data
查看測(cè)試報(bào)告展示效果:
從運(yùn)行結(jié)果中可以看到,在fixture中定義的step會(huì)在setup和teardown單獨(dú)以樹(shù)形結(jié)構(gòu)展示出來(lái)。
這可能是B站最詳細(xì)的pytest自動(dòng)化測(cè)試框架教程,整整100小時(shí),全程實(shí)戰(zhàn)!!!