python之測試

來源:互聯網
上載者:User

標籤:

unittest

 

Test outcomes

Tests have 3 possible outcomes:

ok
The test passes.

FAIL
The test does not pass, and raises an AssertionError exception.

ERROR
The test raises an exception other than AssertionError.

 

 

Asserting Truth

class TruthTest(unittest.TestCase):    def test_fail_unless(self):        self.failUnless(True)    def test_assert_true(self):        self.assertTrue(True)    def test_fail_if(self):        self.failIf(False)    def test_assert_fail(self):        self.assertFalse(False)

 

Testing Equality

class EqualityTest(unittest.TestCase):    def test_equal(self):        self.failUnlessEqual(1, 3-2)    def test_not_equal(self):        self.failIfEqual(2, 3-2)

 

Almost Equal

class AlmostEqualTest(unittest.TestCase):    def test_almost_equal(self):        self.failUnlessAlmostEqual(1.1, 1.2, places=0)    def test_not_almost_equal(self):        self.failIfAlmostEqual(1.1, 1.2, places=1)

 

Testing for Exceptions

def raises_error(*args, **kwargs):    print args, kwargs    raise ValueError(‘Invalid value:‘ + str(args) + str(kwargs))class ExceptionTest(unittest.TestCase):    def test_fail_unless_raises(self):        self.failUnlessRaises(ValueError, raises_error, ‘a‘, b=‘c‘)

 

Test Fixtures

Fixtures are resources needed by a test.
For example, if you are writing several tests for the same class, those tests all need an instance of that class to use for testing.
Other test fixtures include database connections and temporary files.
TestCase includes a special hook to configure and clean up any fixtures needed by your tests.
To configure the fixtures, override setUp(). To clean up, override tearDown().

class FixturesTest(unittest.TestCase):    def setUp(self):        print ‘In setUp‘        self.fixture = range(1, 10)    def tearDown(self):        print ‘in tearDown‘        del self.fixture    def test_fixtures(self):        print ‘in test‘        self.failUnlessEqual(self.fixture, range(1, 10))

 

 

 

 

Mocks

Mocking is primarily used in unit testing.

An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the the real objects are impractical to incorporate into the unit test.

In short, mocking is creating objects that simulate the behavior of real objects.

 

Google    PyCon US 2014 Montreal  Ned Batchelder - Getting Started Testing

 

 

 

 2015-06-02

python之測試

相關文章

聯繫我們

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