python-mock

How to mock just the method inside the class

别来无恙 提交于 2021-02-18 12:10:55
问题 Trying to write a testcase for my class based function. This is skeleton of my class class Library(object): def get_file(self): pass def query_fun(self): pass def get_response(self): self.get_file() response = self.query_fun() # some business logic here return response I need to create a testcase that can mock just the query_fun and do the rest. tried below but seems is not the right direction: from unittest import mock, TestCase from library.library import Library class TestLibrary(TestCase)

How can I mock any function which is not being called directly?

有些话、适合烂在心里 提交于 2021-02-10 15:17:00
问题 TL;DR How can I patch or mock "any functions that are not being called/used directly" ? Sceneario I have a simple unit-test snippet as # utils/functions.py def get_user_agents(): # sends requests to a private network and pulls data return pulled_data # my_module/tasks.py def create_foo(): from utils.functions import get_user_agents value = get_user_agents() # do something with value return some_value # test.py class TestFooKlass(unittest.TestCase): def setUp(self): create_foo() def test_foo

How can I mock any function which is not being called directly?

我的梦境 提交于 2021-02-10 15:15:25
问题 TL;DR How can I patch or mock "any functions that are not being called/used directly" ? Sceneario I have a simple unit-test snippet as # utils/functions.py def get_user_agents(): # sends requests to a private network and pulls data return pulled_data # my_module/tasks.py def create_foo(): from utils.functions import get_user_agents value = get_user_agents() # do something with value return some_value # test.py class TestFooKlass(unittest.TestCase): def setUp(self): create_foo() def test_foo

How to use Mock library to mock a Django ForeignKey value?

烈酒焚心 提交于 2021-02-07 12:34:41
问题 I have a model and I'm trying to test validation without invoking the database layer. Rather than describe with words I'll just post up some example code. The issue here is the ForeignKey relationship to Bar, which isn't relevant to what I'm trying to test but is stopping me from running the test that I want. First, myapp/models.py : from django.core.exceptions import ValidationError from django.db import models class BadFooError(ValidationError): pass class Bar(models.Model): description =

Using mock to test if directory exists or not

坚强是说给别人听的谎言 提交于 2021-01-28 07:44:48
问题 I have been exploring mock and pytest for a few days now. I have the following method: def func(): if not os.path.isdir('/tmp/folder'): os.makedirs('/tmp/folder') In order to unit test it, I have decided to patch os.path.isdir and os.makedirs, as shown: @patch('os.path.isdir') @patch('os.makedirs') def test_func(patch_makedirs, patch_isdir): patch_isdir.return_value = False assert patch_makedirs.called == True The assertion fails, irrespective of the return value from patch_isdir. Can someone

mock.patch decorator: missing 1 required positional argument: 'self'

主宰稳场 提交于 2021-01-07 04:06:12
问题 I am trying to patch a variable in module settings during a test method run: from unittest import mock class Test(...): @mock.patch('settings.TARGET_SCORES_PER_SECTION', True) def test_register_user(self): I am getting this error: ERROR: tests.test_user.transplant_class.<locals>.C (test_register_user) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib

mock.patch decorator: missing 1 required positional argument: 'self'

蓝咒 提交于 2021-01-07 04:03:53
问题 I am trying to patch a variable in module settings during a test method run: from unittest import mock class Test(...): @mock.patch('settings.TARGET_SCORES_PER_SECTION', True) def test_register_user(self): I am getting this error: ERROR: tests.test_user.transplant_class.<locals>.C (test_register_user) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib

Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

与世无争的帅哥 提交于 2020-12-27 12:50:03
问题 I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock only the method A from django.test import TestCase from mock import MagicMock import myClass class FirstTest(TestCase): def setUp(self): myClass.A = MagicMock(return_value = 'CPU') def test(self): #some tests myClassObj = myClass() myClassObj.check() Whereas in my second test I mock the entire check method: from

Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

人盡茶涼 提交于 2020-12-27 12:45:56
问题 I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock only the method A from django.test import TestCase from mock import MagicMock import myClass class FirstTest(TestCase): def setUp(self): myClass.A = MagicMock(return_value = 'CPU') def test(self): #some tests myClassObj = myClass() myClassObj.check() Whereas in my second test I mock the entire check method: from

Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

妖精的绣舞 提交于 2020-12-27 12:45:52
问题 I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock only the method A from django.test import TestCase from mock import MagicMock import myClass class FirstTest(TestCase): def setUp(self): myClass.A = MagicMock(return_value = 'CPU') def test(self): #some tests myClassObj = myClass() myClassObj.check() Whereas in my second test I mock the entire check method: from