亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2015年04月13日 09:53:55   投稿:junjie  
這篇文章主要介紹了Python單元測試框架unittest使用方法講解,本文講解了unittest概述、命令行接口、測試案例自動(dòng)搜索、創(chuàng)建測試代碼、構(gòu)建測試套件方法等內(nèi)容,需要的朋友可以參考下

概述

1.測試腳手架(test fixture)

測試準(zhǔn)備前要做的工作和測試執(zhí)行完后要做的工作.包括setUp()和tearDown().

2.測試案例(test case)

最小的測試單元.

3.測試套件(test suite)

測試案例的集合.

4.測試運(yùn)行器(test runner)

測試執(zhí)行的組件.

命令行接口

可以用命令行運(yùn)行測試模塊,測試類以及測試方法.

復(fù)制代碼 代碼如下:

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

可加-v打印詳細(xì)信息
復(fù)制代碼 代碼如下:

python -m unittest -v test_module

測試案例自動(dòng)搜索

unittest支持簡單的test discovery. 命令行傳入discovery后,框架會(huì)自動(dòng)在當(dāng)前目錄搜索要測試的案例并執(zhí)行.搜索目錄必須是包或者模塊.基本使用如下:

復(fù)制代碼 代碼如下:

cd project_directory
python -m unittest discover

子選項(xiàng)如下:
-v, –verbose
輸出信息的詳細(xì)級別

-s, –start-directory directory
開始搜索目錄 (默認(rèn)為當(dāng)前目錄)

-p, –pattern pattern
匹配的文件名 (默認(rèn)為test*.py)

-t, –top-level-directory directory
搜索的頂層目錄 (默認(rèn)為start directory)

創(chuàng)建測試代碼

1.方式一

創(chuàng)建子類繼承unittest.TestCase,然后重寫以下方法

復(fù)制代碼 代碼如下:

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

運(yùn)行

2.方式二

編寫以test開頭的方法

復(fù)制代碼 代碼如下:

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

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

    def tearDown(self):
        pass


構(gòu)建測試套件

方式一

復(fù)制代碼 代碼如下:

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

方式二(推薦)

復(fù)制代碼 代碼如下:

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

方式三(推薦)
復(fù)制代碼 代碼如下:

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

方式四

多個(gè)測試套件構(gòu)建成更大的測試套件

復(fù)制代碼 代碼如下:

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

方式五

unittest的TestLoader提供生成默認(rèn)的測試套件

復(fù)制代碼 代碼如下:

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

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

可以分無條件忽略和有條件忽略,通過裝飾器實(shí)現(xiàn)

復(fù)制代碼 代碼如下:

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


測試類也可以忽略
復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評論