Questions tagged [pytest-mock]

pytest-mock provides the functionality of unittest.mock.patch as a pytest fixture. It allows for more convenient patching (avoids nested context managers or unused arguments). It provides the same API as unitest.mock.patch and unitest.mock.patch.object, and additionally provides patcher.spy, which allows spying on function without changing them.

Reference:

249 questions
0
votes
2 answers

Pytest-mock - new_callable class not being used when trying to mock whole class

In the class I am testing, I want to mock the whole DataAccess class that is used as a member variable. The DataAccess class is just abstracting the SQLite database connection. I have created a replacement MockDataAccess class that connects to a…
Charlie Clarke
  • 177
  • 1
  • 9
0
votes
1 answer

Mocking a database in python unittest

I am trying to mock the value returned by get_count function in unittest mock by mocking sql connection. But I got this error. AssertionError: 12939 !=
0
votes
1 answer

How to use pytest-mock to mock a function conditionally

I have a class in foo.py that I wish to test: import requests class Foo: def fooMethod(self, url): response = requests.get(url) return response I want to replace the requests call to simulate the response. Here is my test file…
engeeaitch
  • 81
  • 3
  • 11
0
votes
1 answer

How to test python function used to generate report using pytest?

I have a function that reads an excel file and generates a report based on its content. How do I write a test in pytest to check if the file is generated and the report is created as expected? import openpyxl def create_report(file_path): #open…
user2077648
  • 951
  • 7
  • 27
  • 42
0
votes
0 answers

Pytest Patch: Different Mock is used

I am trying to patch an imported class called BotoAWSRequestsAuth, as I want to assert a call that uses the auth. When I patch the imported class and then try to use the patch in my assertion is is failing because a different magic mock is used, I…
berimbolo
  • 3,319
  • 8
  • 43
  • 78
0
votes
1 answer

Correctly Patching a method in pytest when Module is already patched

We have a test fixture which patches two classes like below. @pytest.fixture def license_fixture(mocker): mocker.patch('api.license_api.UserLicense') mocker.patch('api.license_api.UserLicense.userrole', return_value = 'admin') # doesn't…
Rohit Srivastava
  • 278
  • 4
  • 17
0
votes
0 answers

Patch method used in global variable in a module in Python

I am trying to mock a method used in a global variable's format method in a module. The file I want to test is as follows: do_work.py from foo import bar LINK = "foobar.com" def generate_foobar(): return "visit {}".format(bar(LINK)) FOOBAR =…
harsh8398
  • 36
  • 6
0
votes
1 answer

Is there a built-in mock object to pass in to Python unit test?

I commonly instantiate a Mock object during unit tests. I am sick of: Having to type from unittest.mock import Mock And then instantiate a Mock object via mock = Mock() I am wondering, does pytest, unittest.mock, pytest-mock, etc. have a built in…
0
votes
0 answers

How do I mock objects initialized under __init__ method using pytest

I have been trying to figure out how do I create a mock for an object I am initializing in the init method of the class. I have been using pytest and mockito to try and do the same, but with least amount of luck. I have 2 questions here: How do I…
0
votes
1 answer

Python: Mock AWS with Click

I have a click cli app and I am trying to mock AWS SSM Parameter Store, but runner.invoke is not returning the expected results. This is test_demo.py: from click.testing import CliRunner import os import boto3 from moto import mock_ssm import…
tomarv2
  • 753
  • 3
  • 14
0
votes
1 answer

Does tmpdir in pytest create a file only when we write to it?

When I use fp = tmpdir.join("hello.txt") in pytest, does pytest actually create a file in my temp directory or it creates only when I write fp.write("hello")?
Yong zhu
  • 103
  • 1
  • 1
  • 6
0
votes
1 answer

Unit testing Python mixin which references a function defined elsewhere

I have the following situation: mixin_foo.py class Foo: def doFoo(self): bar=self.defined_elsewhere() #....do work Unit test: @patch('path_to_mixin_foo.mixin_foo.Foo.defined_elsewhere') def test_do_foo(self,…
laconicdev
  • 6,360
  • 11
  • 63
  • 89
0
votes
1 answer

PropertyMock with changing values after every call

I'm trying to mock psycopg2 and need to handle cursor.description # run query 1 column_names = [desc[0] for desc in cursor.description] # run query 2 # NB cursor.description now returns different value column_names = [desc[0] for desc in…
jeznag
  • 4,183
  • 7
  • 35
  • 53
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

Pytest mock an object returned via yield and check whether a method of that object is called

I have a piece of code need to be tested via pytest def my_function(value): with some_generator() as gen: gen.some_method(value) I need to check whether the some_method has been called or not. I have used pytest-mock to mock the…
javaDeveloper
  • 1,403
  • 3
  • 28
  • 42
1 2 3
16
17