Skip to content

TestResult -- Test Result Proxy

API

The module for the test result.

TestResult dataclass

Test result for a single test case.

Parameters:

Name Type Description Default
default_name str

The default name of the test.

required
name str | None

The name of the test.

None
score float | None

The score obtained by the test.

None
max_score float | None

The max score can obtained for the test.

None
weight int | None

The weight of the test.

None
extra_points float | None

The extra points of the test, applied when the test is passed.

None
errors List[ErrorFormatter]

The errors of the test.

list()
pass_status PassStateType | None

The pass status of the test.

None
hidden bool

Whether the test is hidden.

False
descriptions List[str]

The descriptions of the test.

list()
Source code in src/gapper/core/test_result.py
@dataclass
class TestResult:
    """Test result for a single test case.

    :param default_name: The default name of the test.
    :param name: The name of the test.
    :param score: The score obtained by the test.
    :param max_score: The max score can obtained for the test.
    :param weight: The weight of the test.
    :param extra_points: The extra points of the test, applied when the test is passed.
    :param errors: The errors of the test.
    :param pass_status: The pass status of the test.
    :param hidden: Whether the test is hidden.
    :param descriptions: The descriptions of the test.
    """

    default_name: str
    name: str | None = None
    score: float | None = field(default=None)
    max_score: float | None = field(default=None)
    weight: int | None = field(default=None)
    extra_points: float | None = field(default=None)
    errors: List[ErrorFormatter] = field(default_factory=list)
    pass_status: PassStateType | None = field(default=None)
    hidden: bool = False
    descriptions: List[str] = field(default_factory=list)

    @property
    def rich_test_name(self) -> str:
        """The name of the test, with the default name prepended if the name is unset."""
        name = f"{self.name} " if self.name else ""
        default_name = self.default_name
        return "{}{}".format(name, default_name)

    @property
    def rich_test_output(self) -> str:
        """The description output of the test, with the score and max score appended if set."""
        pass_status_msg = self.pass_status.capitalize() if self.pass_status else ""

        description_info = indent("\n".join(self.descriptions), " " * 2)
        description_msg = (
            "Description(s): \n" + description_info if self.descriptions else ""
        )

        error_info = indent(
            "\n".join(err.format() for err in self.errors),
            " " * 2,
        )
        error_msg = "Error(s): \n" + error_info if self.errors else ""

        messages = list(filter(bool, [pass_status_msg, description_msg, error_msg]))
        if len(messages) == 0:
            return "<No Description>"
        else:
            return "\n".join(messages)

    def set_name(self, name: str) -> None:
        """Set the name of the test.

        This does not modify the default name.
        """
        self.name = name

    def add_description(self, *detail: str) -> None:
        """Add a description to the test.

        New descriptions are added as newlines to the end of the existing descriptions.

        :param detail: The description to add.
        """
        self.descriptions.extend(detail)

    def set_descriptions(self, detail: Iterable[str]) -> None:
        """Set the description of the test.

        This overrides all the existing descriptions.

        :param detail: The description to set.
        """
        self.descriptions = list(detail)

    def set_hidden(self, hidden: bool) -> None:
        """Set the hidden status of the test.

        :param hidden: Whether the test is hidden.
        """
        self.hidden = hidden

    def set_score(self, score: float) -> None:
        """Set the score of the test.

        :param score: The score to set.
        """
        self.score = score

    def set_max_score(self, max_score: float | None) -> None:
        """Set the max score of the test.

        :param max_score: The max score of this test case to set.
        """
        self.max_score = max_score

    def set_weight(self, weight: int | None) -> None:
        """Set the weight of the test.

        :param weight: The weight of this test case to set.
        """
        self.weight = weight

    def set_extra_points(self, score: float | None) -> None:
        """Set the extra points of the test.

        :param score: The extra points of this test case to set.
        """
        self.extra_points = score

    def add_error(self, error: ErrorFormatter, set_failed: bool = True) -> None:
        """Add an error to the test.

        :param error: The error to add.
        :param set_failed: Whether to set the pass status to failed.
        """
        self.errors.append(error)
        if set_failed:
            self.set_pass_status("failed")

    @property
    def is_passed(self) -> bool:
        """Whether the test passed."""
        return self.pass_status == "passed"

    @property
    def is_pass_status_unset(self) -> bool:
        """Whether the pass status is unset."""
        return self.pass_status is None

    def set_pass_status(self, status: PassStateType) -> None:
        """Set the pass status of the test.

        :param status: The pass status to set.
        """
        self.pass_status = status

    def set_default_weight(self) -> None:
        """Set the weight of the test to 1."""
        self.weight = 1

is_pass_status_unset property

is_pass_status_unset: bool

Whether the pass status is unset.

is_passed property

is_passed: bool

Whether the test passed.

rich_test_name property

rich_test_name: str

The name of the test, with the default name prepended if the name is unset.

rich_test_output property

rich_test_output: str

The description output of the test, with the score and max score appended if set.

add_description

add_description(*detail: str) -> None

Add a description to the test.

New descriptions are added as newlines to the end of the existing descriptions.

Parameters:

Name Type Description Default
detail str

The description to add.

()
Source code in src/gapper/core/test_result.py
def add_description(self, *detail: str) -> None:
    """Add a description to the test.

    New descriptions are added as newlines to the end of the existing descriptions.

    :param detail: The description to add.
    """
    self.descriptions.extend(detail)

add_error

add_error(error: ErrorFormatter, set_failed: bool = True) -> None

Add an error to the test.

Parameters:

Name Type Description Default
error ErrorFormatter

The error to add.

required
set_failed bool

Whether to set the pass status to failed.

True
Source code in src/gapper/core/test_result.py
def add_error(self, error: ErrorFormatter, set_failed: bool = True) -> None:
    """Add an error to the test.

    :param error: The error to add.
    :param set_failed: Whether to set the pass status to failed.
    """
    self.errors.append(error)
    if set_failed:
        self.set_pass_status("failed")

set_default_weight

set_default_weight() -> None

Set the weight of the test to 1.

Source code in src/gapper/core/test_result.py
def set_default_weight(self) -> None:
    """Set the weight of the test to 1."""
    self.weight = 1

set_descriptions

set_descriptions(detail: Iterable[str]) -> None

Set the description of the test.

This overrides all the existing descriptions.

Parameters:

Name Type Description Default
detail Iterable[str]

The description to set.

required
Source code in src/gapper/core/test_result.py
def set_descriptions(self, detail: Iterable[str]) -> None:
    """Set the description of the test.

    This overrides all the existing descriptions.

    :param detail: The description to set.
    """
    self.descriptions = list(detail)

set_extra_points

set_extra_points(score: float | None) -> None

Set the extra points of the test.

Parameters:

Name Type Description Default
score float | None

The extra points of this test case to set.

required
Source code in src/gapper/core/test_result.py
def set_extra_points(self, score: float | None) -> None:
    """Set the extra points of the test.

    :param score: The extra points of this test case to set.
    """
    self.extra_points = score

set_hidden

set_hidden(hidden: bool) -> None

Set the hidden status of the test.

Parameters:

Name Type Description Default
hidden bool

Whether the test is hidden.

required
Source code in src/gapper/core/test_result.py
def set_hidden(self, hidden: bool) -> None:
    """Set the hidden status of the test.

    :param hidden: Whether the test is hidden.
    """
    self.hidden = hidden

set_max_score

set_max_score(max_score: float | None) -> None

Set the max score of the test.

Parameters:

Name Type Description Default
max_score float | None

The max score of this test case to set.

required
Source code in src/gapper/core/test_result.py
def set_max_score(self, max_score: float | None) -> None:
    """Set the max score of the test.

    :param max_score: The max score of this test case to set.
    """
    self.max_score = max_score

set_name

set_name(name: str) -> None

Set the name of the test.

This does not modify the default name.

Source code in src/gapper/core/test_result.py
def set_name(self, name: str) -> None:
    """Set the name of the test.

    This does not modify the default name.
    """
    self.name = name

set_pass_status

set_pass_status(status: PassStateType) -> None

Set the pass status of the test.

Parameters:

Name Type Description Default
status PassStateType

The pass status to set.

required
Source code in src/gapper/core/test_result.py
def set_pass_status(self, status: PassStateType) -> None:
    """Set the pass status of the test.

    :param status: The pass status to set.
    """
    self.pass_status = status

set_score

set_score(score: float) -> None

Set the score of the test.

Parameters:

Name Type Description Default
score float

The score to set.

required
Source code in src/gapper/core/test_result.py
def set_score(self, score: float) -> None:
    """Set the score of the test.

    :param score: The score to set.
    """
    self.score = score

set_weight

set_weight(weight: int | None) -> None

Set the weight of the test.

Parameters:

Name Type Description Default
weight int | None

The weight of this test case to set.

required
Source code in src/gapper/core/test_result.py
def set_weight(self, weight: int | None) -> None:
    """Set the weight of the test.

    :param weight: The weight of this test case to set.
    """
    self.weight = weight