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
0
votes
1 answer

Replacing a request to an external resource with a JSON object in Django testing

I have a API: import stripe class StripeWebHook(APIView): permission_classes = (AllowAny,) authentication_classes = () def post(self, request, *args, **kwargs): payload = request.body try: event =…
user11301070
0
votes
1 answer

python: How to mock os.statvfs as it returns posix.statvfs_result datatype

I am trying to write a unit test case Below is the partial code of the original function: def disk_space(something, otherthing): """ Comments """ dir_map = something['dir_map'] for dir, expected_free_gb in dir_map.items(): …
LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49
0
votes
1 answer

Python module pytest-mock how to test method call arguments when one class call another class instance?

I started out learning unit testing using pytest a couple of days ago, and got interested in the pytest-mock plugin (https://github.com/pytest-dev/pytest-mock). I have been able to write quite a lot of unit tests to test my code, but now I would…
sxpy
  • 131
  • 1
  • 7
0
votes
1 answer

Mocking in Behave and Flask

I am trying to mock an HTTP request call using mock because I don't want to Behave to call it actually. So I have this code scenario in matches.py file: import request def get_match(): response = request.get("https://example.com") return…
0
votes
1 answer

issue catching exception with unittest.mock.patch and requests

I have an issue with tests, specifically with mocking a request with unittest.mock.patch and requests: this is the function to test: import os from http import HTTPStatus import requests from requests.exceptions import RequestException from…
Luke
  • 1,794
  • 10
  • 43
  • 70
0
votes
1 answer

How to mock a method inside another module during unit testing

In one of my test case the flow requires that a customer provision process wherein the call goes to the api.py file where in the response is saved from the function create_t_customer like following: In api.py file it is using the method…
0
votes
0 answers

Capturing print from exception mocked raised with side_effect

How can we capture the print statement within an except when the exception is mocked? In the example below, I am mocking the make_request_and_get_response using side_effect. Then, using the pytest.raises I assert that this exception was raised. But,…
timmy78h
  • 183
  • 1
  • 3
  • 10
0
votes
1 answer

Python: Mock ImportError with pytest.fixture

I'm trying to make tests on a helper function that reacts on import success or not. The success test is working but not the failing one, do you know why ? The helper : homeassistant/components/owntracks/helper.py """Helper for OwnTracks.""" try: …
Quentame
  • 254
  • 1
  • 4
  • 13
0
votes
1 answer

Mocking requests/responses, mock object has no attribute 'url'

I'm new to the mocking library and so far it's been giving me trouble. I'm trying to test a Url parsing method that takes a response from an initialUrl which is then parsed in the method. I set autospec=true so I think it should have access to all…
resolute
  • 171
  • 2
  • 11
0
votes
1 answer

Mock patch with several exceptions in side_effects

When mocking objects, there are some instruments, which allow to change the result of a function. Let's say, there is some class: class Worker: def do_work(self): return perform_operation() And I want to test some cases, when do_work()…
Vitalii Dmitriev
  • 739
  • 1
  • 8
  • 18
0
votes
1 answer

How do I use mock @patch decorator with a locally imported library?

My directory structure looks like this: myproject/ ├── mymodule/ │ ├── foo.py │ └── model/ │ └── functions.py │ └── tests/ └── footest.py my foo.py file has a local import that looks like this: from .model.functions import MyClass I…
Rosey
  • 739
  • 1
  • 12
  • 27
0
votes
2 answers

Cannot patch a magic attribute

I'd like to be able to patch a magic attribute. The main file, which I'd like to test, is FileUtil.py, containing a class definition of FileUtil: class FileUtil: @logit(showArgs=True, showRetVal=True) def exec_file_path(self) -> str: …
rajah9
  • 11,645
  • 5
  • 44
  • 57
0
votes
1 answer

unittest for python function calling os.popen

I am trying to write unit test for function calling system process os.popen. Function: def change_from(lineTag): Lines = [] command = "ls -al" lines = os.popen(command).readlines() for line in lines: if lineTag in line: …
Wojtas.Zet
  • 636
  • 2
  • 10
  • 30
0
votes
1 answer

mocking extended classes in python

I am trying to mock a library in Python 2.7 and am running into issues. What I currently have worked but I would like to extend it to be able to differentiate between different subprocess calls. I'd like to do this by reading the second element in…
IdecEddy
  • 87
  • 1
  • 16
0
votes
0 answers

Python initializing a global varriable from a Mocked global varriable does not work?

When mocking a global variable, if I initialize another global from the mocked global, the other global does not pick up the mocked value. I hope the code makes it clearer. myglobal.py: ROOT="/" #When we mock ROOT to "/tmp/", DIR below …
Bren
  • 79
  • 1
  • 7