zoukankan      html  css  js  c++  java
  • python之测试

    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

  • 相关阅读:
    Android Studio安装教程
    使用CDN对动态网站内容加速有效果吗
    opencms 安装出现以下的问题:Your 'max_allowed_packet' variable is set to less than 16777216 Byte (16MB).
    MySQL之常见问题总结
    UFLDL教程(一)---稀疏自编码器
    Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.2
    自己定义html中a标签的title提示tooltip
    oracle数据库导入导出
    three.js 源代码凝视(十五)Math/Plane.js
    Android命令行下蓝牙使用
  • 原文地址:https://www.cnblogs.com/whuyt/p/4547604.html
Copyright © 2011-2022 走看看