python 測試框架之---testtools


在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

至於為什么要使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM