def patch_click(monkeypatch):
""" Fixture that monkeypatches click printing functions
Patches functions like click.echo to capture all output in
a mock to use functions like mock.assert_called_once_with().
In the test you can directly import and use click functions
as a Mock. For example:
import click
click.assert_called_with()
"""
echo_mock = mock.Mock()
confirm_mock = mock.Mock()
monkeypatch.setattr(click, "echo", echo_mock)
monkeypatch.setattr(click, "confirm", confirm_mock)
return None
python类assert_called_once_with()的实例源码
def test_assert_called_once_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock('foo', 'bar', baz=2)
mock.assert_called_once_with('foo', 'bar', baz=2)
mock.reset_mock()
mock('foo', 'bar', baz=2)
self.assertRaises(
AssertionError,
lambda: mock.assert_called_once_with('bob', 'bar', baz=2)
)
def test_require_authentication_ok(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mock = mocked_authenticator.client.tokens.get_token_data
mock.return_value = {
"token": make_token_data(sudo_user_with_external_id, now, expired)
}
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 200
assert response.json == {"value": 1}
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
def test_require_authentication_unknown_user(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mock = mocked_authenticator.client.tokens.get_token_data
mock.return_value = {
"token": make_token_data(sudo_user_with_external_id, now, expired)
}
mock.return_value["token"]["user"]["id"] += "FAKE"
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 401
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
def test_require_authentication_no_keystone(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mocked_authenticator.client.tokens.get_token_data.side_effect = \
keystoneauth1.exceptions.ClientException
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 401
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
def test_assert_called_once_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock('foo', 'bar', baz=2)
mock.assert_called_once_with('foo', 'bar', baz=2)
mock.reset_mock()
mock('foo', 'bar', baz=2)
self.assertRaises(
AssertionError,
lambda: mock.assert_called_once_with('bob', 'bar', baz=2)
)
def test_assert_called_once_with_function_spec(self):
def f(a, b, c, d=None):
pass
mock = Mock(spec=f)
mock(1, b=2, c=3)
mock.assert_called_once_with(1, 2, 3)
mock.assert_called_once_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, b=3, c=2)
# Expected call doesn't match the spec's signature
with self.assertRaises(AssertionError) as cm:
mock.assert_called_once_with(e=8)
self.assertIsInstance(cm.exception.__cause__, TypeError)
# Mock called more than once => always fails
mock(4, 5, 6)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, 2, 3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
4, 5, 6)
def test_assert_called_once_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock('foo', 'bar', baz=2)
mock.assert_called_once_with('foo', 'bar', baz=2)
mock.reset_mock()
mock('foo', 'bar', baz=2)
self.assertRaises(
AssertionError,
lambda: mock.assert_called_once_with('bob', 'bar', baz=2)
)
def test_assert_called_once_with_function_spec(self):
def f(a, b, c, d=None):
pass
mock = Mock(spec=f)
mock(1, b=2, c=3)
mock.assert_called_once_with(1, 2, 3)
mock.assert_called_once_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, b=3, c=2)
# Expected call doesn't match the spec's signature
with self.assertRaises(AssertionError) as cm:
mock.assert_called_once_with(e=8)
self.assertIsInstance(cm.exception.__cause__, TypeError)
# Mock called more than once => always fails
mock(4, 5, 6)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, 2, 3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
4, 5, 6)
def test_fill_img_with_color(self, mock):
bgcolor = '#000'
self.img.bgcolor = bgcolor
self.img._fill_image_with_color()
mock.assert_called_once_with(
[(0, 0), self.img.image.size], fill=bgcolor)
def test_draw_header(self, mock):
self.img._draw_header()
mock.assert_called_once_with(45,
self.img.header)
def test_draw_para_with_content_that_fits(self, mock):
self.img._draw_para()
current_h = math.floor(
(self.img.height - self.img.para.height) / 2)
mock.assert_called_once_with(current_h, self.img.para)
def test_draw_para_content_with_header_that_does_not_fit(self, mock):
self.img.content.fits = False
self.img._draw_para()
header_with_padding_height = 2 * \
self.img.content.padding + self.img.header.height
current_h = header_with_padding_height
mock.assert_called_once_with(current_h, self.img.para)
def test_draw_para_content_without_header_that_does_not_fit(self, mock):
self.img.content.fits = False
self.img.header = None
self.img._draw_para()
current_h = self.img.content.padding
mock.assert_called_once_with(current_h, self.img.para)
def test_logout(mocked_authenticator, configure_model,
sudo_user_with_external_id):
now, expired = make_now_expired()
mock = mocked_authenticator.client.get_raw_token_from_identity_service
mock.return_value = make_token_data(
sudo_user_with_external_id, now, expired)
model = mocked_authenticator.authenticate("sudo", "sudo")
mocked_authenticator.client.tokens.revoke_token.assert_not_called()
mocked_authenticator.logout(model)
mocked_authenticator.client.tokens.revoke_token.assert_called_once_with(
model.model_id)
def test_assert_called_once_with_message(self):
mock = Mock(name='geoffrey')
self.assertRaisesRegex(AssertionError,
r"Expected 'geoffrey' to be called once\.",
mock.assert_called_once_with)
def test_assert_called_once_with_message(self):
mock = Mock(name='geoffrey')
self.assertRaisesRegex(AssertionError,
r"Expected 'geoffrey' to be called once\.",
mock.assert_called_once_with)
def test_assert_called_with_failure_message(self):
mock = NonCallableMock()
expected = "mock(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nNot called'
self.assertRaisesWithMsg(
AssertionError, message % (expected,),
mock.assert_called_with, 1, '2', 3, bar='foo'
)
mock.foo(1, '2', 3, foo='foo')
asserters = [
mock.foo.assert_called_with, mock.foo.assert_called_once_with
]
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, '2', 3, bar='foo'
)
# just kwargs
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, bar='foo'
)
# just args
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, 2, 3)"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, 2, 3
)
# empty
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo()"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), meth
)
def test_assert_called_with_failure_message(self):
mock = NonCallableMock()
expected = "mock(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nNot called'
self.assertRaisesWithMsg(
AssertionError, message % (expected,),
mock.assert_called_with, 1, '2', 3, bar='foo'
)
mock.foo(1, '2', 3, foo='foo')
asserters = [
mock.foo.assert_called_with, mock.foo.assert_called_once_with
]
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, '2', 3, bar='foo'
)
# just kwargs
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, bar='foo'
)
# just args
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, 2, 3)"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, 2, 3
)
# empty
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo()"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), meth
)
def test_assert_called_with_failure_message(self):
mock = NonCallableMock()
expected = "mock(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nNot called'
self.assertRaisesWithMsg(
AssertionError, message % (expected,),
mock.assert_called_with, 1, '2', 3, bar='foo'
)
mock.foo(1, '2', 3, foo='foo')
asserters = [
mock.foo.assert_called_with, mock.foo.assert_called_once_with
]
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, '2', 3, bar='foo'
)
# just kwargs
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(bar='foo')"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, bar='foo'
)
# just args
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, 2, 3)"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual),
meth, 1, 2, 3
)
# empty
for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')"
expected = "foo()"
message = 'Expected call: %s\nActual call: %s'
self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), meth
)