在tempest框架中,使用的是testtools为基础框架来运行接口自动化

一、初识

testools是属于python中诸多自动化框架中的一个,官方文档如下:

http://testtools.readthedocs.io/en/latest/overview.html

但是,官方中的例子还有一个myproject基础文件,让很多同学很困惑,下面我们看看使用testtool最简单的框架,如下

from testtools import TestCaseclass TestLearnTesttools(TestCase):def setUp(self):super(TestLearnTesttools, self).setUp()print "this is setUp"def test_case_1(self):self.assertIn('a', 'cat')def test_case_2(self):assert 2 == 3def tearDown(self):super(TestLearnTesttools, self).tearDown()print "this is tearDown"@classmethoddef setUpClass(cls):print "this is setUp class"

注意,setUp必须写super,不然会报如下错误

TestCase.setUp was not called. Have you upcalled all the way up the hierarchy fr
om your setUp? e.g. Call super(TestLearnTesttools, self).setUp() from your setUp
().

不然你就不要写setUp,这点感觉这个框架好奇葩...

运行结果如下:

E:\workspace\test_case>python -m testtools.run testtools_learn.py
Tests running...
this is setUp class
this is setUp
this is tearDown
this is setUp
this is tearDown
======================================================================
FAIL: testtools_learn.TestLearnTesttools.test_case_2
----------------------------------------------------------------------
Traceback (most recent call last):File "testtools_learn.py", line 22, in test_case_2assert 2==3
AssertionErrorRan 2 tests in 0.005s
FAILED (failures=1)

在官网中,testtool可以使用很多种方式来运行,直接贴过来吧

  • testrepository
  • Trial
  • nose
  • unittest2
  • zope.testrunner (aka zope.testing)

那下面我们用nose来运行一下看看:

E:\workspace\test_case>nosetests -s -v testtools_learn.py
ALL starting...
this is setUp class
test_case.testtools_learn.TestLearnTesttools.test_case_1 ... this is setUp
this is tearDown
ok
test_case.testtools_learn.TestLearnTesttools.test_case_2 ... this is setUp
this is tearDown
FAIL======================================================================
FAIL: test_case.testtools_learn.TestLearnTesttools.test_case_2
----------------------------------------------------------------------
_StringException: Traceback (most recent call last):File "E:\workspace\nosetest_lear\test_case\testtools_learn.py", line 22, in te
st_case_2assert 2==3
AssertionError----------------------------------------------------------------------
Ran 2 tests in 0.012sFAILED (failures=1)

感觉还是nose的格式好看。

二、了解

既然知道testtool是怎么写及怎么运行了,我们来仔细看看,testtools到底是个什么东东

还是看官网,可以得知道,testtools是python标准库中unittest的扩展,从testtools源码中可以看到,继承的还是unittest2.TestCase

class TestCase(unittest.TestCase):"""Extensions to the basic TestCase.:ivar exception_handlers: Exceptions to catch from setUp, runTest andtearDown. This list is able to be modified at any time and consists of(exception_class, handler(case, result, exception_value)) pairs.:ivar force_failure: Force testtools.RunTest to fail the test after thetest has completed.:cvar run_tests_with: A factory to make the ``RunTest`` to run tests with.Defaults to ``RunTest``.  The factory is expected to take a test caseand an optional list of exception handlers."""skipException = TestSkippedrun_tests_with = RunTest

View Code

至于为什么要使用Testtools,官网上也说了,

1、有更好的断言方式

testtool加了assertInassertIsassertIsInstance及其它的。

虽然unittest标准库中也有这3个方式,但这3个方式是python 2.7之后才有的

具体testtool的断言优化可以到这里testtools.assertions查询

特别用法举例,来源网上

assertThat

def test_abs(self):result = abs(-7)self.assertEqual(result, 7)self.assertNotEqual(result, 7)self.assertThat(result, testtools.matchers.Equals(7))self.assertThat(result, testtools.matchers.Not(Equals(8)))

expectFailure

def test_expect_failure_example(self):self.expectFailure("cats should be dogs", self.assertEqual, 'cats', 'dogs')

2、更多调试手段

在测试过程中,如果你想得到更多的错误信息,可以使用TestCase.addDetail

TestCase.addDetail具体使用这里不写,感觉没用

3、fixtures

测试用例一般都包含一个 setUp 方法来准备测试环境,主要是生成一些测试过程中

会用到的变量与对象。有时候我们会需要在不同的测试用例间共享这些变量与对象,避免

在每一个测试用例中重复定义。所以就有了 fixtures,它制定了一套规范,将测试代码与

环境准备代码分离开来,使得测试代码亦可方便的组织和扩展。

class SomeTest(TestCase):def setUp(self):super(SomeTest, self).setUp()self.server = self.useFixture(Server())

4、Skipping tests

检测到环境不符合要求的情况下,忽略某些测试

def test_make_symlink(self):symlink = getattr(os, 'symlink', None)if symlink is None:self.skipTest("No symlink support")symlink(whatever, something_else)

5、Test attributes

标签,与nose框架中的attr一样的道理,testtool中同样支持,使用时使用的参数是--load-list

from testtools.testcase import attr, WithAttributesclass AnnotatedTests(WithAttributes, TestCase):@attr('simple')def test_one(self):pass@attr('more', 'than', 'one')def test_two(self):pass@attr('or')@attr('stacked')def test_three(self):pass

6、自定义的错误输入

self.exception_handlers.insert(-1, (ExceptionClass, handler)).

这样,在任何一个setUp teardown或测试用例中raise ExceptionClass都会调用

更多的用法请查阅framework folk

转载于:https://www.cnblogs.com/landhu/p/6770080.html

python 测试框架之---testtools相关推荐

  1. Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture

    Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)PyCharm设 ...

  2. Python测试框架pytest(04)fixture - 测试用例调用fixture、fixture传递测试数据

    Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)PyCharm设 ...

  3. Python测试框架pytest(03)setup和teardown

    Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 ​​​​​​Python测试框架pytest(02)Py ...

  4. python测试框架untest_Python测试框架之unittest和pytest

    目前搜狗商城接口测试框架用的是unittest+HTMLTestRunner,case数有1097条,目前运行一次自动化测试,时长约为30分钟,期望控制在10分钟或者更短的时间内.近期打算重新优化框架 ...

  5. Python测试框架之pytest详解

    目录 前言 1.pytest安装 2.Pytest的setup和teardown函数 3.Pytest配置文件 4 Pytest常用插件 4.1 前置条件: 4.2 Pytest测试报告 5.pyte ...

  6. gtest测试框架使用详解_【python】新手小白必看,教你如何使用全功能Python测试框架 - python秋枫...

    大家好,我是在升职加薪道路上越奋斗头发越少的阿茅. 今天来跟想入门还徘徊在门外的小白们聊一聊 1.安装和简单使用 2.配置文件 3.断言 一. 第1步 (安装和简单使用) pytest是一个非常成熟的 ...

  7. Python测试框架Pytest的基础入门

    Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...

  8. 全功能Python测试框架:pytest

    python通用测试框架大多数人用的是unittest+HTMLTestRunner,这段时间看到了pytest文档,发现这个框架和丰富的plugins很好用,所以来学习下pytest. pytest ...

  9. 收藏清单: python测试框架最全资源汇总

    xUnit frameworks 单元测试框架 frameworks 框架 unittest - python自带的单元测试库,开箱即用 unittest2 - 加强版的单元测试框架,适用于Pytho ...

最新文章

  1. apache 编译安装php mysql_编译安装APACHE+PHP+MYSQL
  2. 在使用win 7 无线承载网络时,启动该服务时,有时会提示:组或资源的状态不是执行请求操作的正确状态。 网上有文章指出,解决这个问题的方法是在设备管理器中启动“Microsoft托管网络虚拟适配
  3. if和case用法比较
  4. 2020下半年新机最新消息_三星小米华为苹果纷纷曝光高端机,这么多你选择谁?...
  5. PostgreSQL数据库密码
  6. Recurrent Neural Network系列1--RNN(循环神经网络)概述
  7. Vue 优雅地使用 WebSocket
  8. n平方的求和公式_极限求解--数列前n项和公式推导(补充知识)
  9. vb.net 教程 6-14 终止线程的例子
  10. WDF基本对象和句柄定义
  11. 线性代数学习笔记——第五十七讲——特征子空间
  12. h61 nvme硬盘_切割SN520amp;amp;对比主流NVME2242amp;amp;无硬盘盒迁移系统
  13. 【华为OJ】【042-矩阵乘法】
  14. 查询快递单号物流,筛选出代收的单号
  15. 电影《面包店的女孩+苏姗娜的故事》观后感
  16. PHP商城笔记137 —— 商城项目知识点
  17. Android微信抢红包插件原理和实现 适配微信6.6.1版本
  18. 机器学习之西瓜书绪论--关于机器学习的简单介绍
  19. cad怎么转换成pdf格式?
  20. Zabbix 实现简单的WEN监测

热门文章

  1. Lottie开源动画库
  2. RHEL5.3下搭建LAMP+Django环境(二)
  3. 从全球最大光伏展看中国光伏行业:火爆的背后是什么?
  4. node js npm 和 cnpm的使用
  5. springmvc4之mvc:exclude-mapping path= /拦截配置
  6. 阿里感悟(十三)降低成本的敏捷设计
  7. linux shell 报错 Syntax error: Bad for loop variable
  8. vsftp 550,227 报错解决
  9. 不喜欢冷漠,喜欢笑容、热情和拥抱
  10. mysql正在加载_本地坏境或者服务器环境下phpmyadmin出现始终正在加载问题的解决方法...