Can Python MiniMock mock functions defined in a single file?

I am using the Python MiniMock library for unit testing. I would like to make fun of the function defined in the same Python file as my doctrist. Can MiniMock handle this? The naive approach fails:

def foo():
    raise ValueError, "Don't call me during testing!"

def bar():
    """
    Returns twice the value of foo()

    >>> from minimock import mock
    >>> mock('foo',returns=5)
    >>> bar()
    Called foo()
    10

    """
    return foo() * 2

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Here's what happens if I try to run this code:

**********************************************************************
File "test.py", line 9, in __main__.bar
Failed example:
    bar()
Exception raised:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/doctest.py", line 1212, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.bar[2]>", line 1, in <module>
        bar()
      File "test.py", line 13, in bar
        return foo() * 2
      File "test.py", line 2, in foo
        raise ValueError, "Don't call me!"
    ValueError: Don't call me!
**********************************************************************
1 items had failures:
   1 of   3 in __main__.bar
***Test Failed*** 1 failures.

Edit: In accordance with the answers below, this was identified as a bug and was fixed in MiniMock .

+3
source share
2 answers

I just answered on the mailing list with a MiniMock patch that fixes this.

, :

>>> mock('foo',returns=5)
>>> bar.func_globals['foo'] = foo

>>> mock('foo', nsdicts=(bar.func_globals,), returns=5)
+5

:

def foo():
    raise ValueError, "Don't call me during testing!"

def bar():
    """
    Returns twice the value of foo()

    >>> from minimock import mock
    >>> mock('foo',returns=5)
    >>> bar.func_globals['foo'] = foo
    >>> bar()
    Called foo()
    10

    """
    return foo() * 2

if __name__ == "__main__":
    import doctest
    doctest.testmod()

, foo in bar , .

, , bar globals . , mock foo, , bar .

, .

2. . MiniMock . , .

EDIT: , - , :

def foo():
    raise ValueError, "Don't call me during testing!"

def bar():
    """
    Returns twice the value of foo()

    >>> bar()
    10

    """
    return foo() * 2

if __name__ == "__main__":
    from minimock import mock
    mock('foo',returns=5)
    import doctest
    doctest.testmod()

, " foo()" .

+1

Source: https://habr.com/ru/post/1718320/


All Articles