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): ..
I am struggling to understand how to stub a class / mock all the methods from a class dependency in Python & pytest. The listing below shows the class that I am testing. It has two internal dependencies: OWMProxy and PyOwmDeserializer. Class Under Test class OWM: def __init__(self, api_key: str, units: WeatherUnits) -> None: self._api_key ..
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 ..
Let’s say that I have a class, this class have some attributes and some methods. This methods don’t have any parameters or any return, they only work by changing the attributes of the class using the ‘self.’ property. How can I test these types of methods? Let’s create the imaginary class Foo to serve as ..
I would like to test by mocking an instance’s attribute, but I do not have access to the instance beforehand. How can I mock the attribute without it? Here is my minimum reproducible code. # test.py class Foo: def __init__(self, x): self.x = x def bar(): return Foo(1).x + 1 def test_bar(mocker): mocker.patch(‘test.Foo.x’, 2) assert ..
I am going to start a project in Python which will deal with a lot of database operations and this means I have to write tons of unit tests. I was reading about mocking database connections, cursors and executing sql queries for my unit tests (that are going to come up). I hit upon different ..
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 ..
In normal grpc communications, grpc automatically creates a context to go with the request. How do I create a mock context to pass with my unit test call, so my servicing function has an object to manipulate? Currently I have the code skip the context calls if it receives a NoneType as context, but that ..
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 ..
I’m trying to patch a class and its method, this is an example that what I’m trying to do: class Car: def __init__(self, color): self.color = color def show_color(self): return self.color I have this class and I want to patch the class, and its method separately, I didn’t create a fake class, because in my ..