Skip to content

Commit

Permalink
♻️ Uses pytest.raises match shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Nov 29, 2020
1 parent 50af55e commit be35827
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class TestEntryPoint:
def test_main_no_token(self):
"""Argument `--github-token` is required."""
argv = ["src/entrypoint.py"]
with patch_sys_argv(argv), pytest.raises(SystemExit) as ex_info:
with patch_sys_argv(argv), pytest.raises(
SystemExit, match=f"{signal.SIGINT.value}"
):
entrypoint.main()
assert ex_info.value.args == (signal.SIGINT.value,)

def test_main(self):
argv = ["src/entrypoint.py", "--github-token", "TOKEN"]
Expand Down Expand Up @@ -81,10 +82,9 @@ def test_main_parallel_finished(self):
def test_try_main(self):
with mock.patch(
"entrypoint.main", side_effect=Exception
) as m_main, pytest.raises(SystemExit) as ex_info:
) as m_main, pytest.raises(SystemExit, match=f"{entrypoint.ExitCode.FAILURE}"):
entrypoint.try_main()
assert m_main.call_args_list == [mock.call()]
assert ex_info.value.args == (entrypoint.ExitCode.FAILURE,)

def test_run_coveralls_github_token(self):
"""Simple case when Coveralls.wear() returns some results."""
Expand Down Expand Up @@ -144,10 +144,11 @@ def test_run_coveralls_wear_error_twice(self):
CoverallsException("Error 1"),
CoverallsException("Error 2"),
)
with patch_coveralls_wear() as m_wear, pytest.raises(SystemExit) as ex_info:
with patch_coveralls_wear() as m_wear, pytest.raises(
SystemExit, match=f"{entrypoint.ExitCode.FAILURE}"
):
m_wear.side_effect = side_effect
entrypoint.run_coveralls(repo_token="TOKEN")
assert ex_info.value.args == (entrypoint.ExitCode.FAILURE,)

def test_status_code_422(self):
"""
Expand All @@ -157,7 +158,7 @@ def test_status_code_422(self):
"""
status_code = 422
with patch_requests_post(status_code=status_code) as m_post, pytest.raises(
SystemExit
SystemExit, match=f"{entrypoint.ExitCode.FAILURE}"
), patch_log() as m_log:
entrypoint.run_coveralls(repo_token="TOKEN")
# coveralls package will retry once per service we call it with
Expand Down Expand Up @@ -231,7 +232,7 @@ def test_post_webhook_error(self):
environ = {}
with patch_requests_post(json_response) as m_post, patch_os_envirion(
environ
), pytest.raises(AssertionError) as ex_info:
), pytest.raises(AssertionError, match=f"{json_response}"):
entrypoint.post_webhook(repo_token)
assert m_post.call_args_list == [
mock.call(
Expand All @@ -243,7 +244,6 @@ def test_post_webhook_error(self):
},
)
]
assert ex_info.value.args == (json_response,)

@pytest.mark.parametrize(
"value,expected",
Expand All @@ -269,13 +269,11 @@ def test_str_to_bool(self, value, expected):
@pytest.mark.parametrize("value", ["", "yesn't"])
def test_str_to_bool_value_error(self, value):
"""Other unrecognised string values raise a `ValueError`."""
with pytest.raises(ValueError) as ex_info:
with pytest.raises(ValueError, match=f"{value} is not a valid boolean value"):
entrypoint.str_to_bool(value)
assert ex_info.value.args == (f"{value} is not a valid boolean value",)

@pytest.mark.parametrize("value", [None, 0])
def test_str_to_bool_attribute_error(self, value):
"""Other unrecognised non-string values raise an `AttributeError`."""
with pytest.raises(AttributeError) as ex_info:
with pytest.raises(AttributeError, match=" object has no attribute 'lower'"):
entrypoint.str_to_bool(value)
assert ex_info.value.args[0].endswith(" object has no attribute 'lower'")

0 comments on commit be35827

Please sign in to comment.