天天看點

python 自動化架構_python自動化測試(3)- 自動化架構及工具

3 基本示例

如下示例也來自于官方文檔 basic_demo.py:

# coding:utf-8

"""

基本的自動化測試腳本 basic_demo.py

"""

__author__ = 'zheng'

import unittest

class TestStringMethods(unittest.TestCase):

def setUp(self):

print 'init by setUp...'

def tearDown(self):

print 'end by tearDown...'

def test_upper(self):

self.assertEqual('foo'.upper(), 'FOO')

def test_isupper(self):

self.assertTrue('FOO'.isupper())

self.assertFalse('Foo'.isupper())

self.assertTrue('Foo'.isupper())

def test_split(self):

s = 'hello world'

self.assertEqual(s.split(), ['hello', 'world'])

# check that s.split fails when the separator is not a string

with self.assertRaises(TypeError):

s.split(2)

if __name__ == '__main__':

unittest.main()

雖然官方文檔裡面介紹了幾種組織測試用例腳本的方式:

獨立測試函數

單用例測試類

多用例測試類

不同的編寫形态,會有不同的組織方式,具體的可以看官方文檔。本文作者研究過官方文檔後,最喜歡第三種方式 多用例測試類,也就是上面基本示例的方式,這種方式具有如下特點:

測試類 繼承于 unittest.TestCase

一個測試類可以管理多個 測試腳本函數

測試腳本函數名稱需要以 test_ 開頭

一個測試類裡面的所有的測試函數共享 setUp和tearDown函數

在控制台中運作此程式:

➜ src git:(master) ✗ python basic_demo.py

init by setUp...

Fend by tearDown...

init by setUp...

end by tearDown...

.init by setUp...

end by tearDown...

.

======================================================================

FAIL: test_isupper (__main__.TestStringMethods)

----------------------------------------------------------------------

Traceback (most recent call last):

File "basic_demo.py", line 24, in test_isupper

self.assertTrue('Foo'.isupper())

AssertionError: False is not true

----------------------------------------------------------------------

Ran 3 tests in 0.001s

FAILED (failures=1)

➜ src git:(master) ✗

前面的基本例子的 main 函數采用的最簡單的方式,直接運作所有的測試用例,并生成預設的文本報告。其實隻需要對調用函數做一些簡單的修改,可以将這些測試用例進行合理組織,并擷取其實有用的資料資訊,以便和資訊系統進行內建,形成較好的擴充。

if __name__ == '__main__':

# unittest.main()

# 裝載測試用例

test_cases = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)

# 使用測試套件并打包測試用例

test_suit = unittest.TestSuite()

test_suit.addTests(test_cases)

# 運作測試套件,并傳回測試結果

test_result = unittest.TextTestRunner(verbosity=2).run(test_suit)

#生成測試報告

print("testsRun:%s" % test_result.testsRun)

print("failures:%s" % len(test_result.failures))

print("errors:%s" % len(test_result.errors))

print("skipped:%s" % len(test_result.skipped))

運作後生成的輸出為:

➜ src git:(master) ✗ python basic_demo.py

test_isupper (__main__.TestStringMethods) ... init by setUp...

FAIL

end by tearDown...

test_split (__main__.TestStringMethods) ... init by setUp...

end by tearDown...

ok

test_upper (__main__.TestStringMethods) ... init by setUp...

end by tearDown...

ok

======================================================================

FAIL: test_isupper (__main__.TestStringMethods)

----------------------------------------------------------------------

Traceback (most recent call last):

File "basic_demo.py", line 23, in test_isupper

self.assertTrue('Foo'.isupper())

AssertionError: False is not true

----------------------------------------------------------------------

Ran 3 tests in 0.001s

FAILED (failures=1)

testsRun:3

failures:1

errors:0

skipped:0

顯然上面的輸入結果已經将測試的結果進行了統計,這些資料都是一次測試活動中的重要名額,這些資料可以入庫,和測試資訊管理系統內建,後期生成儀表盤或者統計報表,形成穩定和産品測試線路圖,這些都是和開發相關的了,在此不再多叙述了。

結合上面的具體例子,我們也可以找到上一節的理論部分對應的具體實作對象:

測試裝置(test fixture)由setUp函數來做初始化工作,由tearDown做銷毀工作

測試用例(test case)對應TestCase類,或者更細化的對應裡面的測試腳本函數

測試套件(test suite)對應TestSuite類

測試執行器(test runner)對應TextTestRunner類