Skip to content

Commit

Permalink
additional coverage of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bandophahita committed Sep 22, 2023
1 parent d7bfc95 commit 6bcd0c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
57 changes: 54 additions & 3 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,11 @@ def test_raises_assertionerror_if_one_fails(self, Tester) -> None:
(FakeQuestion(), IsEqualTo(True)),
).perform_as(Tester)

def test_stops_at_first_failure(self, Tester) -> None:
def test_stops_at_first_failure(self, Tester, caplog) -> None:
mock_question = FakeQuestion()

caplog.set_level(logging.INFO)

with pytest.raises(AssertionError):
SeeAllOf(
(mock_question, IsEqualTo(True)),
Expand All @@ -604,8 +606,24 @@ def test_stops_at_first_failure(self, Tester) -> None:
).perform_as(Tester)

assert mock_question.answered_by.call_count == 2
assert [r.msg for r in caplog.records] == [
"Tester sees if all of 4 tests pass:",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
" Tester sees if fakeQuestion is equal to <False>.",
" ... hoping it's equal to <False>.",
" => <False>",
# don't be fooled! the next few lines do not have commas on purpose
" ***ERROR***\n"
"\n"
"AssertionError: \n"
"Expected: <False>\n"
" but: was <True>\n",
]

def test_passes_if_all_pass(self, Tester) -> None:
def test_passes_if_all_pass(self, Tester, caplog) -> None:
caplog.set_level(logging.INFO)
# test passes if no exception is raised
SeeAllOf(
(FakeQuestion(), IsEqualTo(True)),
Expand All @@ -614,6 +632,22 @@ def test_passes_if_all_pass(self, Tester) -> None:
(FakeQuestion(), IsEqualTo(True)),
).perform_as(Tester)

assert [r.msg for r in caplog.records] == [
"Tester sees if all of 4 tests pass:",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
]

def test_describe(self) -> None:
test = (FakeQuestion(), IsEqualTo(True))
tests = (
Expand Down Expand Up @@ -679,9 +713,11 @@ def test_raises_assertionerror_if_none_pass(self, Tester) -> None:

assert "did not find any expected answers" in str(actual_exception)

def test_stops_at_first_pass(self, Tester) -> None:
def test_stops_at_first_pass(self, Tester, caplog) -> None:
mock_question = FakeQuestion()

caplog.set_level(logging.INFO)

SeeAnyOf(
(mock_question, IsEqualTo(False)),
(mock_question, IsEqualTo(True)), # <--
Expand All @@ -690,6 +726,21 @@ def test_stops_at_first_pass(self, Tester) -> None:
).perform_as(Tester)

assert mock_question.answered_by.call_count == 2
assert [r.msg for r in caplog.records] == [
"Tester sees if any of 4 tests pass:",
" Tester sees if fakeQuestion is equal to <False>.",
" ... hoping it's equal to <False>.",
" => <False>",
# don't be fooled! the next few lines do not have commas on purpose
" ***ERROR***\n"
"\n"
"AssertionError: \n"
"Expected: <False>\n"
" but: was <True>\n",
" Tester sees if fakeQuestion is equal to <True>.",
" ... hoping it's equal to <True>.",
" => <True>",
]

def test_passes_with_one_pass(self, Tester) -> None:
# test passes if no exception is raised
Expand Down
6 changes: 3 additions & 3 deletions tests/useful_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def get_mock_action_class() -> Any:
class FakeAction(Action):
def __new__(cls, *args, **kwargs):
rt = mock.create_autospec(FakeAction, instance=True)
rt.describe.return_value = None
rt.describe.return_value = "FakeAction"
return rt

return FakeAction
Expand All @@ -18,7 +18,7 @@ def get_mock_question_class() -> Any:
class FakeQuestion(Question):
def __new__(cls, *args, **kwargs):
rt = mock.create_autospec(FakeQuestion, instance=True)
rt.describe.return_value = None
rt.describe.return_value = "FakeQuestion"
rt.answered_by.return_value = True
return rt

Expand All @@ -30,7 +30,7 @@ class FakeResolution(Resolution):
def __new__(cls, *args, **kwargs):
rt = mock.create_autospec(FakeResolution, instance=True)
rt.resolve.return_value = rt
rt.describe.return_value = None
rt.describe.return_value = "FakeResolution"
return rt

return FakeResolution
Expand Down

0 comments on commit 6bcd0c2

Please sign in to comment.