自动化测试框架 - pytest

pytest是Python最流行的单元测试框架之一, 帮助更便捷的编写测试脚本, 并支持多种功能复杂的测试场景, 能用来做app测试也能用作函数测试

官方文档: https://docs.pytest.org/en/latest/

pytest具有以下优点:

  • 允许使用assert进行断言
  • 自动识别测试脚本、类、函数
  • 可用于管理小型或者参数类型的测试数据或资源
  • 兼容unittest和nose测试框架
  • 支持Python2.7/Python3.4+
  • 丰富的插件支持,超过315个插件支持

pytest安装

  1. pip install -U pytest

如果提示下面的错误,说明是pip的版本太老了, 要更新下:

  1. Could not find a version that satisfies the requirement pytest (from versions: )
  2. No matching distribution found for pytest

更新方式:

  1. easy_install --upgrade pip

官方示例

准备一个test_sample.py, 内容如下:

  1. def inc(x):
  2. return x +
  3.  
  4. def test_answer():
  5. assert inc() ==

在文件所在目录执行:

  1. pytest

这里我们做下说明:

pytest脚本都以test_xxx.py为文件名;

inc方法是我们定义的一个自增函数,该函数将传递进来的参数加1后返回;

test_answer是我们编写的一个测试函数,其中我们使用基本的断言语句assert来对结果进行验证,测试函数以test_xxx作为命名

执行结果如下:

  1. ============================================================ test session starts ============================================================
  2. platform darwin -- Python 2.7., pytest-4.1., py-1.7., pluggy-0.8.
  3. rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:
  4. collected item
  5.  
  6. test_sample.py F [%]
  7.  
  8. ================================================================= FAILURES ==================================================================
  9. ________________________________________________________________ test_answer ________________________________________________________________
  10.  
  11. def test_answer():
  12. > assert inc() ==
  13. E assert ==
  14. E + where = inc()
  15.  
  16. test_sample.py:: AssertionError
  17. ========================================================= failed in 0.05 seconds ==========================================================
  18. (wda_python) bash-3.2$

当执行到assert inc(3) == 5时,报错

执行pytest会在当前目录和子目录中寻找test_xx.py的测试文件,并进入到测试文件中寻找test_xx开头的测试函数开始执行

执行pytest -q  test_xxx.py是执行执行的脚本

在看一个例子,测试指定错误: (Assert that a certain exception is raised)

  1. import pytest
  2.  
  3. def f():
  4. raise SystemExit()
  5.  
  6. def test_mytest():
  7. with pytest.raises(SystemExit):
  8. f()

执行指令:

  1. pytest -q test_sysexit.py

输出:

  1. (wda_python) bash-3.2$ pytest -q test_sysexit.py
  2. . [%]
  3. passed in 0.04 seconds
  4. (wda_python) bash-3.2$

如果要开发多个测试方法,可以把方法写进一个class中

  1. class TestClass(object):
  2. def test_one(self):
  3. x = 'this'
  4. assert 'h' in x
  5.  
  6. def test_two(self):
  7. x = 'hello'
  8. assert hasattr(x, 'check')

pytest能够自动识别类中的测试方法, 也不用我们去创建子类或者实实例, 运行结果如下:

  1. (wda_python) bash-3.2$ pytest -q test_sample.py
  2. .F [%]
  3. ================================================================== FAILURES ==================================================================
  4. _____________________________________________________________ TestClass.test_two _____________________________________________________________
  5.  
  6. self = <test_sample.TestClass object at 0x102e151d0>
  7.  
  8. def test_two(self):
  9. x = 'hello'
  10. > assert hasattr(x, 'check')
  11. E AssertionError: assert False
  12. E + where False = hasattr('hello', 'check')
  13.  
  14. test_sample.py:: AssertionError
  15. failed, passed in 0.08 seconds
  16. (wda_python) bash-3.2$

除了直接在脚本路径执行pytest外, 还可以用以下方式

  1. python -m pytest xxx.py

出现第一个(或第N个)错误时停止

  1. pytest -x # stop after first failure
  2. pytest --maxfail= # stop after two failures

运行执行测试脚本

  1. pytest test_mod.py

运行指定目录下的所有脚本

  1. pytest testing/

运行包含指定关键字的测试方法, 可以是文件名、类名、测试函数名

  1. pytest -k "MyClass and not method"

执行node id运行测试脚本,每一个被收集的测试方法都会分配一个指定的id, 我们可以用一下方式运行执行的测试方法:

  1. # To run a specific test within a module
  2. pytest test_mod.py::test_func
  3.  
  4. # To run a test within a class
  5. pytest test_mod.py::TestClass::test_method

日志打印的不同方式

  1. pytest --showlocals # show local variables in tracebacks
  2. pytest -l # show local variables (shortcut)
  3.  
  4. pytest --tb=auto # (default) 'long' tracebacks for the first and last
  5. # entry, but 'short' style for the other entries
  6. pytest --tb=long # exhaustive, informative traceback formatting
  7. pytest --tb=short # shorter traceback format
  8. pytest --tb=line # only one line per failure
  9. pytest --tb=native # Python standard library formatting
  10. pytest --tb=no # no traceback at all

测试报告

pytest默认是完整的测试报告, 我们可以加上-r标签显示简短测试报告,可再搭配一下参数

  1. Here is the full list of available characters that can be used:
  2.  
  3. f - failed
  4. E - error
  5. s - skipped
  6. x - xfailed
  7. X - xpassed
  8. p - passed
  9. P - passed with output
  10. a - all except pP

可以多个参数一起使用

Debug模式

  1. pytest --pdb

示例:

  1. (wda_python) bash-3.2$ pytest --pdb
  2. ========================================================== test session starts ===========================================================
  3. platform darwin -- Python 2.7., pytest-4.1., py-1.7., pluggy-0.8.
  4. rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:
  5. collected items
  6.  
  7. test_sample.py .F
  8. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  9.  
  10. self = <test_sample.TestClass object at 0x10e928610>
  11.  
  12. def test_two(self):
  13. x = 'hello'
  14. > assert hasattr(x, 'check')
  15. E AssertionError: assert False
  16. E + where False = hasattr('hello', 'check')
  17.  
  18. test_sample.py:: AssertionError
  19. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  20. > /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test/test_sample.py()test_two()
  21. -> assert hasattr(x, 'check')
  22. (Pdb) print x
  23. hello
  24. (Pdb) print hasattr(x,'check')
  25. False
  26. (Pdb)

还可以指定第几次失败开始进入debug:

  1. pytest -x --pdb # drop to PDB on first failure, then end test session
  2. pytest --pdb --maxfail= # drop to PDB for first three failures

任何失败的异常信息都会存储在sys.last_value,sys.last_type 以及 sys_last_traceback

在debug中可以通过以下方式获取最后报错的内容

  1. (Pdb) import sys
  2. (Pdb) sys.last_traceback.tb_lineno
  3.  
  4. (Pdb) sys.last_value
  5. AssertionError(u"assert False\n + where False = hasattr('hello', 'check')",)
  6. (Pdb)

在执行一开始就进入到debug模式

  1. pytest --trace

输入next执行下一步, exit退出

脚本中设置断点

  1. import pdb
  2.  
  3. pdb.set_trace()

例如:

  1. import pdb
  2.  
  3. class TestClass(object):
  4. def test_one(self):
  5. x = 'this'
  6. pdb.set_trace()
  7. assert 'h' in x
  8.  
  9. def test_two(self):
  10. x = 'hello'
  11. assert hasattr(x, 'check')

获取执行最慢的n个测试步骤

  1. pytest --durations=
  1. ======================================================= slowest test durations ========================================================
  2.  
  3. (0.00 durations hidden. Use -vv to show these durations.)

但如果所有脚本的运行时间都小于0.01s, 就不显示了, 除非带上-vv参数

  1. pytest --durations= -vv

输出结果:

  1. ======================================================= slowest test durations ========================================================
  2. .00s call test_sample.py::TestClass::test_two
  3. .00s setup test_sysexit.py::test_mytest
  4. .00s setup test_sample.py::TestClass::test_two
  5. .00s setup test_sample.py::TestClass::test_one
  6. .00s teardown test_sample.py::TestClass::test_two
  7. .00s teardown test_sample.py::TestClass::test_one
  8. .00s call test_sysexit.py::test_mytest
  9. .00s teardown test_sysexit.py::test_mytest
  10. .00s call test_sample.py::TestClass::test_one
  11. =================================================== failed, passed in 0.06 seconds ===================================================
  12. (wda_python) bash-3.2$

将日志保存到指定文件

  1. pytest --resultlog=path

Disabling plugins

To disable loading specific plugins at invocation time, use the -p option together with the prefix no:.

Example: to disable loading the plugin doctest, which is responsible for executing doctest tests from text files, invoke pytest like this:

  1. pytest -p no:doctest

我们也可以在pytestdemo脚本中去启动pytest:

  1. import pytest
  2.  
  3. pytest.main()

执行python pytestdemo.py就可以执行pytest

main()不会抛出SystemExit的异常, 但会返回exitcode, 一共有6种exitcode

  1. Exit code : All tests were collected and passed successfully
  2. Exit code : Tests were collected and run but some of the tests failed
  3. Exit code : Test execution was interrupted by the user
  4. Exit code : Internal error happened while executing tests
  5. Exit code : pytest command line usage error
  6. Exit code : No tests were collected

我们试着加上打印

  1. import pytest
  2.  
  3. print pytest.main()

输出:

  1. (wda_python) bash-3.2$ python pytestDemo.py
  2. ========================================================== test session starts ===========================================================
  3. platform darwin -- Python 2.7., pytest-4.1., py-1.7., pluggy-0.8.
  4. rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:
  5. collected items
  6.  
  7. test_sample.py .F [ %]
  8. test_sysexit.py . [%]
  9.  
  10. ================================================================ FAILURES ================================================================
  11. ___________________________________________________________ TestClass.test_two ___________________________________________________________
  12.  
  13. self = <test_sample.TestClass object at 0x1038ba650>
  14.  
  15. def test_two(self):
  16. x = 'hello'
  17. > assert hasattr(x, 'check')
  18. E AssertionError: assert False
  19. E + where False = hasattr('hello', 'check')
  20.  
  21. test_sample.py:: AssertionError
  22. =================================================== failed, passed in 0.05 seconds ===================================================
  23.  
  24. (wda_python) bash-3.2$

我们还可以在main中传递参数:

  1. pytest.main(['-q','test_sample.py'])

给pytest.main添加plugin, 如下示例在执行的开头和结尾, 添加打印信息

  1. import pytest
  2.  
  3. class MyPlugin(object):
    def pytest_sessionfinish(self):
    print '*** Test run reporting finishing'
  4.  
  5. def pytest_sessionstart(self):
    print '*** Test run report beginning'
  6.  
  7. pytest.main(['-q','test_sample.py'], plugins=[MyPlugin()])

输出:

(wda_python) bash-3.2$ python pytestDemo.py
*** Test run report beginning
.F [100%]*** Test run reporting finishing

================================================================ FAILURES ================================================================
___________________________________________________________ TestClass.test_two ___________________________________________________________

self = <test_sample.TestClass object at 0x1090843d0>

def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')

test_sample.py:11: AssertionError
1 failed, 1 passed in 0.05 seconds

iOS自动化探索(四)自动化测试框架pytest - 安装和使用的更多相关文章

  1. iOS自动化探索(一)WebDriverAgent安装

    WebDriverAgent FaceBook推出的一款iOS移动测试框架, 支持真机和模拟器, 同时支持USB, 官方是这样介绍的: https://github.com/facebook/WebD ...

  2. python3: 自动化测试框架pytest

    最近在学习web自动化,所以在这里总结一下pytest框架. 其实pytest 和 unittest 都是自动化测试框架,但是pytest更好用一些,有以下几个优点:1)可以根据标签执行用例:2)?? ...

  3. iOS自动化探索(七)自动化测试框架pytest - 测试报告

    这里我们单独来看下关于如何生存测试报告 准备测试代码如下: #coding: utf- import pytest @pytest.fixture() def login(): print '输入账号 ...

  4. iOS自动化探索(六)自动化测试框架pytest - fixtures

    Fixture介绍 fixture是pytest特有的功能,它用pytest.fixture标识,定义在函数前面.在编写测试函数的时候,可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将 ...

  5. iOS自动化探索(五)自动化测试框架pytest - Assert断言的使用

    使用assert语句进行断言 pytest允许使用标准的python assert语法,用来校验expectation and value是否一致 代码演示: def func(): def test ...

  6. iOS自动化探索(八)Mac上的Jenkins安装

    安装Jenkins 首先检查是否有Jenkins依赖的java环境 java -version 出现java version "1.8.xx"说明已经安装了java Jackeys ...

  7. Python接口自动化测试框架: pytest+allure+jsonpath+requests+excel实现的接口自动化测试框架(学习成果)

    废话 最近在自己学习接口自动化测试,这里也算是完成一个小的成果,欢迎大家交流指出不合适的地方,源码在文末 问题 整体代码结构优化未实现,导致最终测试时间变长,其他工具单接口测试只需要39ms,该框架中 ...

  8. 【Mac + Python3.6 + ATX基于facebook-wda】之IOS自动化(一):WebDriverAgent安装

    此篇介绍如何安装WebDriverAgent,下一篇介绍facebook-wda库的安装使用以及自动化脚本的开发. 前言: 对于iOS的应用的测试,如果不需要用到图像识别,推荐使用这个项目facebo ...

  9. iOS自动化探索(十)代码覆盖率统计

    iOS APP代码覆盖率统计 今年Q3季度领导给加了个任务要做前后端代码覆盖率统计, 鉴于对iOS代码代码比较熟就选择先从iOS端入手,折腾一整天后终于初步把流程跑通了记录如下 覆盖率监测的原理 Xc ...

随机推荐

  1. vue事件修饰器

    事件修饰器 Vue.js 为 v-on 提供了 事件修饰符.通过由点(.)表示的指令后缀来调用修饰符.· .stop .prevent .capture .self <div id=" ...

  2. tomcat 配置文件 介绍

    [root@mysql logs]# cd ../conf/ [root@mysql conf]# ll总用量 228drwxr-x---. 3 root root 4096 11月 15 2018 ...

  3. 007-shiro与spring web项目整合【一】基础搭建

    一.需求 将原来基于url的工程改成使用shiro实现 二.代码 https://github.com/bjlhx15/shiro.git 中的permission_shiro 三.去除原项目拦截器 ...

  4. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  5. Tornado的基本知识

    Tornado是FriendReed使用的可扩展的非阻塞式的web服务器及其相关工具的开源版本. 这个框架看起来有些像web.py或者Google的webapp,不过为了能有效利用非阻塞服务器环境,这 ...

  6. samba、nginx服务

    一.部署samba Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成,Samba主要用于Linux或UNIX和Windows系统之间的文件共享. SMB( ...

  7. Canvas:橡皮筋线条绘制

    Canvas:橡皮筋线条绘制 效果演示 实现要点 事件监听 [说明]: 在Canvas中检测鼠标事件是非常简单的,可以在canvas中添加一个事件监听器,当事件发生时,浏览器就会调用这个监听器. 我们 ...

  8. akka框架地址

    http://doc.akka.io/docs/akka/2.2.3/AkkaJava.pdf

  9. Python3.x:Linux下安装python3.6

    Python3.x:Linux下安装python3.6 下载 #先进入download文件夹 cd /home/download #输入命令(下载到当前目录) wget https://www.pyt ...

  10. jQuery动态网址标签

    在线演示 本地下载