Skip to content

Commit

Permalink
fix: test result shown as failed when a subtest fails (#228)
Browse files Browse the repository at this point in the history
* fix: test result shown as failed when a subtest fails
* test: adding test
* test: changing name variable
  • Loading branch information
yannpoupon authored Jan 20, 2023
1 parent 4e55627 commit ae4202a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/pykiso/test_result/text_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from shutil import get_terminal_size
from typing import List, Optional, TextIO, Union
from unittest import TextTestResult
from unittest.case import _SubTest

from ..test_coordinator.test_case import BasicTest
from ..test_coordinator.test_suite import BaseTestSuite
Expand Down Expand Up @@ -241,6 +242,22 @@ def addError(self, test: Union[BasicTest, BaseTestSuite], err: ExcInfoType) -> N
super().addError(test, err)
self._error_occurred = True

def addSubTest(
self,
test: Union[BasicTest, BaseTestSuite],
subtest: _SubTest,
err: ExcInfoType,
) -> None:
"""Set the error flag when an error occurs in a subtest.
:param test: running testcase
:param subtest: subtest runned
:param err: tuple returned by sys.exc_info
"""
super().addSubTest(test, subtest, err)
if err is not None:
self._error_occurred = True

def printErrorList(self, flavour: str, errors: List[tuple]):
"""Print all errors at the end of the whole tests execution.
Expand Down
23 changes: 22 additions & 1 deletion tests/test_text_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pytest

from pykiso.test_result.text_result import ResultStream
from pykiso.test_result.text_result import BannerTestResult, ResultStream

DUMMY_FILE = "dummy.txt"

Expand Down Expand Up @@ -94,3 +94,24 @@ def test_close(self, test_result_instance):

assert test_result_instance.stderr is None
assert test_result_instance.file is None


class TestBannerTestResult:
@pytest.fixture()
def banner_test_result_instance(self):
return BannerTestResult(sys.stderr, True, 1)

@pytest.mark.parametrize(
"error,result_expected", [((Exception), True), (None, False)]
)
def test_addSubTest(
self, mocker, banner_test_result_instance, error, result_expected
):
add_subTest_mock = mocker.patch("unittest.result.TestResult.addSubTest")
test_mock = mocker.patch("pykiso.test_coordinator.test_case.BasicTest")
subtest_mock = mocker.patch("unittest.case._SubTest")

banner_test_result_instance.addSubTest(test_mock, subtest_mock, error)

add_subTest_mock.assert_called_once_with(test_mock, subtest_mock, error)
assert banner_test_result_instance.error_occurred == result_expected

0 comments on commit ae4202a

Please sign in to comment.