可能大家在做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
