Category : pytest-mock

I am completing a python coding course from udemy, and have a project with a directory structure like: ├── LICENSE ├── README.md ├── __init__.py ├── requirements.txt ├── src │ ├── __init__.py │ └── milage_converter.py └── tests ├── __init__.py └── milage_converter_test.py Within milage_converter_test.py I am running some simple tests like: from py_exercises.src import milage_converter def test_base_milage_string_to_float(monkeypatch): ..

Read more

lets say I have the following assert statement with the pydantic models: assert my_schedule == [ Class.construct( student_id=student1.student1.id, property=Property.A, est_time=mock.ANY, ), Class.construct( student_id=student1.student1.id, property=Property.B, est_time=mock.ANY, ) ] Now all other attributes except est_time are equal. i,e: my_schedule = [ Class( student_id=student1.id, property=Property.A, est_time=datetime(2020,1,3), ), Class( student_id=student1.id, property=Property.B, est_time=datetime(2020,1,4), ) ] But the above assertion fails ..

Read more

I’m trying to mock 3 functions in my code and parameterize the two other variables. Here an example: @pytest.mark.parametrize(‘a, b’, [ (5, 8), ]) @mock.patch(‘path_to_mocked_function3’) @mock.patch(‘path_to_mocked_function2’) @mock.patch(‘path_to_mocked_function1’) def test_function(self, mocked1, mocked2, mocked3, a, b): mocked1.return_value = a mocked2.return_value = None mocked3.return_value = b output = export(**self.args).to_json() self.assertEqual(output, 5) Now I want to parametrize a,b variables ..

Read more

I am trying to write unit test for the following function. def my_func(): _session, _engine = get_session_and_engine() with _session.begin(): # Some functionality pass return result My unit test file @mock.patch(‘core.my_func_file.get_session_and_engine’) def test_my_func(mock_get_func): mock_session = mock.Mock() mock_session.return_value = mock_session mock_session.__enter__ = mock.Mock(return_value=(mock_session, None)) mock_session.__exit__ = mock.Mock(return_value=None) mock_get_func.return_value = (mock_session, mock.Mock()) res = my_func() assert res is ..

Read more