def test_mock_method_calls(self): mock = Mock() mock() mock.method().attribute.method() mock.attribute.method() # mock_calls: inclusive, recursive; split by '()', good for callable mocks. self.assertEqual(mock.mock_calls, [ call(), call.method(), call.method().attribute.method(), call.attribute.method()]) # method_calls: exclusive, recursive; terminated by '()', good for non-callable mocks. self.assertEqual(mock.method_calls, [call.method(), call.attribute.method()])
def test_any_mock_calls_comparison_order(self): mock = Mock() d = datetime.now() class Foo(object): def __eq__(self, other): return False def __ne__(self, other): return True for d in datetime.now(), Foo(): mock.reset_mock() mock(d, foo=d, bar=d) mock.method(d, zinga=d, alpha=d) mock().method(a1=d, z99=d) expected = [ call(ANY, foo=ANY, bar=ANY), call.method(ANY, zinga=ANY, alpha=ANY), call(), call().method(a1=ANY, z99=ANY) ] self.assertEqual(expected, mock.mock_calls) self.assertEqual(mock.mock_calls, expected)
def test_corba_exception(self): self.corba_object.method.side_effect = CORBA.TRANSIENT with self.assertRaises(CORBA.TRANSIENT): self.corba_client.method() self.assertEqual(self.corba_object.mock_calls, [call.method()]) self.assertLogs(['DEBUG', 'ERROR'], ('method()', 'method failed with CORBA.TRANSIENT'))
def test_user_exception(self): self.corba_object.method.side_effect = CustomError with self.assertRaises(CustomError): self.corba_client.method() self.assertEqual(self.corba_object.mock_calls, [call.method()]) self.assertLogs(['DEBUG', 'DEBUG'], ('method()', 'method failed with .*CustomError'))
def test_mock_method_calls(self): mock = Mock() mock() mock.method().attribute.method() mock.attribute.method() # mock_calls: inclusive, recursive; split by '()', good for callable mocks. self.assertEqual(mock.mock_calls, [ call(), call.method(), call.method().attribute.method(), call.attribute.method() ]) # method_calls: exclusive, recursive; terminated by '()', good for non-callable mocks. self.assertEqual( mock.method_calls, [call.method(), call.attribute.method()])
def test_internal_server_error(self): self.corba_object.method.side_effect = InternalServerError with self.assertRaises(InternalServerError): self.corba_client.method() self.assertEqual(self.corba_object.mock_calls, [call.method()]) self.assertLogs( ['DEBUG', 'ERROR'], ('method()', 'method failed with .*InternalServerError'))
def test_args_encoded_py2(self): # Test strings in arguments are encoded before corba is called self.corba_client.method('ěščřž', 'ýáíé') self.assertEqual( self.corba_object.mock_calls, [call.method('ěščřž'.encode('utf-8'), 'ýáíé'.encode('utf-8'))]) self.assertIsInstance(self.corba_object.mock_calls[0][1][0], six.binary_type) self.assertIsInstance(self.corba_object.mock_calls[0][1][1], six.binary_type)
def test_any_mock_calls_comparison_order(self): mock = Mock() class Foo(object): def __eq__(self, other): pass def __ne__(self, other): pass for d in datetime.now(), Foo(): mock.reset_mock() mock(d, foo=d, bar=d) mock.method(d, zinga=d, alpha=d) mock().method(a1=d, z99=d) expected = [ call(ANY, foo=ANY, bar=ANY), call.method(ANY, zinga=ANY, alpha=ANY), call(), call().method(a1=ANY, z99=ANY) ] self.assertEqual(expected, mock.mock_calls) self.assertEqual(mock.mock_calls, expected)
def test_args_encoded(self): # Test strings in arguments are encoded before corba is called self.corba_client.method('ěščřž', 'ýáíé') self.assertEqual(self.corba_object.mock_calls, [call.method('ěščřž', 'ýáíé')])
def test_return_value(self): self.assertEqual(self.corba_client.method(), sentinel.result) self.assertEqual(self.corba_object.mock_calls, [call.method()])
def test_call_args(self): self.corba_client.method(sentinel.arg, sentinel.other_arg) self.assertEqual(self.corba_object.mock_calls, [call.method(sentinel.arg, sentinel.other_arg)])
def test_call(self): self.corba_client.method() self.assertEqual(self.corba_object.mock_calls, [call.method()]) self.assertLogs(['DEBUG', 'DEBUG'], ('method()', 'method returned'))
def testDownloadHttpd(): downloadHttpd() expected = [call.method("curl -o httpd-2.4.2.tar.gz -L %s"%HTTPD_URL), call.method("tar xvzf httpd-2.4.2.tar.gz -C %s"%DOWNLOAD_DIR)] assert mock.mock_calls == expected