Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用两个返回值对函数进行单元测试?_Python_Python 3.x_Unit Testing - Fatal编程技术网

Python 如何使用两个返回值对函数进行单元测试?

Python 如何使用两个返回值对函数进行单元测试?,python,python-3.x,unit-testing,Python,Python 3.x,Unit Testing,我在一个类中有一个函数,它返回两个字典 class A(): def __init__(self): self.dict1={} self.dict2={} def funct1(self,a,b): self.dict1['a']=a self.dict2['b']=b return self.dict1,self.dict2 我想编写一个单元测试来测试函数funct1,它返回两个字典Python函数始终只返回

我在一个类中有一个函数,它返回两个字典

class A():
   def __init__(self):
       self.dict1={}
       self.dict2={}
   def funct1(self,a,b):
       self.dict1['a']=a
       self.dict2['b']=b
       return self.dict1,self.dict2

我想编写一个单元测试来测试函数funct1,它返回两个字典

Python函数始终只返回一个对象。在您的例子中,该对象是包含两个对象的元组

只需测试这两个对象;您可以在作业中解压它们并测试各个词典,例如:

def test_func1_single(self):
    instance_under_test = A()
    d1, d2 = instance_under_test.func1(42, 81)
    self.assertEqual(d1, {'a': 42})
    self.assertEqual(d2, {'b': 81})

def test_func1_(self):
    instance_under_test = A()

    d1, d2 = instance_under_test.func1(42, 81)
    self.assertEqual(d1, {'a': 42})
    self.assertEqual(d2, {'b': 81})

    d3, d4 = instance_under_test.func1(123, 321)
    # these are still the same dictionary objects
    self.assertIs(d3, d1)
    self.assertIs(d4, d2)
    # but the values have changed
    self.assertEqual(d1, {'a': 123})
    self.assertEqual(d2, {'b': 321})
确切地说,您测试的内容取决于您的特定用例和需求。

一个简单的测试就是

o = A()  # creates an instance of A
a, b = o.funct1(1, 2)  # call methods and unpack the result in two variables
assert a["a"] == 1 and b["b"] == 2  # test the values according to our precedent function call

python中没有违反直觉的东西

是什么阻止了您这么做?这两个值是一个元组。