最新更新请看 《基于Pytest框架的自动化测试开发实践(万字长文入门篇)》
1、Pytest介绍
pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它具有如下特点:
- 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
- 能够支持简单的单元测试和复杂的功能测试
- 支持参数化
- 执行测试过程中可以将某些测试跳过,或者对某些预期失败的case标记成失败
- 支持重复执行失败的case
- 支持运行由nose, unittest编写的测试case
- 具有很多第三方插件,并且可以自定义扩展
- 方便的和持续集成工具集成
2、安装pytest
pip install -U pytest
安装完成后,可以验证安装的版本:
py.test --version
3、一个实例
# content of test_sample.py def func(x): return x+1 def test_func(): assert func(3) == 5
这里我们定义了一个被测试函数func,该函数将传递进来的参数加1后返回。我们还定义了一个测试函数test_func用来对func进行测试。test_func中我们使用基本的断言语句assert来对结果进行验证。
$ py.test =========================== test session starts ============================ platform linux -- Python 3.4.1 -- py-1.4.27 -- pytest-2.7.1 rootdir: /tmp/doc-exec-101, inifile: 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:5: AssertionError ========================= 1 failed in 0.01 seconds =========================
执行测试的时候,我们只需要在测试文件test_sample所在的目录下,运行py.test即可。pytest会在当前的目录下,寻找以test开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找test_开头的测试函数并执行。
4、再一个实例
# content of test_class.py class TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check')
我们可以通过执行测试文件的方法,执行上面的测试:
$ py.test -q test_class.py .F ================================= FAILURES ================================= ____________________________ TestClass.test_two ____________________________ self =
def test_two(self): x = "hello" > assert hasattr(x, 'check') E assert hasattr('hello', 'check') test_class.py:8: AssertionError 1 failed, 1 passed in 0.01 seconds
从测试结果中可以看到,该测试共执行了两个测试样例,一个失败一个成功。同样,我们也看到失败样例的详细信息,和执行过程中的中间结果。
5、如何编写pytest测试样例
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有 __init__ 方法
- 测试函数以test_开头
- 断言使用基本的assert即可
6、如何执行pytest测试样例
py.test # run all tests below current dir py.test test_mod.py # run tests in module py.test somepath # run all tests below somepath py.test -k stringexpr # only run tests with names that match the # the "string expression", e.g. "MyClass and not method" # will select TestMyClass.test_something # but not TestMyClass.test_method_simple py.test test_mod.py::test_func # only run tests that match the "node ID", # e.g "test_mod.py::test_func" will select # only test_func in test_mod.py
7、测试报告
pytest可以方便的生成测试报告,即可以生成HTML的测试报告,也可以生成XML格式的测试报告用来与持续集成工具集成。
生成HTML格式报告:
py.test --resultlog=path
生成XML格式的报告:
py.test --junitxml=path
8、如何获取帮助信息
py.test --version # shows where pytest was imported from py.test --fixtures # show available builtin function arguments py.test -h | --help # show help on command line and config file options
9、最佳实践
virtualenv . # create a virtualenv directory in the current directory source bin/activate # on unix
2、在虚拟环境中安装pytest:
pip install pytest
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229516.html原文链接:https://javaforall.net
