首页 > 解决方案 > Python3 unittest 中的 Proxyquire 等效项

问题描述

我正在测试一个看起来像这样的脚本:

import arcpy

Class A:
    function go(x, y, z):
        arcpy.dothing(y,x,z['attr'])

我想编写一个单元测试python 3.6.6,传递虚拟参数并验证它们是否正确转换并传递给arcpy.dothing方法。为此,我在节点中使用了 proxyquire,但在 python 中我无法弄清楚。特别是@patch似乎不允许捕获输入参数。

标签: pythonunit-testingpython-unittest

解决方案


所以我在经过大量的实验后发现了这一点。这是我最终得到的结果:

import unittest
from a import A
from unittest.mock import patch

class Test_ATest(unittest.TestCase):   
    @patch('a.arcpy.dothing')
    def test_dothing(self, dothing):
        d = {"attr": "c"}
        obj = A()
        obj._go(1,2,d)

        dothing.assert_called_with(2,1,'c')

推荐阅读