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

  • 相关阅读:
    简单图片预加载
    前端进行图片压缩
    原生js实现拖动滑块验证
    chrome和IE下的滚动条样式修改
    简单canvas刮刮乐
    时间轴
    简单边框动画
    滚动指示器
    美化checkbox多选框
    将过长的文字改用省略号显示
  • 原文地址:https://www.cnblogs.com/whuyt/p/4547604.html
Copyright © 2011-2022 走看看