[转]Python测试框架对比----unittest, pytest, nose, robot framework对比

 

 
测试框架

什么是框架?

框架(Framework)是整个或部分系统的可重用设计,
框架是用来解决代码的组织及运行控制问题的。html

在咱们编写自动化脚本的时候,常常须要读取配置文件,读取数据文件,发送请求,记录日志,链接并对比数据库数据。每一个脚本里都重写一遍各类方法不只工做量大并且易错。因此咱们须要把公共的方法提取出来,封装成单独的模块,放到公用方法包里。另外配置文件,数据文件,日志等咱们也须要分类存到不一样的文件夹下。这种对公共方法的封装及对脚本及配置文件怎么组织的设计就叫作框架。python

同时,通常框架除了完成对代码及配置文件的组织以外还要提供运行的控制功能。好比批量执行,分模块执行,生成报告,异常处理等等。web

总结为如下3点:数据库

  • 封装公共方法
  • 代码及配置文件的组织
  • 执行控制

什么是测试框架?

一个完整的测试脚本(用例)通常包含如下几个步骤:session

  • 环境准备或检查
  • 执行业务操做
  • 断言结果
  • 清理环境

而测试框架通常还要完成用例加载,批量执行,异常控制,结果输出等功能。基础的测试框架通常只提供执行控制方面的功能。多线程

测试框架应具备的特色

  • 易用性:编写用例,执行用例,生成报告及定位问题方便
  • 健壮性:稳定,好比timeout机制等
  • 扩展性:插件
  • 灵活性:用例组织或执行的灵活性,Fixture功能(不一样范围的setUp和tearDown)等
  • 定制性:二次开发方便

unittest,pytest,nose,robot framework简介

  • unittest: Python自带,最基础的单元测试框架
  • nose: 基于unittest开发,易用性好,有许多插件
  • pytest: 一样基于unittest开发,易用性好,信息更详细,插件众多
  • robot framework:一款基于Python语言的关键字驱动测试框架,有界面,功能完善,自带报告及log清晰美观
项目 unittest nose pytest robot framework
用例编写 继承unittest.TestCase类须要组织各类testSuite断言种类繁多 test开头的方法便可 test开头的方法即 可 robot格式,文本文件
执行器 本身写run_all_tests+discover+CommandParser+... nosetests ... py.test ... pybot ...
用例发现Discover 支持 支持 支持 支持
跳过用例 unittest.skip()unittest.skipIf()raise uniitest.SkipTest from nose.plugins.skip import SkipTestraise SkipTest @pytest.mark.skipif( condition)@pytest.mark.xfail -
Fixtures setUp/tearDown@classmethodsetUpClass... 支持 @pytest.fixture(session="session", autouse=True)fixture的做用域:function、module、session ,autouse=True使得函数将默认执行 [Setup] ...[Teardown] ...
用例标签tags 借助unittest.skip()+comandParser实现 attrib标签from nose.plugins.attrib import attr@attr(speed='slow')def test_big_download(): pass$ nosetests -a speed=slow @pytest.mark.webtest自定义一个mark,以下,而后 py.test -v -m webtest 只运行标记了webtest的函数, py.test -v -m "not webtest" 来运行未标记webtest的 [Tags] test level1pybot -i/--include tagName C:\TF-Testpybot -e/--exculde level1 *.robot排除
超时机制Timeout 本身实现 from nose.tools import timedimport time@timed(1)def test_lean_5():time.sleep(2)pass pip install pytest-timeout@pytest.mark.timeout(60)或 pytest --timeout=300 [Timeout] 3 seconds
参数化 结合ddt使用 结合ddt使用 @pytest.mark.parametrize("a,b,expected", testdata)def test_timedistance_v0(a, b, expected):diff = a - bassert diff == expected [Template] 1 2 3
报告 HTMLTestRunner pip install nose-htmloutput--with-html --html-file= pip install -U pytest-htmlpy.test --html=./report.html 支持,默认自动生成
日志log 本身实现 --nologcapture 不使用log--logging-format=FORMAT使用自定义的格式显示日志--logging-datefmt=FORMAT 和上面类相似,多了日期格式--logging-filter=FILTER日志过滤,通常不多用,能够不关注--logging-clear-handlers 也能够不关注--logging-level=DEFAULT log的等级定义 pytest test_add.py --resultlog=./log.txtpytest test_add.py --pastebin=all 支持,默认自动生成
只列出用例collect-only nosetests --collect-onlynosetests -v --with-id --collect-only -v -
失败用例重跑rerun failures nosetests -v --failed pip install -U pytest-rerunfailures@pytest.mark.flaky(reruns=5)py.test --rerun=3 robot --rerunfailed
baseline对比
并发 改造unittest使用协程并发,或使用线程池+Beautiful Report 命令行并发 pytest-xdist:分发到不用的cpu或机器上 命令行并发
xml报告 --with-xunit --xunit-file=... /pytest+Allure --junit-xml=
Selenium支持 pytest-selenium robotframework-seleniumlibraryrobotframwork-selenium2library

总结:整体来讲,unittest比较基础,二次开发方便,适合高手使用;pytest/nose更加方便快捷,效率更高,适合小白及追求效率的公司;robot framework因为有界面及美观的报告,易用性更好,灵活性及可定制性略差。并发

参考框架设计方案

unittest框架

888米兔: python+requests接口自动化测试框架实例详解教程函数

上海一悠悠: selenium+python自动化91-unittest多线程生成报告(BeautifulReport)单元测试

严北: Python - 用协程并发执行测试用例

pytest

晶莹的营: pytest学习笔记(一)
晶莹的营: pytest学习笔记(二)
晶莹的营: pytest学习笔记(三)

liuchunming033: 用Pytest+Allure生成漂亮的HTML图形化测试报告

相关文章
相关标签/搜索