Python單元測試架構unittest使用方法講解

來源:互聯網
上載者:User
概述

1.測試腳手架(test fixture)

測試準備前要做的工作和測試執行完後要做的工作.包括setUp()和tearDown().

2.測試案例(test case)

最小的測試單元.

3.測試套件(test suite)

測試案例的集合.

4.測試回合器(test runner)

測試執行的組件.

命令列介面

可以用命令列運行測試模組,測試類別以及測試方法.

代碼如下:


python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method


可加-v列印詳細資料

代碼如下:


python -m unittest -v test_module


測試案例自動搜尋

unittest支援簡單的test discovery. 命令列傳入discovery後,架構會自動在目前的目錄搜尋要測試的案例並執行.搜尋目錄必須是包或者模組.基本使用如下:

代碼如下:


cd project_directory
python -m unittest discover


子選項如下:
-v, –verbose
輸出資訊的詳細資訊層級

-s, –start-directory directory
開始搜尋目錄 (預設為目前的目錄)

-p, –pattern pattern
匹配的檔案名稱 (預設為test*.py)

-t, –top-level-directory directory
搜尋的頂層目錄 (預設為start directory)

建立測試代碼

1.方式一

建立子類繼承unittest.TestCase,然後重寫以下方法

代碼如下:


class WidgetTestCase(unittest.TestCase):
def setUp(self):
pass
def runTest(self):
pass
def tearDown(self):
pass

運行

2.方式二

編寫以test開頭的方法

代碼如下:


class WidgetTestCase(unittest.TestCase):
def setUp(self):
pass

def test_xx1(self)
def test_xx2(self)
...
def test_xxN(self)

def tearDown(self):
pass


構建測試套件

方式一

代碼如下:


widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))

方式二(推薦)

代碼如下:


def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_size'))
suite.addTest(WidgetTestCase('test_resize'))
return suite


方式三(推薦)

代碼如下:


def suite():
tests = ['test_default_size', 'test_resize']
return unittest.TestSuite(map(WidgetTestCase, tests))


方式四

多個測試套件構建成更大的測試套件

代碼如下:


suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])


方式五

unittest的TestLoader提供產生預設的測試套件

代碼如下:


suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)

忽略測試案例( Python2.7支援)

可以分無條件忽略和有條件忽略,通過裝飾器實現

代碼如下:


class MyTestCase(unittest.TestCase):

@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")

@unittest.skipIf(mylib.__version__ < (1, 3),
"not supported in this library version")
def test_format(self):
# Tests that work for only a certain version of the library.
pass

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass


測試類別也可以忽略

代碼如下:


@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
def test_not_run(self):
pass

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.