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

Python async property on mock instance

Im trying patch an async property on a mock instance. class Foo: @property async def bar(self) -> bool: return True foo_mock = AsyncMock(Foo) foo_mock.bar.return_value = False assert await foo_mock.bar is False but it fails…
0
votes
0 answers

In robot framework (or python), is there a way to patch a mock network request response into the browser

I am currently starting automated testing in robot framework. As part of this I need to go to a demo page and click on a button. The click event triggers a network request to our server which returns whether or not agents are available. I need a way…
0
votes
0 answers

Is it possible to reference the original return_value of unmocked function without needing to recall the function?

I have read through the docs and couldn't find anything about accessing the original/expected/unedited return_value of the foo_hello function and use it to make assertions. The best I could come up with is getting the parameters from the call_args…
0
votes
1 answer

How to mock the post() and get() calls of httpx in python unitest?

The following test works when I patch the entire function get_post() and get_call(). How can I patch the httpx.post() and httpx.get()? In src/app.py import httpx class Client: def __init__(self, url): self.url = url def…
0
votes
1 answer

Mock environment variables inside __init__ file

have a question about mocking environment variables The module has this structure sharepoint |-- __init__.py (Here I initialize some variables I used in get_file and get_token) |-- get_file.py |-- get_token __init__.py main.py So, I'm trying to…
BAPF123
  • 37
  • 5
0
votes
0 answers

How do I get access to mocked return value within patched method?

I have a SomeModel class defined in a third party library which is used like this: class SystemUnderTest: def foo(): ... with SomeModel.my_method() as model: x = ... model.bar(x) I would like to test that…
John
  • 10,837
  • 17
  • 78
  • 141
0
votes
1 answer

python unittest: How to mock an object's construct to return a mock?

I have a.py class foo: def __init__(self, name): self.name=name def get_name(self): return self.name And a caller b.py form a import foo def get_foo(): myfoo = foo("Alex") name = myfoo.get_name() return name Now I want…
harryk
  • 123
  • 1
  • 10
0
votes
0 answers

Python unittest.patch - patching a class attribute to return different results each time the attribute is accessed

I need to patch/mock a class instance attribute so that each time it is called it returns a different value. The class instance attribute is a managed list which can change every time its accessed due to other processes adding to it. I believe I…
0
votes
1 answer

python - mock - class method mocked but not reported as being called

learning python mocks here. I need some helps to understand how the patch work when mocking a class. In the code below, I mocked a class. the function under tests receives the mock and calls a function on it. In my assertions, the class is…
0
votes
1 answer

How to set a side_effect on an instance method of a class that's been patched?

How do I get a method of a @patch'ed class to throw an Exception when called? B is just a class that you can call go() on, which in turn just prints that it happened: # module_b.py class B: def __init__(self) -> None: self.x = True …
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
0
votes
1 answer

How to create a callable mock from a non-callable spec?

I have two classes: class One that does some stuff and class Wrapper that translates the API of One. To test the Wrapper I want to mock the class One. My problem is that One sets a number of attributes in it's __init__ method and I'd like my test to…
vlc146543
  • 1
  • 2
0
votes
2 answers

Python unittest case expected not matching with the actual

I am trying to mock the secrets manager client. Earlier the variables weren't in the class so I was able to mock the client directly using a patch like below: @patch('my_repo.rc.client') and now since I am using an instance method, I need to mock…
0
votes
1 answer

unittest in python not working as expected

We are deploying the code in CI/CD fashion via Terraform. So, we have a lambda function under which I have written a code for retrieving the specific secret creds. Below is my lambda code: logger = get_provisioner_logger() session =…
0
votes
0 answers

Why unittest.mock.mock_add_spec removes existing mock methods?

Why after using unittest.mock.mock add_spec methods of mock are disappearing? from unittest.mock import Mock m = Mock(spec=str) print(hasattr(m, 'lower')) # True m.mock_add_spec(['foo'],) print(hasattr(m, 'lower')) # False print(hasattr(m,…
salius
  • 918
  • 1
  • 14
  • 30
0
votes
0 answers

How do I mock a method that uses requests.get with the WITH keyword in my class?

I am having trouble understanding how mocking works when the get responses involve using the with keyword. Here is an example I am following for my class `Album' and I have been successful when I am mocking a url as seen below: def…
Lee
  • 1
  • 1