简体   繁体   English

如何模拟在同一文件中定义但未被测试方法导入的函数?

[英]How to mock a function that is defined in the same file and is not imported by the method being tested?

I have the following code so far: 到目前为止,我有以下代码:

import unittest
from mock import patch, Mock


def method_1():
    from math import ceil
    return ceil(1.2)


def test_1():
    m = Mock(return_value=10)
    with patch('math.ceil', m) as p:
        a = method_1()
        assert(a == 10)


def method_2():
    return method_1() + 1


def test_2():
    m = Mock(return_value=20)
    with patch('method_1', m) as p:
        a = method_2()
        assert(a == 21)

on running the tests I get the following error: 在运行测试时,出现以下错误:

$ nosetests -s unittest.py 
.E
======================================================================
ERROR: unittest.test_2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/srv/www/rpr/unittest.py", line 27, in test_2
    with patch('method_1', m) as p:
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1564, in patch
    getter, attribute = _get_target(target)
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1413, in _get_target
    (target,))
TypeError: Need a valid target to patch. You supplied: 'method_1'

----------------------------------------------------------------------
Ran 2 tests in 27.840s

FAILED (errors=1)

I am able to mock math.ceil correctly and test_1 passes without any issues. 我能够正确模拟math.ceil ,并且test_1通过而没有任何问题。 I am having a hard time mocking method_1 itself. 我很难method_1本身。 How do I do this? 我该怎么做呢?

I had to change test_2 to the following to get it to work right: 我必须将test_2更改为以下内容才能使其正常工作:

def test_2():
    m = Mock(return_value=20)
    with patch(__name__ + '.method_1', m) as p:
        a = method_2()
        assert(a == 21)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 模拟:如何模拟从正在测试的 class 方法调用的 class - Python mock: How to mock a class being called from the class method being tested 如何在不知道python中模块名称的情况下模拟由测试模块直接导入的函数 - How to mock a function imported directly by the tested module without knowing the module name in python 如何模拟从不同模块导入的方法中导入的函数 - How to mock a function, that is imported within an imported method from different module pytest 模拟我自己在测试 function 中导入的模块 - pytest mock my own module imported in tested function 如何在测试方法中模拟受保护/私有方法? - How to mock a protected/private method in a tested method? 如何模拟导入实例的方法 - How to mock a method of an imported instance 模拟对象如何替换正在测试的所有系统功能? - How can mock object replace all system functionality being tested? Python:在测试函数的同一模块中定义的修补函数 - Python: patching function defined in same module of tested function Python如何在导入文件中定义的导入文件中使用函数? - Python how to use function in imported file defined in importing file? 如何在测试函数中创建对象使用的内置函数模拟 - How to create builtin function mock used by object in tested function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM