Questions tagged [python-unittest.mock]

mock object library (part of the Python Standard Library)

unittest.mock allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

It provides a core Mock class removing the need to create a host of stubs throughout your test suite.

330 questions
0
votes
1 answer

Problem with mocking glob does not called for for loop

I am using mock to test something I developed. In the app, I am using glob to loop for something in a directory for example: '/tmp/*.png'. It will collect all .png files in the directory and return a list of that files. When I mock glob, it returns…
Nikko
  • 1,410
  • 1
  • 22
  • 49
0
votes
0 answers

mock patching python instance method

There are many examples online and in the docs of patching static methods on classes, or mocking the entire class. What I want to do is create a new method on an existing class which is in a library that I do not own - requests.Response. I tried the…
Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
0
votes
1 answer

Python test if a function was not called

I am a newbie at writing unit tests so pardon my lack of knowledge. I have looked at previous posts, but still not able to get it working. I have def get_bugs(): bugs = [] if ...: bugs.append(123) # can be empty return bugs def…
sam
  • 1,280
  • 2
  • 11
  • 20
0
votes
1 answer

In python, how to write unit test for a function is using with open file?

This is function to get version and using with open to read file from file path location. def get_version(self): try: with open("file_path") as openfile: for line in openfile: sline = line.split() …
sid
  • 1
  • 1
  • 1
0
votes
1 answer

How to create a decorator that swallows sys.stdout using unittest.mock.patch function?

I'm trying to create a Python Mixin for my unit-tests testing functions with return values and sys.stdout. I want the Mixin to have a method that will work as a decorator for swallowing the sys.stdout, but I haven't been successful so far. My…
0
votes
1 answer

What is the difference between mocking with @patch() with and without side_effect?

I have a script.py file: # in script.py def my_basic_function(value, c): return c(value).words() class BasicClass: def __init__(self, val): self.val = val def words(): return self.val and a test.py file: # in…
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
0
votes
1 answer

How to build unit test cases using python's unittest mock?

Base class has methods which the child class inherits and calls them. The methods of base class in-turn makes call to methods present in util classes. util class @staticmethod def create_n_insert_into_sql(table_name, data): #logic to create…
Gopinath S
  • 101
  • 1
  • 14
0
votes
1 answer

Python - Mock class init that instantiates another class inside

I have the following python file board.py: def __init__(self, language): self.foo = Foo(language) self.words = Aux(self.foo) And I'm creating this test_file: @classmethod def setUpClass(cls): super().setUpClass() …
Enorio
  • 65
  • 8
0
votes
1 answer

Mocking a module function called by a static method does not work

Okay so I have a series of Python3 code that looks like this: foo.py from bar import ClassB class ClassA: @staticmethod def get_all(fn): something = ClassB(fn) return something.lines() bar.py def baz(f): with open(f,…
pepoluan
  • 6,132
  • 4
  • 46
  • 76
0
votes
1 answer

How to write Unittest for subclass while mocking base class?

I want to write test for Subclass while mocking Base as Base comes from an external library. How can I mock it given that we modify all callables in base class. class SubClass(Base): def __init__(self, *args, **argv): …
hasanatkazmi
  • 8,041
  • 2
  • 22
  • 18
0
votes
1 answer

How to mock list inside mocked object with python unittest?

I'm trying to return a list from Mock object but can't get it to work. That's the code: def execute_call(model, payload): result = model.execute(payload) code = result[0] Here's the test I have tried: def test_get_code(self, session): …
npc
  • 45
  • 1
  • 14
0
votes
0 answers

how to mock one module within a package in python

TL;DR : How to mock single module from a python package Detailed description: We are using Beaglebone black based custom board and we have different git repos containing different python scripts/modules. Now in most of the repos we maintain unit…
ART
  • 1,509
  • 3
  • 29
  • 47
0
votes
1 answer

Mock a series of interdependent calls

I have a method that scrapes a web page and saves data into a file (see below for an example code). I need to test that the resulting data is well-formed. The problem is, the data is received from a series of calls, and further calls use the results…
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
0
votes
1 answer

unittest.mock: how to mock two calls on method?

How to i mock datetime.datetime.now with two calls on same method? Below my tried: from datetime import datetime as dt def metodo(): return dt.today().strftime('%Y'), dt.today().strftime('%Y-%m-%d %X %z') def test_metodo(self): expected =…
britodfbr
  • 1,747
  • 14
  • 16
0
votes
0 answers

Python Method: Execute multiple IP's in one unit test? The best practise?

Details For a larger unittest I need a method in Python this allows servers / switches / routers to call IP's and respond accordingly to the test. My thoughts in this regard: Reading out the IP's from a CSV file Create a configuration file in which…
Mornon
  • 59
  • 5
  • 22
1 2 3
21
22