Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test result shown as failed when a subtest fails #228

Merged
merged 3 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

@sebclrsn sebclrsn Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best way of doing it! :)

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