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

一、初识

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

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

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

from testtools import TestCase

class 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 == 3 def tearDown(self):
super(TestLearnTesttools, self).tearDown()
print "this is tearDown" @classmethod
def 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 , in test_case_2
assert ==
AssertionError Ran tests in .005s
FAILED (failures=)

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

那下面我们用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 , in te
st_case_2
assert ==
AssertionError ----------------------------------------------------------------------
Ran tests in .012s FAILED (failures=)

感觉还是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 and
tearDown. 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 the
test 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 case
and an optional list of exception handlers.
""" skipException = TestSkipped run_tests_with = RunTest

至于为什么要使用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, WithAttributes

class 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

python 测试框架之---testtools的更多相关文章

  1. 用 Python 测试框架简化测试

    用 Python 测试框架简化测试 摘要:本文将向您介绍了三种流行 Python 测试框架(zope.testing,py.test,nose)的基本特性,并讨论新一代的测试风格. 最近出现了行业级的 ...

  2. 【转载】Python测试框架doctest

    原文在这里 :Python测试框架doctest 先记录一下,直接复制粘贴后,排版是乱的,后续再弄.

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

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

  4. <自动化测试>之<使用unittest Python测试框架进行参数化测试>

    最近在看视频时,虫师简单提到了简化自动化测试脚本用例中的代码量,而python中本身的参数化方法用来测试很糟糕,他在实际操作中使用了parameterized参数化... 有兴趣就查了下使用的方法,来 ...

  5. python 测试框架nose

    python测试框架nose nose不是python自带模块,这里我才用pip的方式安装 pip install nose 这样就完成了安装,然后再确认下是否安装成功了,直接打开cmd输入noset ...

  6. Python测试框架unittest

    Python测试框架unittest 一.unittest框架 (1)unittest特点 1.python自带的单元测试框架,不需要安装 2.用例执行互不干扰 3.提供不同范围的setUp和tear ...

  7. Python测试框架pytest入门基础

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

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

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

  9. 转 python测试框架最全资源汇总

    转自: http://www.testclass.net/list/python_list_1/ xUnit frameworks(单元测试框架) frameworks 框架 unittest - p ...

随机推荐

  1. 【VirtualBox】ubuntu虚拟机与windows设置共享文件夹

    第一步:配置 http://blog.csdn.net/a962804835/article/details/72820355 第二步:解决ubuntu下共享文件夹无访问权限的问题 http://bl ...

  2. 常用Dos(转)

    先介绍一下通配符的概念. 通配符*和? *表示一个字符串 ?只代表一个字符 注意通配符只能通配文件名或扩展名,不能全都表示.例如我们要查找以字母y开头的所有文件,可以输入以下命令:dir y*.*:如 ...

  3. highcharts系列之xAxis

    xAxis定义的是x坐标轴的配置选项.默认情况下,x轴指的是水平轴,特殊指定的时候也可以作为垂直的轴使用,在多坐标系中,xAxis是有多个配置好了的轴object的数组. 下面来看一下,xAxis常用 ...

  4. Java显示指定类型的文件

    文件作为存储数据的单元,会根据数据类型产生很多分类,也就是所谓的文件类型.在对数据文件进行操作时,常常需要根据不同的文件类型来作不同的处理.本实例实现的是读取文件夹指定类型的文件并显示到表格控件中.这 ...

  5. Java实现非法访问异常

    创建ExceptionTest类,在该类的main()方法中,使用反射获得String类的所有域,不要使用setAccessible方法修改这些域的可见性,然后通过反射获得私有域中与”hash”相匹配 ...

  6. nodejs服务器部署教程三

    安装mongodb数据库 如何在ubuntu上安装mongodb数据库,其实官方文档写的很清楚啦 sudo apt-key adv --keyserver hkp://keyserver.ubuntu ...

  7. ArcGIS应用

    1.ArcGIS Server发布资源报错:网络资源问题 有可能是跟网络相关的服务没有开启,开启相关服务器后有可能可以解决此问题. 还有可能通过此法解决:开始--控制面板--网络和共享中心--高级共享 ...

  8. bing搜索引擎子域名收集(Python脚本)

    利用bing搜索引擎进行子域名收集,编写了一个简单的Python脚本,分享一下. #! /usr/bin/env python # _*_ coding:utf-8 _*_ import reques ...

  9. Nginx(六)-- 配置文件之Gzip

    1.概念及作用 Gizp主要对内容.静态文件做压缩,用来提升网站访问速度,节省带宽. 2.使用方法 gzip既可以配置在server中,也可以配置在server外,此处配置在server中,如下:   ...

  10. ubuntu11.10 64bit 编译android 4.0

    前言: 据说google内部使用的的ubuntu版本始终是10.4, 而我一直使用的编译2.3Gingerbread的 11.04下补充安装uuid-dev和liblzo2-dev两个库就可以直接编译 ...