Pytest01-環(huán)境安裝和入門
一、pytest介紹與安裝
1.pytest框架介紹
Pytest是Python的另一個(gè)第三方單元測(cè)試庫。在自動(dòng)化測(cè)試領(lǐng)域,pytest就是用來組織自動(dòng)化用例執(zhí)行的,包括指定執(zhí)行的用例模塊、用例前置后置操作、跳過執(zhí)行、失敗退出等控制功能。
pytest的特性有:
- 支持用簡(jiǎn)單的assert語句實(shí)現(xiàn)豐富的斷言,無需復(fù)雜的self.assert*函數(shù)
- 自動(dòng)識(shí)別測(cè)試模塊和測(cè)試函數(shù)
- 模塊化夾具用以管理各類測(cè)試資源
- 對(duì) unittest 完全兼容,對(duì) nose基本兼容
支持Python3和PyPy3 - 豐富的插件生態(tài),已有1000多個(gè)各式各樣的插件,社區(qū)繁榮
插件鏈接:https://docs.pytest.org/en/latest/reference/plugin_list.html
2.pytest 安裝
a)安裝
注意,盡量使用下面版本,最新版本可能會(huì)引入兼容性問題,比如Allure報(bào)告參數(shù)信息展示等等
pip install pytest==7.3.1
如果不清楚第三方庫的版本和pytest的版本兼容關(guān)系可以在如下網(wǎng)址查詢:
https://pypi.org/project/pytest-rerunfailures/#requirements
b)驗(yàn)證安裝
pytest --version
c) 運(yùn)行模式的配置

如上圖,請(qǐng)?jiān)贔ile>Setting>Tools>Python Integrated Tools>Testing>Defalut test runner中選擇Unittests模式
默認(rèn)是pytest或者auto,走的Pytest調(diào)試模式,只支持單文件調(diào)試,Unittests模式才能運(yùn)行多文件。
d) pytest文檔
官方文檔:https://docs.pytest.org/en/latest/contents.html
3.pytest的用例運(yùn)行規(guī)則
1)pytest將在當(dāng)前目錄及其子目錄中運(yùn)行所有格式為test_.py或者_test.py文件
2)測(cè)試方法/測(cè)試函數(shù) 默認(rèn)必須是test開頭
3) 測(cè)試類必須是Test開頭
4) 測(cè)試類不能有構(gòu)造方法 init
參考代碼目錄:

# 文件名: test_pytest.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
1)pytest將在當(dāng)前目錄及其子目錄中運(yùn)行所有格式為test_*.py或者*_test.py文件
2)測(cè)試方法/測(cè)試函數(shù) 默認(rèn)必須是test開頭
3) 測(cè)試類必須是Test開頭
4) 測(cè)試類不能有構(gòu)造方法 __init__
'''
import pytest
def test01():
print("hello")
assert 1 == 2
if __name__ == '__main__':
pytest.main(['-s'])
# 文件名: test_aabb.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 測(cè)試函數(shù)
def test02():
print("doc目錄下的用例test02")
class TestShopping:
# pytest測(cè)試類不能使用構(gòu)造方法
# def __init__(self):
# print("構(gòu)造方法")
# 測(cè)試方法
def test03(self):
print("類下的用例")
在test_pytest.py文件運(yùn)行后結(jié)果
D:\Python38\python.exe
============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-7.3.1, pluggy-0.13.1
rootdir: E:\p01_pytest_discover
plugins: allure-pytest-2.8.11, base-url-2.0.0, forked-1.1.3, html-3.0.0, metadata-1.8.0, ordering-0.6, parallel-0.1.0, playwright-0.3.3, repeat-0.9.1, rerunfailures-9.1.1, xdist-1.31.0
collected 3 items
test_pytest.py hello
F
doc\test_aabb.py doc目錄下的用例test02
.類下的用例
.
================================== FAILURES ===================================
___________________________________ test01 ____________________________________
def test01():
print("hello")
> assert 1 == 2
E assert 1 == 2
test_pytest.py:13: AssertionError
=========================== short test summary info ===========================
FAILED test_pytest.py::test01 - assert 1 == 2
========================= 1 failed, 2 passed in 0.21s =========================
Process finished with exit code 0
測(cè)試技術(shù)交流請(qǐng)聯(lián)系我

備注博客園扶搖
【學(xué)習(xí)軟件測(cè)試/Python自動(dòng)化測(cè)試技術(shù)/領(lǐng)取Python自動(dòng)化測(cè)試學(xué)習(xí)路線圖/簡(jiǎn)歷優(yōu)化】
視頻鏈接:
課程服務(wù)介紹
Jmeter接口自動(dòng)化測(cè)試實(shí)戰(zhàn)全集
加微信(備注博客園扶搖)即可免費(fèi)領(lǐng)取下面的自動(dòng)化測(cè)試資料和一份軟件測(cè)試面試寶典


本文來自博客園,作者:測(cè)試?yán)险蟹鰮u,轉(zhuǎn)載請(qǐng)注明原文鏈接:http://www.rzrgm.cn/cekailsf/p/17941074
posted on 2024-01-02 18:32 測(cè)試?yán)险蟹鰮u 閱讀(94) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)