草庐IT

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

Python 模拟具有不同结果的多个调用

我希望能够对特定属性函数进行多次调用,每次连续调用都返回不同的结果。在下面的示例中,我希望在第一次调用时返回5,然后在第二次调用时返回10。例如:importmockclassA:def__init__(self):self.size=0defincrement(self,amount):self.size+=amountreturnamount@mock.patch("A.increment")deftest_method(self,mock_increment):defdiff_inc(*args):defnext_inc(*args):#Idon'tknowwhatbelongs

Python 模拟具有不同结果的多个调用

我希望能够对特定属性函数进行多次调用,每次连续调用都返回不同的结果。在下面的示例中,我希望在第一次调用时返回5,然后在第二次调用时返回10。例如:importmockclassA:def__init__(self):self.size=0defincrement(self,amount):self.size+=amountreturnamount@mock.patch("A.increment")deftest_method(self,mock_increment):defdiff_inc(*args):defnext_inc(*args):#Idon'tknowwhatbelongs

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

Python mock 修补另一个函数调用的函数

deff1():return10,Truedeff2():num,stat=f1()return2*num,stat如何使用python的模拟库修补f1()并返回自定义结果以便我可以测试f2()?已编辑:我的测试有问题吗?这似乎不起作用,所有测试都因AssertionError而失败fromfoo.barimportf2frommockimportpatchclassMyTest(TestCase):deftest_f2_1(self):withpatch('project.module.f1')assome_func:some_func.return_value=(20,False)

Python mock 修补另一个函数调用的函数

deff1():return10,Truedeff2():num,stat=f1()return2*num,stat如何使用python的模拟库修补f1()并返回自定义结果以便我可以测试f2()?已编辑:我的测试有问题吗?这似乎不起作用,所有测试都因AssertionError而失败fromfoo.barimportf2frommockimportpatchclassMyTest(TestCase):deftest_f2_1(self):withpatch('project.module.f1')assome_func:some_func.return_value=(20,False)