mocking

Is there any advantage to writing both classist and mockist unit tests for a single method?

我的未来我决定 提交于 2021-02-10 18:55:32
问题 One can write unit tests either the classicist or the mockist way as per http://martinfowler.com/articles/mocksArentStubs.html Will writing both classist and mockist unit tests for a single method increase robustness of the code since both state and behaviour is tested? My colleagues seem to just be mocking all the way, and as they're the "example", it is assumed that I will be mocking as well unless I have a good reason not to. (I'm new to unit testing). However, I feel that testing just the

Is there any advantage to writing both classist and mockist unit tests for a single method?

穿精又带淫゛_ 提交于 2021-02-10 18:51:53
问题 One can write unit tests either the classicist or the mockist way as per http://martinfowler.com/articles/mocksArentStubs.html Will writing both classist and mockist unit tests for a single method increase robustness of the code since both state and behaviour is tested? My colleagues seem to just be mocking all the way, and as they're the "example", it is assumed that I will be mocking as well unless I have a good reason not to. (I'm new to unit testing). However, I feel that testing just the

Writing a contextmanager that patches an object

。_饼干妹妹 提交于 2021-02-10 17:47:58
问题 In my Python 3 test code, I have a lot of these statements: from unittest.mock import patch user = User(...) with patch.object(MyAuthenticationClass, 'authenticate', return_value=(user, 'token'): # do something Now I want to write this as: with request_user(user): # do something How would I write a method request_user as context manager such that it patches the authentication in this way, and removes the patch after the with block? 回答1: You can write a simple wrapper like this: def request

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

Really weird… can't set attributes of built-in/extension type 'lxml.etree._Element'

匆匆过客 提交于 2021-02-10 14:18:40
问题 I've changed attributes for other classes before without issues. _Element is obviously not a built-in. from lxml.etree import _Element _Element.new_attr = 54 results in: TypeError: can't set attributes of built-in/extension type 'lxml.etree._Element' 回答1: _Element is implemented in Cython. As Steve Holden explains (my emphasis), The problem is that extension types' attributes are determined by the layout of the object's slots and forever fixed in the C code that implements them: the slots can

Patching decorator in setUp or setUpClass in TestCases does not work

廉价感情. 提交于 2021-02-10 13:36:30
问题 I am trying to patch some functions during either the setUp or setUpClass methods of a unittest.TestCase subclass. Given a module patch_me_not.py # patch_me_not.py def patch_me(at): print('I am not patched at {}.'.format(at)) def patch_me_not(at): patch_me(at) The following script produces more output that I would expect. # main.py import unittest from unittest.mock import patch from patch_me_not import patch_me_not @patch('patch_me_not.patch_me', lambda x: None) class PatchMeNotTests

Python: How to mock SQLAlchemy event handlers (using mock.unittest)

谁都会走 提交于 2021-02-10 12:13:55
问题 So I have a SQLAlchemy model that has an event listener: class User(db.Model): id = db.Column(db.Integer, primary_key=True) @event.listens_for(User, "after_insert") @event.listens_for(User, "after_update") def do_something(mapper, connection, self): foo = SomeClass(self) foo.do_something_to_database() And I have a unit test that needs to update/insert the Model @patch('my_package.user.do_something') def test_user(mock_do_something): user = User() # This insert will invoke 'do_something' via

Mockito get all mocked objects

强颜欢笑 提交于 2021-02-10 12:07:29
问题 I want to get the list of all mocked objects. Using a previous version of Mockito I could do this: List<Object> createdMocks = new LinkedList<Object>(); MockingProgress progress = new ThreadSafeMockingProgress(); progress.setListener(new CollectCreatedMocks(createdMocks)); These listeners are removed in the latest 2.8 version of Mockito, is there any alternative for it? 回答1: Since Mockito 2.x this has been replaced by implementations of org.mockito.listeners.MockitoListener which you engage

Python: How to mock SQLAlchemy event handlers (using mock.unittest)

时间秒杀一切 提交于 2021-02-10 12:06:18
问题 So I have a SQLAlchemy model that has an event listener: class User(db.Model): id = db.Column(db.Integer, primary_key=True) @event.listens_for(User, "after_insert") @event.listens_for(User, "after_update") def do_something(mapper, connection, self): foo = SomeClass(self) foo.do_something_to_database() And I have a unit test that needs to update/insert the Model @patch('my_package.user.do_something') def test_user(mock_do_something): user = User() # This insert will invoke 'do_something' via