Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
62 views

@transaction.atomic def deposit(user: User, account_number: str, amount: float) -> None: account = get_object_or_404( Account.objects.select_for_update(), account_number=account_number, ...
Jueun's user avatar
  • 11
0 votes
1 answer
115 views

I want to do a unittest with pytest for this method: class UserService: @classmethod async def get_user_by_login(cls, session: SessionDep, login: str) -> Optional[User]: sql = ...
Mister Balise's user avatar
2 votes
2 answers
122 views

I am coding a FastAPI app with async functions. My issue is, I can't properly mock async function with the mocker.patch from pytest-mock nor with AsyncMock from unittest. The issue is, I can't test ...
Mister Balise's user avatar
0 votes
1 answer
82 views

I have a configuration module, config with data config.data where I want to add a value for testing. Ideally, I want to use mock.patch.dict as a context manager because the value is a class attribute ...
KSS's user avatar
  • 179
1 vote
1 answer
64 views

I have a custom class in the file MyClass.py class MyClass: def __init__(self): self.fetched_data = False def get_data(self): self.fetched_data = True ...
DwightFromTheOffice's user avatar
1 vote
1 answer
107 views

I am implementing a test for a function func1a() in module1.py using mocks. The function func1a() calls: one function from module2.py: func2a() one function from module1.py: func1b() It appears that ...
fbas's user avatar
  • 13
2 votes
1 answer
44 views

We can check if a unittest.mock.Mock has any call with some specified arguments. I now want to test that some of the arguments are correct, while I do not know about the other ones. Is there some ...
502E532E's user avatar
  • 599
0 votes
1 answer
115 views

I have a scenario where I create a git.repo.base.Repo object in a "someFile.py": def someFunction(): //some part of code repo = Repo(path) repo.head.reference = repo.commit(...
Shiva Shukla's user avatar
0 votes
0 answers
30 views

In odoo 17 I need to use unitest.mock to mock the return value of a function The function is called cfdi_cancel. It is located in mymodulename module, inside that module is the 'models' folder, inside ...
Ernesto Ruiz's user avatar
0 votes
1 answer
70 views

I always thought that you could mock a method on a class only when the class it is coming from is not instantiated in the code under test. And if it is you have to mock the class (just as you have to ...
Nemelis's user avatar
  • 5,562
1 vote
1 answer
46 views

I am trying to call four decorators on my unit tests to easily allow code to be refactored: pytest.fixture, unittest.mock.patch, pytest.mark.parameterize, pytest.mark.asyncio. I currently am calling ...
Addi O's user avatar
  • 21
0 votes
1 answer
35 views

I have code with below functionality src/client.py class Client1(object): def foo(self, t): return f"foo-{t}" def get_foo(self, type): return self.foo(type) src/...
uday8486's user avatar
  • 1,423
1 vote
1 answer
107 views

I have an app/main.py for FastAPI which does: import qdrant_client as QdrantClient ... qdrant_client = QdrantClient(url=...) qdrant_client.create_collection(...) ... app = FastAPI() ... @app.get(&...
Clovis's user avatar
  • 4,483
0 votes
1 answer
135 views

Is it possible to declare a local variable before it's imported? For example to get this code to run as expected: # a.py # do magic here to make b.foo = "bar" import b b.printlocal() # b....
Brett S's user avatar
  • 589
-1 votes
1 answer
70 views

A minimal working example is available at https://github.com/rgaiacs/django-mwe-magicmock. When using Django, I use Model.clean() to validate the form submitted by the user. During the validation, ...
Raniere Silva's user avatar
0 votes
1 answer
235 views

Here's a minimal reproducible example. This is with python 3.11. Besides pytest, no other dependency. # minum_reproducible_example.py from typing import Literal from unittest.mock import Mock, patch ...
largehadroncollider's user avatar
1 vote
0 answers
36 views

Running docker compose -f docker-compose.local.yml run --rm django pytest ./project/app/ the following test passes perfectly, meaning patch method is being used @patch("project.app....
Nadav's user avatar
  • 634
0 votes
1 answer
70 views

I do not manage to do some basic assert_called() in a class where some methods are internally calling other methods. Example code: from unittest.mock import Mock class Foo: def print1(self) -> ...
Manu's user avatar
  • 102
1 vote
1 answer
195 views

I'm trying to write a test for a class which performs a pandas apply. Here's a simplified version: import pandas as pd class Foo: def bar(self, row): return "BAR" def apply(...
olives's user avatar
  • 118
1 vote
1 answer
91 views

I am trying to write unit tests that involve mocking several libraries in different ways for each test. When I run each test individually, they all pass, but when I run them all together, many of them ...
Disciple153's user avatar
-1 votes
2 answers
58 views

I have a file that looks roughly like this: bar.py from foo import foo # function that returns an int from dog import Dog dog_foo = 4 * foo() my_dog = Dog(dog_foo) ... I want to test that the ...
user7097722's user avatar
0 votes
2 answers
111 views

Mock.assert_has_calls asserts that the specified calls exist, but not that these were the only calls: from unittest.mock import Mock, call mock = Mock() mock("foo") mock("bar") ...
LondonRob's user avatar
  • 79.9k
0 votes
2 answers
80 views

How would I assert that this function wrote to those files in tests? def write() -> None: with open('./foo', 'w') as f: f.write('fooo') with open('./bar', 'w') as f: f.write(...
sezanzeb's user avatar
  • 1,223
1 vote
2 answers
3k views

I'm working on a FastAPI application and am trying to write some tests for one of my routes. My testing environment cannot make calls to external services. My goal is just to test the parsing of the ...
jakobkub's user avatar
3 votes
1 answer
57 views

I've tried multiple ways and just can't get the result I'm looking for. It seems like a lot of questions use a different type of mocking with the whole @patch decorators but I've been using this more ...
Darius Fiallo's user avatar
3 votes
3 answers
3k views

Given this example in Python 3.8, using Pydantic v2: from pydantic import BaseModel import pytest from unittest.mock import MagicMock class MyClass(BaseModel): a: str = '123' # the actual ...
Remolten's user avatar
  • 2,702
2 votes
1 answer
130 views

I am using Python 2.7 for a coding project. I'd love to switch to Python 3, but unfortunately I'm writing scripts for a program that only has a python package in 2.7 and even if it had one in 3 our ...
Réka's user avatar
  • 145
0 votes
1 answer
86 views

I'm trying to perform a unit test on each function of a Python file using unittest. Assuming the file I want to test is called X.py and the file containing the testing code is Y.py. In file X, there ...
Yichen Zhang's user avatar
1 vote
1 answer
113 views

I'm trying to figure out how to mock matplotlib's plt.savefig inside a class to test if my GUI's button is actually saving a figure. For simplicity's sake, I will not include the GUI, but only the ...
DracoArtist's user avatar
-1 votes
1 answer
68 views

main.py from path.to.mod import run def foo(args: str): run(args) test.py @patch("path.to.mod.run") def verify_args(mock): foo("bar") mock.assert_called_once_with(&...
ealeon's user avatar
  • 12.6k
-1 votes
2 answers
86 views

I have a test that intermittently fails during automated builds so I want to add some retries to my tool. However, I'm not sure how to force the tool to fail and verify that it has retried before I ...
jon_two's user avatar
  • 1,278
0 votes
1 answer
54 views

I am trying to test a consumer that consumes messages of a queue and creates the corresponding object in salesforce. To test the consumer I have to start it in a new thread because, it is an infinite ...
Gill Mertens's user avatar
0 votes
1 answer
80 views

I'm trying to obtain different responses by each call on a Mock by using the side_effect attribute. However, I'm obtaining the first one each time. I would like to ask if I can get any help on it. ...
HouKaide's user avatar
  • 363
2 votes
1 answer
51 views

I want to verify Foo() calls Bar() without actually calling Bar(). And then I want to verify that obj is assigned with whatever Bar() returns. I tried the following: class Bar: def __init__(self, ...
lulalala's user avatar
  • 18k
0 votes
1 answer
22 views

I have Processor class: class Processor: def __init__(self, session_provider): self.session_provider = session_provider def run(self): ... self.session_provider....
KwarcPL's user avatar
  • 85
0 votes
0 answers
28 views

class TestFilterDF(unittest.TestCase): @patch('plugins.qa_plugins.preprocessing.read_df') def test_filter_df(self, read_df_mock): # Mocking read_df function to return a DataFrame ...
Uplabdhi Khare's user avatar
0 votes
3 answers
64 views

Trying to unittest a method that receives a class as an argument but unsure how to mock it right I am trying to unittest this method ` def get_local_path(p4_path, p4): if p4_path.endswith('...'): ...
Craig L's user avatar
0 votes
1 answer
67 views

I have to mock the global variable in python, but the variable value is coming from the another function. When i am importing the file, this function is getting run but instead of this, I want the ...
ak4550126's user avatar
1 vote
0 answers
135 views

chain.py ---------------------------------- from langchain.chains import GraphCypherQAChain from langchain_openai import ChatOpenAI from langchain_community.graphs import Neo4jGraph from langchain....
Saikat Bhattacharya's user avatar
0 votes
1 answer
336 views

I'm trying to test a function that should mock s3 resource but instead it tries to call the actual function. Here is my code to test. import sys import requests import json import pandas as pd import ...
47sahilone's user avatar
0 votes
0 answers
44 views

I am using unittest.mock to test my Django application. The set-up is the following: I want to test a function foo foo uses a method X which is a constructor from an external package X is called in ...
kenshuri's user avatar
  • 542
0 votes
1 answer
72 views

Given the following code, I am unable to properly patch the os.environ "USE_THING". I believe this is because USE_THING is a constant created before the test runs, so I cannot reassign the ...
bort's user avatar
  • 379
1 vote
0 answers
52 views

I have these two classes: one.py class One: def __init__(self) -> None: pass def startS(self): self._doS() def _doS(self): print("Inside doS ...
tatti's user avatar
  • 11
0 votes
2 answers
50 views

In my get_orders.py file I am importing a function called admin_only that exists at the given path (src.lib.authorization). In my test_get_orders.py file I am trying to patch this function, however ...
Tony96's user avatar
  • 41
0 votes
1 answer
52 views

I'm using side_effect for dynamic mocking in unittest. This is the code. // main functionn from api import get_users_from_api def get_users(user_ids): for user_id in user_ids: res = ...
user avatar
0 votes
1 answer
610 views

I am new to using asyncio in Python and I'm having trouble figuring out how to write a test which uses python-websockets (and asyncio). I want to have a client which connects to a websocket and sends ...
j1nrg's user avatar
  • 116
0 votes
1 answer
81 views

I'm going to mock a Python function in unit tests. This is the main function. from api import get_users_from_api def get_users(user_ids): for user_id in user_ids: res = get_users_from_api(...
user avatar
1 vote
1 answer
107 views

I need to mock a function that is called in a class attribute in a python class. I'm able to do it patching the function when the object is initialised, but it does not work when I access it without ...
lisandro laurent's user avatar
0 votes
0 answers
63 views

I want to make a unit test for the following code. But i can not try to correctly mock the cursor of my mocked connection. In the unit test, I mock first the connection of my tested code. But the ...
Thomas Campos's user avatar
2 votes
1 answer
41 views

While having deep dive into how the built-in unittest.mock was designed, I run into these lines in the official source code of mock.py: class Base(object): _mock_return_value = DEFAULT ...
Rb87's user avatar
  • 23

1
2 3 4 5
8