mocking

How to mock os.listdir to pretend files and directories in Python?

拥有回忆 提交于 2021-02-20 05:49:25
问题 I have a proprietary repository format and I'm trying to develop a Python module to process these repositories. Repo format goes as: /home/X/ | + alpha/ | + beta/ | + project.conf Here, X is a project. alpha and beta are folders inside this project and they represent groups in this project. A group is a container in this repo and what it represents is really not relevant for this question. The repo X also has files in its root level; project.conf is an example of such a file. I have a class

Date mock with two or more tests

拜拜、爱过 提交于 2021-02-19 08:17:07
问题 We are using jest for mocking. I have a function which will greet us based on the time that file looks like below: export default function getGreetingMessage() { const today = new Date(); const curHr = today.getHours(); if (curHr < 12) { return 'Good morning'; } else if (curHr < 18) { return 'Good afternoon'; } return 'Good evening'; } And My test file will look like below import getGreetingMessage from '../messages'; describe('messages', () => { function setup(date) { const DATE_TO_USE = new

Cannot use attach_mock with an autospec function mock

烈酒焚心 提交于 2021-02-19 07:27:19
问题 Library module: # mod.py def foo(): bar1("arg1") bar2("arg2x", "arg2y") def bar1(x): pass def bar2(x, y): pass Test module: # test_mod.py from mod import foo def test_foo(mocker): mock = mocker.MagicMock() mock.attach_mock(mocker.patch("mod.bar1"), "b1") mock.attach_mock(mocker.patch("mod.bar2", autospec=True), "b2") foo() mock.assert_has_calls( [ mocker.call.b1("arg1"), mocker.call.b2("arg2x", "arg2y"), ] ) The mocker fixture is from pytest-mock plugin. Execute the MCVE with python -m pytest

JUnit tests: Suppress enum constructor by mocking?

℡╲_俬逩灬. 提交于 2021-02-18 22:50:33
问题 I know that it is possible to mock a single enum(using How to mock an enum singleton class using Mockito/Powermock?), but I have like 1000 of enum values and they can call 5 different constructors. The enum values are often changing in development. I want to really mock only one or two for my JUnit test. I don't care about the rest, but they are still instantiated, which calls some nasty stuff, which loads the values for the enum from the file system. Yes I know It's very bad design. But for

JUnit tests: Suppress enum constructor by mocking?

一曲冷凌霜 提交于 2021-02-18 22:48:37
问题 I know that it is possible to mock a single enum(using How to mock an enum singleton class using Mockito/Powermock?), but I have like 1000 of enum values and they can call 5 different constructors. The enum values are often changing in development. I want to really mock only one or two for my JUnit test. I don't care about the rest, but they are still instantiated, which calls some nasty stuff, which loads the values for the enum from the file system. Yes I know It's very bad design. But for

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)

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

折月煮酒 提交于 2021-02-18 11:09:40
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

我是研究僧i 提交于 2021-02-18 11:08:05
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

浪子不回头ぞ 提交于 2021-02-18 11:06:50
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'

check unittest.mock call arguments agnostically w.r.t. whether they have been passed as positional arguments or keyword arguments

丶灬走出姿态 提交于 2021-02-18 11:06:29
问题 When a unittest.mock.Mock object has been called, I can check for the argument values with the exact signature of the call: from unittest.mock import Mock m = Mock() # creation of mock m('foo', bar='baz') # call to the mock m.assert_called_once_with('foo', bar='baz') # check call arguments Checking for a different signature with the same values will fail. E.g., if we check with 'baz' as a positional argument instead of a named argument, the assertion will fail: m.assert_called_once_with('foo'