• 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

  • 相关阅读:
    python的lambda使用
    python的timedelta
    ptyhon读文件一行长度len为1022,出现\x00
    eclipse生成jar包
    linux 文件的软链接与硬链接(看着写的不错就转发了)
    各种VBA excel 命令、属性、方法
    键盘鼠标共享 Synergy
    关于 range 区域 excel
    php mssql几条常见的数据库分页 SQL 语句
    Get the Degree of Angle Between Hour and Minute Hand of a Clock at Anytime
  • 原文地址:https://www.cnblogs.com/whuyt/p/4547604.html
Copyright © 2020-2023  润新知