pytest.ini的作用
可以改變pytest的運行方式,讀取配置信息,並按指定的方式去運行
非test文件
pytest里面有些文件是非test文件
- pytest.ini:pytest的主配置文件,可以改變pytest的默認行為
- conftest.py:測試用例的一些fixture配置
- init.py:識別該文件夾為python的package包
pytest.ini應該放哪里?
必須放在項目根目錄下 ,不要亂放、亂起其他名字
配置的使用
marks的使用
作用:測試用例中添加了 @pytest.mark.webtest 裝飾器,如果不添加marks選項的話,就會報warnings
格式:list列表類型
示例:
[pytest]
markers =
mylogin: this is mylogin page
query: this is query page
addcart: this is addcart page
關於案例,請移步到《Pytest學習(八) - 自定義標記mark的使用》
xfail_strict的使用
作用:設置xfail_strict = True可以讓那些標記為@pytest.mark.xfail但實際通過顯示XPASS的測試用例被報告為失敗
格式:True 、False(默認),1、0
示例:
[pytest]
markers =
mylogin: this is mylogin page
query: this is query page
addcart: this is addcart page
xfail_strict = True
案例代碼如下:
# -*- coding: utf-8 -*-
# @Time : 2020/11/29 11:01
# @Author : longrong.lang
# @FileName: test_pytestini.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
import pytest
@pytest.mark.xfail()
def test_case():
assert 1 == 1
1、未設置 xfail_strict = True 時,測試結果顯示XPASS
執行效果如下:
2、已設置 xfail_strict = True 時,測試結果顯示FAILED
執行效果如下:
addopts的使用
1、安裝插件
pip3 install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install pytest-rerun -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install pytest-count -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2、作用
addopts參數可以更改默認命令行選項,這個當我們在cmd輸入指令去執行用例的時候,會用到,比如我想測試完生成報告,指令比較長
pytest -v —rerun 1 —html=report.html —self-contained-html
每次輸入這么多,不太好記住,於是可以加到pytest.ini里,示例代碼如下:
[pytest]
markers =
mylogin: this is mylogin page
query: this is query page
addcart: this is addcart page
xfail_strict = True
addopts = -v --rerun 1 --html=report.html --self-contained-html
這樣下次打開cmd,直接輸入pytest,它就能默認帶上這些參數了
log_cli的使用
作用:控制台實時輸出日志
格式:log_cli=True 或False(默認),或者log_cli=1 或 0
log_cli=0的運行結果
log_cli=1的運行結果
結論
很明顯,加了log_cli=1之后,可以清晰看到哪個package下的哪個module下的哪個測試用例是否passed還是failed;
這個地方我還是沒太弄明白,怎么用,有懂得盆友可以在評論下方留言,還請賜教。