python mock 使用

python中mock数据非常简单

例如:

class Human(object):
    def __init__(self, age, gender):
        self.age = age
        self.gender = gender

 我们有一个human类

那么如果想mock一个数据那么就

1 from mock import Mock
2 
3 class MockDemoTest(unittest.TestCase):
4     def setUp(self):
5         pass
6 
7     def test_mock_obj(self):
8         human = Mock(Human)
9         self.assertIsInstance(human, Human)

然后运行测试用例就可以看到可以成功的做了一个假数据

如果需要调用就patch一个

猜你喜欢

转载自www.cnblogs.com/bufubaoni/p/9857346.html