谭子pytest学习笔记(1)

来源:互联网 发布:ubuntu root修改grub 编辑:程序博客网 时间:2024/05/16 15:13

  •  谭子pytest学习笔记1
    •  pytest的特点
    •  安装方法
    •  pytest用例搜集及执行
    •  pytest命令行及执行方式总结
      •  pytest命令行执行方式
      •  python命令行参数
        •  帮助信息版本参数项名称环境变量等
        •  常用命令行参数
      •  通过python代码执行pytest
      •  断言

 谭子pytest学习笔记(1)

 pytest的特点

- 模块化parametrizeable装置
- 参数化测试函数
- 标记测试功能属性
- Skip和xfail:处理不成功的测试用例
- 通过xdist插件分发测试到多个CPU
- 不断的重新运行失败的测试
- 灵活约定的Python测试发现

 安装方法

方法一:

需要有外网,需要先安装python的pip安装工具

pip install -U pytest

安装成功判断方法:

windows:

C:\Users\27231>pytest --versionThis is pytest version 3.0.7, imported from D:\Python27\lib\site-packages\pytest-3.0.7-py2.7.egg\pytest.pyc

Linux:

$ pytest --versionThis is pytest version 3.0.7, imported from $PYTHON_PREFIX/lib/python3.5/site-˓→packages/pytest.py

方法二:

由于公司内无法直接访问外网,故给出,离线安装放法;离线安装需要下载pytest的安装包及其两个依赖库 colorama、py

对应下载链接如下:
pytest: https://pypi.python.org/pypi/pytest
colorama: https://pypi.python.org/pypi/colorama
py: https://pypi.python.org/pypi/py

三个包安装先后顺序为: colorama ,py , pytest

安装方式:
注意:windows下若python未计入环境变量,请先将其加入环境变量

windows使用cmd,Linux直接切到指定包对应的目录Y:\接口自动化测试\安装包\pytest\colorama-0.3.9\colorama-0.3.9>python setup.py install

 pytest用例搜集及执行

1)如下为对一个函数测试的简单用例:

# content of test_sample.pydef func(x): return x + 1def test_answer(): assert func(3) == 5

通过pytest执行用例方法及结果如下

C:\Users\Administrator\PycharmProjects\pytest\myprac>pytest============================= test session starts =============================platform win32 -- Python 2.7.5, pytest-3.0.7, py-1.4.33, pluggy-0.4.0metadata: {'Python''2.7.5''Platform''Windows-7-6.1.7601-SP1''Packages': {'py''1.4.33''pytest''3.0.7''pluggy''0.4.0'}, 'JAVA_HOME''C:\\Program Files (x86)\\Java\\jdk1.6.0_10''Plugins': {'rerunfailures''2.1.0''html''1.1''metadata''1.4.0'}}rootdir: C:\Users\Administrator\PycharmProjects\pytest, inifile: pytest.iniplugins: metadata-1.4.0, html-1.1, rerunfailures-2.1.0collected 1 itemstest_sample.py F generated html file: C:\Users\Administrator\PycharmProjects\pytest\myprac\report.html================================== FAILURES ===================================_________________________________ test_answer _________________________________    def test_answer():>       assert func(3) == 5E       assert 4 == 5E        +  where 4 = func(3)test_sample.py:5: AssertionError=========================== pytest-warning summary ============================WI2 build\bdist.win32\egg\pytest_html\plugin.py:25 use pluginmanager.add_hookspecs instead of deprecated addhooks() method.================= 1 failed, 1 pytest-warnings in 0.04 seconds =================

由执行结果看到,pytest失败的测试报告输出,还是很友好的,函数fun(3)的返回值友好的展示在了测试结果中

2)执行方式1)中会执行当前目录及子目录下的所有的test_*.py 或者 *_test.py的文件,用例搜集规则为搜集Test开头的类,test开头的函数

看一个通过类进行分组的用例集

class TestClass(object):    def test_one(self):        x = "this"        assert 'h' in x    def test_two(self):        x = "hello"        assert  hasattr(x, 'check')

通过pytest test_class.py 可指定执行的文件,通过pytest test_class.py::TestClass::test_one 可执行指定的用例,加入 -q参数可使得测试结果只展示必要信息,如:

C:\Users\Administrator\PycharmProjects\pytest\myprac>pytest -q test_sample.py::TestClass::test_twoF generated html file: C:\Users\Administrator\PycharmProjects\pytest\myprac\report.html================================== FAILURES ===================================_____________________________ TestClass.test_two ______________________________self = <test_sample.TestClass object at 0x031DA0B0>    def test_two(self):        x = "hello">       assert  hasattr(x, 'check')E       AssertionError: assert FalseE        +  where False = hasattr('hello''check')test_sample.py:9: AssertionError=========================== pytest-warning summary ============================WI2 build\bdist.win32\egg\pytest_html\plugin.py:25 use pluginmanager.add_hookspecs instead of deprecated addhooks() method.1 failed, 1 pytest-warnings in 0.04 seconds

 pytest命令行及执行方式总结

 pytest命令行执行方式

 -  python -m pytest […]
 -  pytest […]

 python命令行参数

 帮助信息、版本、参数项名称、环境变量等

pytest --version # 展示pytest的加载路径及已经安装的相关插件的路径pytest --fixtures # 显示可用的内置函数参数pytest -h | --help # 显示pytest命令行及配置文件相关选项的帮助信息

 常用命令行参数

1)在第几个测试用例失败后,停止测试

pytest -x # 出现第一个用例执行失败时即停止测试pytest --maxfail=2 # 在有两个用例执行失败时停止测试pytest filename --full-trace #跟踪用例执行的完整过程pytest filename --durations=10  #获取执行最慢的10个用例pytest filename --rerun=5  #失败用例重复执行5次pytest filename --html=report.html #输出html报告pytest filename -m high  #指定执行high标识的用例

2)指定执行测试范围

pytest test_mod.py # 执行指定文件下的测试pytest somepath # 执行指定目录下及其子目录下的所有测试文件pytest -k stringexpr # only run tests with names that match the                     # "string expression", e.g. "MyClass and not method"                     # will select TestMyClass.test_something                     # but not TestMyClass.test_method_simplepytest test_mod.py::test_func # 只运行指定模块下的用例test_func,                              # e.g. "test_mod.py::test_func" will select                              # only test_func in test_mod.pypytest test_mod.py::TestClass::test_method # 执行指定文件下指定类中的用例test_method

 通过python代码执行pytest

通过python代码直接调用执行pytest

pytest。main() # 相当于命令行直接pytestpytest.main(['-q','test_sample.py'], plugins=[MyPlugin()]) # 第一个为pytest执行参数,第二个为pytest执行时需要使用的插件

 断言

1)python支持通过python的标准语句assert来验证期望值与实际值是否一致

example_one:

# content of test_sample.pydef tanzi():    return 3def test_tanzi():    assert tanzi() == 4

执行结果如下:

C:\Users\Administrator\PycharmProjects\pytest\myprac>pytest  -q test_sample.pyF generated html file: C:\Users\Administrator\PycharmProjects\pytest\myprac\report.html================================== FAILURES ===================================_________________________________ test_tanzi __________________________________    def test_tanzi():>       assert tanzi() == 4E       assert 3 == 4E        +  where 3 = tanzi()test_sample.py:5: AssertionError=========================== pytest-warning summary ============================WI2 build\bdist.win32\egg\pytest_html\plugin.py:25 use pluginmanager.add_hookspecs instead of deprecated addhooks() method.1 failed, 1 pytest-warnings in 0.03 seconds

由失败用例的测试报告中可看到,pytest支持展示常见子表达式的值,包括调用、属性、比较,以及二进制和单目运算符

2)异常类断言
通过pytest.raises可断言python中的异常断言

example:

import pytestdef test_zero_division():    with pytest.raises(ZeroDivisionError):        1/0

有时候还要确认异常中一些更为具体的异常信息,可按如下方式实现

def test_recursion_depth():    with pytest.raises(RuntimeError) as excinfo:        def f():            f()    f()    assert 'maximum recursion' in str(excinfo.value)

excinfo 是一个Exceptioninfo的一个实例,它是实际异常的一个装饰器,其主要属性包括 .type, .value 和 .traceback

3)定义自己的断言比较式

pytest可通过pytest_assertrepr_compare(config, op, left, right)来构造自己的断言失败展示信息

# content of conftest.pyfrom test_sample import Foodef pytest_assertrepr_compare(op, left, right):    if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":        return ['Comparing Foo instances:',            ' vals: %s != %s' % (left.val, right.val)]
# content of test_sample.pyclass Foo:    def __init__(self, val):        self.val = val    def __eq__(self, other):        return self.val == other.valdef test_compare():    f1 = Foo(1)    f2 = Foo(2)    assert f1 == f2

f2 = Foo(2)时用例断言失败,输出结果为

C:\Users\Administrator\PycharmProjects\pytest\myprac>pytest  -q test_sample.pyF generated html file: C:\Users\Administrator\PycharmProjects\pytest\myprac\report.html================================== FAILURES ===================================________________________________ test_compare _________________________________    def test_compare():        f1 = Foo(1)        f2 = Foo(2)>       assert f1 == f2E       assert Comparing Foo instances:E          vals: 1 != 2test_sample.py:10: AssertionError=========================== pytest-warning summary ============================WI2 build\bdist.win32\egg\pytest_html\plugin.py:25 use pluginmanager.add_hookspecs instead of deprecated addhooks() method.1 failed, 1 pytest-warnings in 0.03 seconds

若将f2改为 f2 = Foo(1),断言结果为pass,则对应的自定义错误信息也不会打印执行

C:\Users\Administrator\PycharmProjects\pytest\myprac>pytest  -q test_sample.py. generated html file: C:\Users\Administrator\PycharmProjects\pytest\myprac\report.html=========================== pytest-warning summary ============================WI2 build\bdist.win32\egg\pytest_html\plugin.py:25 use pluginmanager.add_hookspecs instead of deprecated addhooks() method.1 passed, 1 pytest-warnings in 0.01 seconds
原创粉丝点击