小编myg*_*myg的帖子

模拟python函数具有多个返回值

我有一个返回多个值的python函数.我的功能:

def myExampleFunction(a,b)
    # here is my code
    return name, number1, number2


def FunctionIWantToTest(self):
    # here is my code
    myName, myNumber1, myNumber2 = self.myExampleFunction(a,b)
Run Code Online (Sandbox Code Playgroud)

我想将自己的值赋予FunctionIWantToTest返回的值.所以,我试图用nosetest测试第二个函数,但我不知道如何模拟myExampleFunction的返回.

我试过这个:

def test_mytest(self):
    [...]
    c.myExampleFunction = Mock()
    c.myExampleFunction.side_effect = ["myName", 100, 200]
    [...]
Run Code Online (Sandbox Code Playgroud)

但它不起作用.当我启动nosetests时,我读到了这条消息:

ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)

任何的想法?我使用python 2.7.3

python mocking nose

7
推荐指数
2
解决办法
9767
查看次数

带有参数的python模拟函数

我想模拟一个调用带有参数的外部函数的函数。我知道如何模拟一个函数,但是我不能给出参数。我尝试了@ patch,side_effects,但没有成功。

def functionToTest(self, ip):
    var1 = self.config.get(self.section, 'externalValue1')
    var2 = self.config.get(self.section, 'externalValue2')
    var3 = self.config.get(self.section, 'externalValue3')

    if var1 == "xxx":
        return False
    if var2 == "yyy":
        return False

    [...]
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我可以这样做:

    def test_functionToTest(self):
    [...]
    c.config = Mock()
    c.config.get.return_value = 'xxx'
Run Code Online (Sandbox Code Playgroud)

因此,var1,var2和var3都使用“ xxx”相同的值,但是我不知道如何模拟每条指令并给出我想要的var1,var2和var3值

(python版本2.7.3)

python mocking

2
推荐指数
1
解决办法
1696
查看次数

标签 统计

mocking ×2

python ×2

nose ×1