Skip to content

Commit

Permalink
Add test for failed unittest test
Browse files Browse the repository at this point in the history
  • Loading branch information
ollipa committed Dec 14, 2021
1 parent e8ffc9c commit c5c736e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- name: Generate report
run: |
poetry run coverage run --parallel-mode --source flexmock -m unittest tests/test_flexmock.py
poetry run coverage run --parallel-mode --source flexmock tests/test_unittest.py
poetry run coverage run --parallel-mode --source flexmock tests/test_doctest.py
poetry run coverage run --parallel-mode --source flexmock -m pytest tests/test_pytest.py
poetry run coverage combine
Expand Down
18 changes: 15 additions & 3 deletions src/flexmock/_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ def _patch_test_result(klass: Type[Any]) -> None:


def _patch_stop_test(klass: Type[unittest.TextTestResult]) -> None:
"""Add call to teardown in klass.stopTest method.
"""Patch TextTestResult class stopTest method and add call to flexmock
teardown.
If the test failed already before flexmock teardown, nothing is done.
However if the test was successful before flexmock teardown and flexmock
assertions fail, flexmock updates the test result to failed by calling
addFailure method.
Args:
klass: the class whose stopTest method needs to be decorated.
Expand Down Expand Up @@ -65,10 +71,16 @@ def decorated(self: unittest.TextTestResult, test: unittest.TestCase) -> None:


def _patch_add_success(klass: Type[unittest.TextTestResult]) -> None:
"""Modify the addSuccess method of the klass for flexmock.
"""Patch the addSuccess method of the TextTestResult class.
TextTestResult addSuccess method is replaced and the original addSuccess
method is called after flexmock teardown patched stopTest method.
An attribute is set in the replaced addSuccess method to indicate if the
test was successful before flexmock teardown was called.
Args:
klass: the class whose stopTest method needs to be decorated.
klass: the class whose addSuccess method needs to be decorated.
"""

@wraps(klass.addSuccess)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test unittest integration."""
# pylint: disable=missing-docstring,no-self-use
import sys
import unittest

from flexmock import flexmock


class TestUnitTestIntegration(unittest.TestCase):
"""Flexmock unittest integration specific tests."""

def test_failed_test_case(self):
"""This tests that after a successful tests, failing flexmock assertions
will change the test result from successful to failed.
"""
flexmock().should_receive("this_test_should_fail").once()


if __name__ == "__main__":
EXPECTED_FAILURES = 1
EXPECTED_TESTS = 1
test = unittest.main(exit=False)

if len(test.result.failures) == EXPECTED_FAILURES and test.result.testsRun == EXPECTED_TESTS:
sys.exit(0) # OK
sys.exit(1)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ whitelist_externals=

commands =
python -m unittest tests/test_flexmock.py
python tests/test_unittest.py
python tests/test_doctest.py
python -c "from twisted.scripts.trial import run; run();" tests/test_flexmock.py
zope-testrunner --test-path=./ --test-file-pattern=test_flexmock --verbose
Expand Down

0 comments on commit c5c736e

Please sign in to comment.