隨筆記錄
目錄
1. 安裝?
2. 安裝pytest 相關(guān)插件
2.1 準(zhǔn)備階段
2.2 安裝?
2.3 驗證安裝成功?
3. pytest測試用例的運行方式
3.1 主函數(shù)模式
3.1.1 主函數(shù)執(zhí)行指定文件
?3.1.2?主函數(shù)執(zhí)行指定模塊
3.1.3 主函數(shù)執(zhí)行某個文件中的某個類、方法、函數(shù)
3.1.4?主函數(shù)執(zhí)行生成allure報告
3.2 命令行模式
1. 安裝?
1. install pycharm
2. install python
3. config Envrionment variable
2. 安裝pytest 相關(guān)插件
2.1 準(zhǔn)備階段
# 將以下插件寫入 requirements.txt 中
pytest-rerunfailures #用例失敗后重跑
pytest-xdist # 測試用例分布式執(zhí)行,多CPU 分發(fā)
pytest-ordering # 控制用例執(zhí)行順序
pytest # pytest 框架
pytest-html # 生成html格式的自動化測試報告
allure-pytest # 用于生成美觀的測試報告
2.2 安裝?
terminal 執(zhí)行 以下命令,一次性安裝所有插件:
# pip install -r .\requirements.txt
文章來源:http://www.zghlxwxcb.cn/news/detail-827357.html
2.3 驗證安裝成功?
執(zhí)行一下命令,驗證pytest 安裝成功
# pytest
PS D:\Backup\自動化腳本\Riskcop> pytest
================================================================================================================================================== test session starts ===================================================================================================================================================
platform win32 -- Python 3.7.9, pytest-7.4.0, pluggy-1.0.0
rootdir: D:\Backup\自動化腳本\Riskcop
plugins: allure-pytest-2.13.2, anyio-3.6.1, Faker-18.10.1, assume-2.4.3, forked-1.4.0, html-3.1.1, metadata-2.0.1, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
collected 0 items
================================================================================================================================================= no tests ran in 0.02s ==================================================================================================================================================
PS D:\Backup\自動化腳本\Riskcop>
文章來源地址http://www.zghlxwxcb.cn/news/detail-827357.html
3. pytest測試用例的運行方式
3.1 主函數(shù)模式
#主函數(shù)植式
1. 運行所有:pytest.main()
2. 指定模塊:
# pytest main(['-vs','<文件名>'])
# pytest.main(-vs','test login.py])
3. 指定目錄:
# pytest main(['-vs','<模塊名>'])
# pytest main(-vs','/interface_testcase])
4. 通過nodeid指定用例運行:nodeid由模塊名,分隔符(::),類名,方法名,函數(shù)名組成。
4.1 運行指定函數(shù)
# pytest main(['-vs','<模塊名>/<文件名>::<方法名>'])
# pytest.main(['-vs','./interface_testcase/test_interface.py::test_04_func'])
4.2 運行某個類中的某個方法
# pytest main(['-vs','<模塊名>/<文件名>::<類名>::<方法名>'])
# pytest main(['- vs','./interface_testcase/test_interface.py::Testinterface::test_03_zhiliao'])
# 參數(shù)詳解:
-S:表示輸出調(diào)試信息,包括print打印的信息
-V:顯示更詳細(xì)的信息
-VS:這兩個參數(shù)一起用
分隔符- "::"
3.1.1 主函數(shù)執(zhí)行指定文件
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件 :main.py
@說明 :
@時間 :2024/01/22 17:07:32
@作者 :magx
@版本 :1.0
'''
import os
import time
import pytest
# 當(dāng)前路徑 (使用adbpath 方法 可通過dos 窗口執(zhí)行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上級目錄
father_path = os.path.abspath(os.path.join(current_path,".."))
# json 報告路徑
json_report_path = os.path.join(current_path, './Reports/json')
# html 報告路徑
html_report_path = os.path.join(current_path, './Reports/html')
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
if __name__ == '__main__':
'''
-v : 詳細(xì)信息 - 文件名:: 類名::方法名:
-s : 表示輸出調(diào)試信息,包括print 打印的信息
'''
# 方式1:
# 指定運行文件 "./TestCases/test_AccountLevel_2.py"
# test_AccountLevel_2.py
# test_MultiRule_12
pytest.main(['-vs','./TestCases/test_AccountLevel_2.py']) # test_AccountLevel_2.py
?3.1.2?主函數(shù)執(zhí)行指定模塊
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件 :main.py
@說明 :
@時間 :2024/01/22 17:07:32
@作者 :magx
@版本 :1.0
'''
import os
import time
import pytest
# 當(dāng)前路徑 (使用adbpath 方法 可通過dos 窗口執(zhí)行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上級目錄
father_path = os.path.abspath(os.path.join(current_path,".."))
# json 報告路徑
json_report_path = os.path.join(current_path, './Reports/json')
# html 報告路徑
html_report_path = os.path.join(current_path, './Reports/html')
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
if __name__ == '__main__':
'''
-v : 詳細(xì)信息 - 文件名:: 類名::方法名:
-s : 表示輸出調(diào)試信息,包括print 打印的信息
'''
# 方式2: 運行指定模塊
pytest.main(['-vs', './TestCases/'])
3.1.3 主函數(shù)執(zhí)行某個文件中的某個類、方法、函數(shù)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件 :main.py
@說明 :
@時間 :2024/01/22 17:07:32
@作者 :magx
@版本 :1.0
'''
import os
import time
import pytest
# 當(dāng)前路徑 (使用adbpath 方法 可通過dos 窗口執(zhí)行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上級目錄
father_path = os.path.abspath(os.path.join(current_path,".."))
# json 報告路徑
json_report_path = os.path.join(current_path, './Reports/json')
# html 報告路徑
html_report_path = os.path.join(current_path, './Reports/html')
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
if __name__ == '__main__':
'''
-v : 詳細(xì)信息 - 文件名:: 類名::方法名:
-s : 表示輸出調(diào)試信息,包括print 打印的信息
'''
# 方法3: 指定運行某個文件中的某個類、方法、函數(shù)
# 模塊名/文件名::函數(shù)名
# 文件名::類名::方法名
pytest.main(['--vs','./TestCases/test_AccountLevel_2.py::cleanlog'])
3.1.4?主函數(shù)執(zhí)行生成allure報告
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件 :main.py
@說明 :
@時間 :2024/01/22 17:07:32
@作者 :magx
@版本 :1.0
'''
import os
import time
import pytest
# 當(dāng)前路徑 (使用adbpath 方法 可通過dos 窗口執(zhí)行)
current_path = os.path.dirname(os.path.abspath(__file__))
print('current_path:',current_path)
# 上級目錄
father_path = os.path.abspath(os.path.join(current_path,".."))
# json 報告路徑
json_report_path = os.path.join(current_path, './Reports/json')
# html 報告路徑
html_report_path = os.path.join(current_path, './Reports/html')
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
if __name__ == '__main__':
'''
-v : 詳細(xì)信息 - 文件名:: 類名::方法名:
-s : 表示輸出調(diào)試信息,包括print 打印的信息
'''
# ===================================================================================
# --alluredir生成json格式報告
# allure generate 使用generate命令導(dǎo)出html報告,json_report_path json格式報告路徑, -o生成報告到文件夾, --clean清空原來的報告
#執(zhí)行pytest下的用例并生成json文件
pytest.main(['-vs', './TestCases','--alluredir=%s' %json_report_path, '--clean-alluredir'])#, '--clean-alluredir'
# 把json文件轉(zhuǎn)成html報告
os.system('allure generate %s -o %s --clean' %(json_report_path, html_report_path))
3.2 命令行模式
# 命令行模式
1. 運行所有:pytest
2. 指定模塊:
# pytest -vs <文件名>
# pytest -vs test_login.py
3. 指定目錄:
# pytest-vs <模塊名>
# pytest-vs ./interface_testcase
4. 指定目錄:
通過nodeid指定用例運行:nodeid由模塊名,分隔符(::),類名,方法名,函數(shù)名組成
# pytest-vs ./interface testcase/test interface.py:test 04_func
到了這里,關(guān)于pytest 框架自動化測試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!