def test_mocker_with_exception(re_usable_db_mocker): #making mock db_service to raise exception # db_service raise exception when it called re_usable_db_mocker.side_effect = Exception("oh noes!") with raises(Exception): #no output for below #becuase when it call db_service , exception occurs count_service('foo')
def test_simple_mocking(mocker): """ pytest-mock provides a fixture for easy, self-cleaning mocking """ # mock_db_service is a mock of the other_code.services.db_service function, if other_code.services.db_service is # called, mock_db_service will be called instead. mock_db_service = mocker.patch("other_code.services.db_service") # this is the mock data we want to be returned instead of the data from the database. mock_data = [(0, "fake row", 0.0)] # we set the return value of mock_db_service (and so other_code.services.db_service) as the data we want to be # returned mock_db_service.return_value = mock_data print("\n(Calling count_service with the DB mocked out...)") # other_code.services.db_service is called in count_service, it counts how much results are retrieved from the # call of other_code.services.db_service, as mock_data is returned, and its only one row, c is equal to 1 c = count_service("foo") # check that we really called our mock_db_service mock with the right argument mock_db_service.assert_called_with("foo") # check the result (only one row in mock_data => c == 1) assert c == 1
def test_simple_mocking(mocker): """ pytest-mock provides a fixture for easy, self-cleaning mocking :param mocker: :return: """ mock_db_service = mocker.patch("other_code.services.db_service", autospec=True) mock_data = [(0, "fake row", 0.0)] mock_db_service.return_value = mock_data print("\n(Calling countservice with the DB mocked out...)") c = count_service('foo') mock_db_service.assert_called_with('foo') assert c == 1
def test_simple_mocking(mocker): """ pytest-mock provides a fixture for easy, self-cleaning mocking """ mock_db_service = mocker.patch("other_code.services.db_service", autospec=True) mock_data = [ (0, 'fake row', 0.0), ] mock_db_service.return_value = mock_data print "\nCalling count_service with the DB mocked out..." c = count_service("foo") mock_db_service.assert_called_with("foo") assert c == 1
def test_simple_mocking(mocker): """ pytest-mock provides a fixture for easy, self-cleaning mocking """ #mocking : mock_db_servirce is a mock of db_service mock_db_service = mocker.patch("other_code.services.db_service", autospec=True) mock_data = [(0, "fake row", 0.0)] #changing return value of db_service to [(0, "fake row", 0.0)] mock_db_service.return_value = mock_data print("\n(Calling count_service with the DB mocked out...)") c = count_service("foo") # uses db_service internally # check whter db_service(mock) called with input as foo mock_db_service.assert_called_with("foo") # assert c == 1
def test_mocker_with_exception(re_usable_db_mocker): re_usable_db_mocker.side_effect = Exception("Oh noes!") with raises(Exception): count_service("foo")
def test_re_usable_mocker(re_usable_db_mocker): c = count_service("foo") re_usable_db_mocker.assert_called_with("foo") assert c == 1
def test_re_usable_mocker(re_usable_mocker): c = count_service('foo') re_usable_mocker.assert_called_with('foo') assert c == 1
def test_re_usable_mocker(re_usable_db_mocker): c = count_service('foo') # uses db_service internally #cofirm whter count_service used db_service('foo') re_usable_db_mocker.assert_called_with( 'foo') # check wheter db_service called with foo assert c == 1