Skip to main content

All Questions

Tagged with
1 vote
0 answers
23 views

mocking out imported objects in Python

Lets say there is a module headache that defines an object that is then imported and used by code I want to test: # defined in headache/__init__.py problem = Problem() then it's imported by Foo: # ...
solyd's user avatar
  • 792
1 vote
1 answer
24 views

Cannot mock the method in dependency module correctly in Python

Here is my project structure |-- project |-- util.py |-- main.py |-- tests |-- test_main.py In the main.py file I reference the function in util.py from util import rename def ...
hh54188's user avatar
  • 15.4k
0 votes
0 answers
40 views

Why python testing always import the file in another module implicitly

Current I have two files in the same folder, in the source file I import the dlt module # File: pipeline.py import dlt Another is a test file which import nothing from the source file yet: import ...
hh54188's user avatar
  • 15.4k
0 votes
0 answers
40 views

"AssertionError: Expected 'show' to have been called once" when trying to mock plt.show()

I am currently trying to design some tests to run with pytest for a GUI. The GUI has a button that plots a graph on click. I want to make sure the button does indeed generate a graph. At this stage, I ...
DracoArtist's user avatar
0 votes
1 answer
46 views

Mock patching an endpoint unittest that makes several requests to third party

I have a single endpoint written using django ninja. This endpoint executes a crawler that performs several requests. The sequence of requests has 3 different routines depending on whether or not the ...
Gustavo Costa's user avatar
0 votes
0 answers
42 views

Python unittest how to mock function which external function is defined in __init__.py

I would like to test my_func2 that is calling my_func1. I would like to mock my_func1, but I don't know how can I do it when my_func2 is imported in my_package.__init__.py. When I delete that import, ...
sceptre's user avatar
0 votes
0 answers
33 views

Mocking a dictionary upon FastAPI router instantiation

Attempting to write tests for an existing route and running into some roadblocks. I'm trying to mock a dictionary that is loaded when a router is instantiated, but it seems like the @patch'd versions ...
Anthony's user avatar
  • 227
2 votes
1 answer
53 views

Simulate stdout in fake subprocess.Popen

I would like to test a function, which invokes subprocess.Popen and captures stdout. In particular, I need to test stdout content physically captured in a file on disc without ever calling the actual ...
Maxim Ivanov's user avatar
2 votes
1 answer
78 views

Test that a function imported into different namespaces is never called

Suppose I have foo.py def my_func(): print('hello world!') bar.py from foo import my_func I want to write tests to ensure that my_func is never called. I have this: from unittest import mock ...
RuthC's user avatar
  • 2,932
0 votes
1 answer
91 views

Test a function called in else condition

I have written a code where I am calling a function in the else case which returns a string. I have mocked it and asserted it to be called once. But this is failing for some reason. This decreases my ...
vaibhav's user avatar
  • 61
1 vote
1 answer
77 views

Why is my phyton mock only working with a specific way of import?

I have a utils class that besides other stuff has a function to get an id: # utils.py def get_id(): return ... Also, there is another class, lets say its about cars, that uses this function: # ...
ningelsohn's user avatar
0 votes
2 answers
42 views

How do I mock a function called inside the function that I am testing?

My file structure is like this: pytests/ ├── moduleA/ │ ├── functions/ │ │ └── process_moduleA.py │ └── test_process_moduleA.py └── utils/ └── archive.py in process_moduleA, I have ...
Mystique's user avatar
1 vote
2 answers
157 views

How to replace one class with another in a test and check that its method was called?

I want to replace one class with another in a test and check that its method was called. An example of what I want to do is the following code: from unittest.mock import patch class A: def ...
Boris Mirzakhanyan's user avatar
0 votes
2 answers
47 views

How to test that the method of an instance has been called?

Let's say I have the following piece of code: class MyClass: def __init__(self, value=1): self.value = value self.destructive_stuff() def destructive_stuff(self): ...
Pierre's user avatar
  • 412
2 votes
1 answer
201 views

How to patch the return value of initializing a class in Python?

I am trying to override the value of an initialized object to a preset value for a unit test. I am using the mock library long with pytest. Here is code that demonstrates the problem: def ...
Anchey Peng's user avatar

15 30 50 per page
1
2 3 4 5
18