pytest-mock

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

How to remove a library with monkeypatch or mock in pytest?

ぃ、小莉子 提交于 2020-12-15 06:38:22
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a

How to remove a library with monkeypatch or mock in pytest?

久未见 提交于 2020-12-15 06:37:51
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a

Mock exception raised from class method with side effect gives 'did not raise'

时光怂恿深爱的人放手 提交于 2020-01-07 07:59:09
问题 Note: This question is based on a previous question I asked but modified to be different according to this answer . Using side_effect, I am trying to raise a 'URLError' exception when a mock is called but I get a DID NOT RAISE error that I do not understand. I have a Query class with a class method make_request_and_get_response which can raise several exceptions. I am not catching the 'URLError' exception within the get_response_from_external_api method in main.py , so I should be able to

How to mock a imported object with pytest-mock or magicmock

穿精又带淫゛_ 提交于 2019-12-22 05:23:29
问题 I am trying to understand the mock/monkeypatch/pytest-mock capabilities. Let me know if this is possible. If not could you please suggest how I can test this code. My code structure: / ./app ../__init__.py ../some_module1 .../__init__.py ../some_module2 .../__init__.py ./tests ../test_db.py The /app/__init__.py is where my application (a Flask application if it helps) is started along with initializing a database connection object to a MongoDB database: # ... def create_app(): # ... return

How to mock a imported object with pytest-mock or magicmock

孤人 提交于 2019-12-05 06:09:52
I am trying to understand the mock/monkeypatch/pytest-mock capabilities. Let me know if this is possible. If not could you please suggest how I can test this code. My code structure: / ./app ../__init__.py ../some_module1 .../__init__.py ../some_module2 .../__init__.py ./tests ../test_db.py The /app/__init__.py is where my application (a Flask application if it helps) is started along with initializing a database connection object to a MongoDB database: # ... def create_app(): # ... return app db_conn = DB() The some_module1 and some_module import the db_conn object and use it as part of their