简体   繁体   English

将python Mockito与python unittest集成

[英]Integrating python mockito with python unittest

I am integrating mockito with python unittest. 我正在将Mockito与python unittest集成。 I am new to unittest but certainly not new to unit testing paradigm. 我是单元测试的新手,但对单元测试范式当然不是新手。 I wanted to use mockito for stubbing and mocking the classes. 我想使用Mockito来存根和模拟类。 I could not find any good documentation on mockito python-unittest integrations and usage. 我找不到关于Mockito python-unittest集成和用法的任何好的文档。

Is mockito best way forward to it? Mockito是实现它的最佳方式吗?

what are its implications? 它的含义是什么?

Can any one guide me in this? 有人可以指导我吗?

I found the documentation for mock easier to understand than for mockito. 我发现模拟文档比模拟文档更容易理解。 And it is part of the standard library now. 并且它现在是标准库的一部分。

The python mockito lilbrary is trying to remodel the Java mockito library. python模拟库库正在尝试重塑Java模拟库。 So it is useful for people who work with Java and with Python at the same time tp make it easier. 因此,对于同时使用Java和Python的人来说,这很有用。 If you do not work with Java mockito or not anymore I see no reason to go that path. 如果您不再使用Java模型,或者不再使用Java模型,那么我就没有理由走这条路。

Probably your answer ist to use mock, because you are asking "Is mockito best way forward to it?" 您的答案可能是使用模拟,因为您在问“模拟的最佳方式吗?”

From the documentation: http://www.voidspace.org.uk/python/mock/getting-started.html#using-mock 从文档中: http : //www.voidspace.org.uk/python/mock/getting-started.html#using-mock


The simple ProductionClass below has a closer method. 下面的简单ProductionClass具有更接近的方法。 If it is called with an object then it calls close on it. 如果用一个对象调用它,那么它将在其上调用close。

>>> class ProductionClass(object):
...     def closer(self, something):
...         something.close()
...

So to test it we need to pass in an object with a close method and check that it was called correctly. 因此,要测试它,我们需要使用close方法传入一个对象,并检查它是否被正确调用。

>>> real = ProductionClass()
>>> mock = Mock()
>>> real.closer(mock)
>>> mock.close.assert_called_with()

Often you want to track more than a single call to a method. 通常,您不仅要跟踪对方法的单个调用。 The mock_calls attribute records all calls to child attributes of the mock - and also to their children. mock_calls属性记录对模拟对象的子属性的所有调用,以及对子对象的所有调用。

>>> mock = MagicMock()
>>> mock.method()
<MagicMock name='mock.method()' id='...'>
>>> mock.attribute.method(10, x=53)
<MagicMock name='mock.attribute.method()' id='...'>
>>> mock.mock_calls
[call.method(), call.attribute.method(10, x=53)]

If you make an assertion about mock_calls and any unexpected methods have been called, then the assertion will fail. 如果您对模拟调用做出断言,并且调用了任何意外方法,则断言将失败。 This is useful because as well as asserting that the calls you expected have been made, you are also checking that they were made in the right order and with no additional calls: 这很有用,因为除了断言您所期望的呼叫已经发出之外,您还要检查它们是否以正确的顺序进行并且没有其他呼叫:

You use the call object to construct lists for comparing with mock_calls: 您可以使用call对象来构造用于与模拟_调用进行比较的列表:

>>> expected = [call.method(), call.attribute.method(10, x=53)]
>>> mock.mock_calls == expected
True

Setting the return values on a mock object is trivially easy: 在模拟对象上设置返回值非常容易:

>>> mock = Mock()
>>> mock.return_value = 3
>>> mock()
3

Of course you can do the same for methods on the mock: 当然,您可以对模拟方法执行相同的操作:

>>> mock = Mock()
>>> mock.method.return_value = 3
>>> mock.method()
3

The return value can also be set in the constructor: 还可以在构造函数中设置返回值:

>>> mock = Mock(return_value=3)
>>> mock()
3

If you need an attribute setting on your mock, just do it: 如果您需要在模拟游戏中设置属性,只需执行以下操作:

>>> mock = Mock()
>>> mock.x = 3
>>> mock.x
3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM