在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 22, in test_case_2
    assert 2==3
AssertionError

Ran 2 tests in 0.005s
FAILED (failures=1)

在官网中,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 22, in te
st_case_2
    assert 2==3
AssertionError


----------------------------------------------------------------------
Ran 2 tests in 0.012s

FAILED (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 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
View Code

相关文章: