草庐IT

mocking-stubbing

全部标签

Android单元测试系列(3)-Mock之Mockito

目录一、官网二、Demo示例1.目录结构2.被测试的类3.测试类三、Mockito方法说明1.mock对象创建2.Mockito框架中的常见方法说明2.1常见的打桩方法2.2常见的验证行为2.3其他方法 3.Mockito的局限性一、官网Mockito:https://github.com/mockito/mockitoMockito(Mockito4.4.0API)为什么要用mock:解决测试类对其他类的依赖。在实际的测试过程中,有些需要被测试的方法对其他类对象或变量有依赖,如果不初始化的话,很容易出现NP导致无法顺利的继续测试,这个时候就需要mock这些对象来解决了。二、Demo示例参考A

python - isinstance 和 Mocking

classHelloWorld(object):defsay_it(self):return'HelloIamHelloWorld'defi_call_hello_world(hw_obj):print'here...checktype:%s'%type(HelloWorld)ifisinstance(hw_obj,HelloWorld):printhw_obj.say_it()frommockimportpatch,MagicMockimportunittestclassTestInstance(unittest.TestCase):@patch('__main__.HelloWor

python - isinstance 和 Mocking

classHelloWorld(object):defsay_it(self):return'HelloIamHelloWorld'defi_call_hello_world(hw_obj):print'here...checktype:%s'%type(HelloWorld)ifisinstance(hw_obj,HelloWorld):printhw_obj.say_it()frommockimportpatch,MagicMockimportunittestclassTestInstance(unittest.TestCase):@patch('__main__.HelloWor

python - 使用unittest.mock.patch时,为什么autospec默认不是True?

当您使用mock修补函数时,您可以选择将autospec指定为True:Ifyousetautospec=Truethenthemockwithbecreatedwithaspecfromtheobjectbeingreplaced.Allattributesofthemockwillalsohavethespecofthecorrespondingattributeoftheobjectbeingreplaced.MethodsandfunctionsbeingmockedwillhavetheirargumentscheckedandwillraiseaTypeErrorifthe

python - 使用unittest.mock.patch时,为什么autospec默认不是True?

当您使用mock修补函数时,您可以选择将autospec指定为True:Ifyousetautospec=Truethenthemockwithbecreatedwithaspecfromtheobjectbeingreplaced.Allattributesofthemockwillalsohavethespecofthecorrespondingattributeoftheobjectbeingreplaced.MethodsandfunctionsbeingmockedwillhavetheirargumentscheckedandwillraiseaTypeErrorifthe

python - 为迭代定制 unittest.mock.mock_open

我应该如何自定义unittest.mock.mock_open来处理这段代码?file:impexpdemo.pydefimport_register(register_fn):withopen(register_fn)asf:return[lineforlineinf]我的第一次尝试尝试了read_data。classTestByteOrderMark1(unittest.TestCase):REGISTER_FN='test_dummy_path'TEST_TEXT=['testtext1\n','testtext2\n']deftest_byte_order_mark_absen

python - 为迭代定制 unittest.mock.mock_open

我应该如何自定义unittest.mock.mock_open来处理这段代码?file:impexpdemo.pydefimport_register(register_fn):withopen(register_fn)asf:return[lineforlineinf]我的第一次尝试尝试了read_data。classTestByteOrderMark1(unittest.TestCase):REGISTER_FN='test_dummy_path'TEST_TEXT=['testtext1\n','testtext2\n']deftest_byte_order_mark_absen

Java 如何 Mock FileInputStream

前言:        最近在写UT(单元测试)的过程中,遇到需要Mock出FileInputStream的情况,在这里分享一下自己的解决方案。需要Mock的类:publicclassClass1{publicClass1(){}publicbooleanmethod1(){try{FileInputStreamfileInputStream=newFileInputStream("file.txt");}catch(FileNotFoundExceptione){e.printStackTrace();returnfalse;}returntrue;}}测试类如下:@RunWith(Power

python - 访问实例属性时修补类会产生 "AttributeError: Mock object has no attribute"

问题将mock.patch与autospec=True一起使用来修补类不会保留该类实例的属性。详情我正在尝试测试一个类Bar,它将类Foo的实例实例化为名为foo的Bar对象属性.被测的Bar方法叫做bar;它调用属于Bar的Foo实例的方法foo。在测试这一点时,我正在模拟Foo,因为我只想测试Bar是否正在访问正确的Foo成员:importunittestfrommockimportpatchclassFoo(object):def__init__(self):self.foo='foo'classBar(object):def__init__(self):self.foo=Foo

python - 访问实例属性时修补类会产生 "AttributeError: Mock object has no attribute"

问题将mock.patch与autospec=True一起使用来修补类不会保留该类实例的属性。详情我正在尝试测试一个类Bar,它将类Foo的实例实例化为名为foo的Bar对象属性.被测的Bar方法叫做bar;它调用属于Bar的Foo实例的方法foo。在测试这一点时,我正在模拟Foo,因为我只想测试Bar是否正在访问正确的Foo成员:importunittestfrommockimportpatchclassFoo(object):def__init__(self):self.foo='foo'classBar(object):def__init__(self):self.foo=Foo