第一步:安裝 和查看版本
pycharm settings 查看?
?第二步: 編寫(xiě)test_example.py
def inc(x):
return x+1
def test_answer():
assert inc(4) ==5
第三步:在當(dāng)前路徑下執(zhí)行pytest 命令
PS E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example> pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item
test_sample.py . [100%]
========================================================================= 1 passed in 0.07s =========================================================================
PS E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example> pytest --version
pytest 7.1.2
PS E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example>
test_sample.py? 后面帶了個(gè)“.” 表示通過(guò)
def inc(x):
return x + 1
def test_answer():
ret = inc(3)
assert ret == 111
如果不符合預(yù)期顯示結(jié)果如下
PS E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example> pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item
test_sample.py F [100%]
============================================================================= FAILURES ==============================================================================
____________________________________________________________________________ test_answer ____________________________________________________________________________
def test_answer():
ret = inc(3)
> assert ret == 1111
E assert 4 == 1111
test_sample.py:7: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_sample.py::test_answer - assert 4 == 1111
========================================================================= 1 failed in 0.40s =========================================================================
PS E:\data\web測(cè)試\Selenium3自動(dòng)化測(cè)試實(shí)戰(zhàn)——基于Python語(yǔ)言\mycode\pytest_example>
問(wèn)題 解決:pytest : 無(wú)法將“pytest”項(xiàng)識(shí)別為 cmdlet、函數(shù)、腳本文件或可運(yùn)行程序的名稱。請(qǐng)檢查名稱的拼寫(xiě),如果包括路徑,請(qǐng)確保路徑正確,然后再試一次。
D:\software\python3\anconda3\Scripts
重啟pycharm 后輸入pytest文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-692769.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-692769.html
到了這里,關(guān)于pytest筆記: pytest單元測(cè)試框架的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!