可能大家在做python自動化過程中,用的比較多的是unittest,今天小編給大家講講如何使用pytest做我們的單元測試,生成一個美麗的報告。
在使用pytest之前,我們首先需要下載下傳安裝它。
一般我們有兩種途徑:
1、下載下傳pytest安裝包,解壓後cd到pytest中setup.py的同級目錄,執行python setup.py install.
2、 pip install -u pytest (那麼你首先得先安裝pip,然後将它加入環境變量,這裡不細說,之前的自動化環境文章裡有介紹過)。
驗證安裝成功指令:
pytest --version
一般我們的unittest方法都是以test開頭,pytest也不例外。
寫好腳本後,例如:
import pytest
def test01():
assert 2 == 5
def test02():
assert 5 == 5
def test03():
assert 4 == 5
使用pytest運作此腳本,生成html報告(當然要生成html報告,我們需要用到pytest-html庫,具體安裝方法:pip install pytest-html)
pytest testpytest.py --html=d:/report1.html
