Questions tagged [python-mock]

A test utility that patches objects with user-defined code or data.

Mock is a Python tool for unit testing. It can patch some functions or objects with mock code with some specified properties like return values. It records and provides information on how the mock object is called.

470 questions
-1
votes
1 answer

How to return multiple values when using mock patch.object with contextlib.nested

I am new to unit testing and I am trying to write unit test using patch.object to mock the function calls. The mocked function getObj() is called twice in the function which I am testing. First time when its called I am expecting None as…
-1
votes
1 answer

Mocking super class method in python

I am implementing the following class: class TestInitializeConnection(TestMyDriver) The super class (TestMyDriver) is a TestCase, meaning: class TestMyDriver(test.TestCase): the super class has an attribute named driver that basically is sort of a…
slashms
  • 928
  • 9
  • 26
-2
votes
1 answer

How to mock a method of a class instance?

I have code like this: a.py from app.cache import Cache my_cache = Cache(cache_prefix='xxxxx') b.py from a import my_cache class MyApp: def run_app(): my_cache.get(1,2) test_b.py from mock import Mock, patch import…
user2611836
  • 386
  • 4
  • 6
  • 17
-2
votes
1 answer

mock `readlines()` in python unit tests

I am trying to mock a readlines() object in python unit tests. What I have so far is class Sample: def read_file(filename): with open(filename, "r") as f: lines = f.readlines() I want to write a unit test for readlines()…
Gingerbread
  • 1,938
  • 8
  • 22
  • 36
-3
votes
1 answer

mocking all classes from a module in python

I have a test_a.py, a.py and b.py in 3 different directories in my test environment. b.py class SBD(): def __init__(self): print("SBD created (In B)") a.py import b from b import * print("In module A") def fun1(): a=SBD() print("SBD…
1 2 3
31
32