def test_database_service(mock_db_service: MockFixture): # Configure the mock object mock_db_service.return_value.connect.return_value = True mock_db_service.return_value.insert.return_value = 1 # Use the mock object in testing db = DatabaseService() result = db.save_data("testdata") # Assert that the mock object was called correctly assert result == 1 mock_db_service.assert_called_once() mock_db_service.return_value.connect.assert_called_once() mock_db_service.return_value.insert.assert_called_once()
def test_rest_api_client(mock_rest_client: MockFixture): # Configure the mock object mock_rest_client.return_value.get.return_value = {"data": "testdata"} # Use the mock object in testing client = RestClient() result = client.get_data() # Assert that the mock object was called correctly assert result == {"data": "testdata"} mock_rest_client.assert_called_once() mock_rest_client.return_value.get.assert_called_once()In both examples, the MockFixture call is used to create a mock object for a service (database or REST API client) and then configure the mock object to return specific values when certain methods are called. The mock object is then used in testing, and assertions are made to ensure that it was called correctly. The package library for pytest_mock is "pytest-mock".