PYTHON單元測試模組unittest

來源:互聯網
上載者:User

一些基本概念

test fixture
    A test fixture represents the preparation needed to perform one or moretests, and any associate cleanup actions. This may involve, for example,creating temporary or proxy databases, directories, or starting a serverprocess.
test case
    A test case is the smallest unit of testing. It checks for a specificresponse to a particular set of inputs. unittest provides a base class,TestCase, which may be used to create new test cases.
test suite
    A test suite is a collection of test cases, test suites, or both. It isused to aggregate tests that should be executed together.
test runner

    A test runner is a component which orchestrates the execution of testsand provides the outcome to the user. The runner may use a graphical interface,a textual interface, or return a special value to indicate the results ofexecuting the tests.

下面是簡單的一個例子

#Rectangle.pyclass Rectangle:    def __init__(self,length,width):        self.length = length        self.width = width            def girth(self):        return 2*(self.length+self.width)        def area(self):        return self.length*self.width#pytest.pyfrom Rectangle import Rectangleimport unittestclass RectangleTestCase(unittest.TestCase):    def setUp(self):        self.rectangle = Rectangle(10,5)         def tearDown(self):        self.rectangle = None       def testGirth(self):        self.assertEqual(self.rectangle.girth(), 30)       def testArea(self):        self.assertEqual(self.rectangle.area(), 100)def suite():    suite = unittest.TestSuite()    suite.addTest(RectangleTestCase("testGirth"))    suite.addTest(RectangleTestCase("testArea"))    return suiteif __name__ == "__main__":       unittest.TextTestRunner().run(suite())   

運行結果如下

joe@joe:/mnt/share$ python pytest.py
.F
======================================================================
FAIL: testArea (__main__.RectangleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "pytest.py", line 15, in testArea
    self.assertEqual(self.rectangle.area(), 100)
AssertionError: 50 != 100

----------------------------------------------------------------------
Ran 2 tests in 0.007s

FAILED (failures=1)

可以看到提示有一個失敗,因為在算面積的時候不正確,應該是50才對,把pytest.py的內容改一下

def testArea(self):        self.assertEqual(self.rectangle.area(), 50)

再跑一遍試試

joe@joe:/mnt/share$ python pytest.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

這下就OK了,沒有錯誤。

下面是一些相關資料:

PYTHON官方文檔

Python Unit Testing Framework

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.