pytest框架学习
1. 安装pytest
a首先安装好python---
b. pip 安装Pytest----在cmd下运行:pip install pytest
注意必须先把C:\Python27\Scripts(python的安装路径在c盘)放到环境变量下
c.pytest --version
b步骤也可以使用源码安装,需要自己下载pytest-3.0.6.tar.gz,运行:python setup.py install
2.第一个实例
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
# py.test
=========================================================================================================== test session starts ===========================================================================================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
collected 1 items
test_sample.py F
================================================================================================================ FAILURES =================================================================================================================
_______________________________________________________________________________________________________________ test_answer _______________________________________________________________________________________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:8: AssertionError
====================================================================================
pytest通过标准测试发现规则发现test_answer函数,通常是查找 test_前缀。我们得到了一个故障报告,因为我们调用func(3)没有返回5。