Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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 unittest:模拟从类对象调用的外部库函数_Python_Pytest_Python Unittest_Python Unittest.mock_Pytest Mock - Fatal编程技术网

Python unittest:模拟从类对象调用的外部库函数

Python unittest:模拟从类对象调用的外部库函数,python,pytest,python-unittest,python-unittest.mock,pytest-mock,Python,Pytest,Python Unittest,Python Unittest.mock,Pytest Mock,你好,我有以下代码 我试图测试文件_a中的加载函数;下载是我导入的外部模块中的功能 file_a.py from foo import download class Bar() __init__(self, arg_1): self.var = arg_1 def load(self): if self.var == "latest_lib": download("latest_lib&qu

你好,我有以下代码

我试图测试文件_a中的加载函数;下载是我导入的外部模块中的功能

file_a.py

from foo import download

class Bar()
    __init__(self, arg_1):
        self.var = arg_1

    def load(self):
        if self.var == "latest_lib":
            download("latest_lib")
@patch(file_a.download)
def test_download():
    import file_a
    bar = file_a.Bar("latest_lib")
    bar.load()
    file_a.download.assert_called() 
我写的测试像

test.py

from foo import download

class Bar()
    __init__(self, arg_1):
        self.var = arg_1

    def load(self):
        if self.var == "latest_lib":
            download("latest_lib")
@patch(file_a.download)
def test_download():
    import file_a
    bar = file_a.Bar("latest_lib")
    bar.load()
    file_a.download.assert_called() 

但bar对象似乎不是调用模拟下载,而是调用导入的下载。如何解决此问题并通过测试?

我认为您缺少模拟设置:

@patch("foo.download")
def test_download(mock_download):
    from file_a import Bar

    Bar("latest_lib").load()
    mock_download.assert_called()

我认为您缺少模拟设置:

@patch("foo.download")
def test_download(mock_download):
    from file_a import Bar

    Bar("latest_lib").load()
    mock_download.assert_called()

我试着用你自己的代码看看哪里出错了。有一些语法错误其实并不重要,但主要问题是您应该将字符串传递给
patch
,以使其正常工作

以下是我对您的代码所做的修改:

# file_a.py

from pprint import pprint as pp

class Bar():
    def __init__(self, arg_1):
        self.var = arg_1

    def load(self):
        if self.var == "latest_lib":
            pp("latest_lib")
以及:

注意事项:

  • 您需要将一个字符串传递给
    patch
    ,使其在运行时模拟对象
  • 您必须在测试函数中接收模拟对象
  • 我在这里使用基于类的测试是因为我想使用
    unittest
    标准库,但如果使用
    pytest
    ,则不必这样做
  • 还有一个最后的注意事项:

    基本原理是在查找对象的位置进行修补, 它不一定与定义它的位置相同


    我试着用你自己的代码看看哪里出错了。有一些语法错误其实并不重要,但主要问题是您应该将字符串传递给
    patch
    ,以使其正常工作

    以下是我对您的代码所做的修改:

    # file_a.py
    
    from pprint import pprint as pp
    
    class Bar():
        def __init__(self, arg_1):
            self.var = arg_1
    
        def load(self):
            if self.var == "latest_lib":
                pp("latest_lib")
    
    以及:

    注意事项:

  • 您需要将一个字符串传递给
    patch
    ,使其在运行时模拟对象
  • 您必须在测试函数中接收模拟对象
  • 我在这里使用基于类的测试是因为我想使用
    unittest
    标准库,但如果使用
    pytest
    ,则不必这样做
  • 还有一个最后的注意事项:

    基本原理是在查找对象的位置进行修补, 它不一定与定义它的位置相同


    它不是那样工作的你有错误吗?实际上,我在本地运行了这段代码,并且正确地检查了断言;你能分享你的实际代码片段吗?它不是那样工作的。你有错误吗?实际上,我在本地运行了这段代码,并且正确地检查了断言;你能分享你的代码片段吗?