Pytest-cov: How can I increase coverage?

23 Views Asked by At

I have written python code (with Object-Oriented Design) that takes a string and a floating-point number, compares the length of the string to the value of the float, then outputs the lesser of the two values:

input.py:

class Input:
    def __init__(self) -> None:
        """
        Initialize the input string.
        """
        self._input_str: list[str] = []

    def get(self) -> list[str]:
        """
        Split the input into two components:
        the movie name, and the cap on the cost.
        """
        input_str = input().split()
        return input_str

cost.py:

class Cost:
    def __init__(self, s: str, c: float) -> None:
        """
        Initialize the two components of the input string.
        """
        self._s = s
        self._c = c

    @property
    def string(self) -> tuple[str, float]:
        """
        Get the properties of the two components.
        """
        return self._s, self._c

    @string.setter
    def string(self, newS: str, newC: float) -> None:
        """
        Set the values of the two components.
        """
        self._s = newS
        self._c = newC

    def calculate(self, s: str, c: float) -> float:
        """
        Calculates cost as the length of the movie title,
        then compares this value with the cap. The smallest
        of the two values is determined to be the final cost.
        """
        cost = len(s)
        final_cost = min(cost, c)

        return final_cost

main.py:

from input import Input
from cost import Cost
    
if __name__ == "__main__":
   """
   Calls the Input class to get the input string,
   then calls the Cost class to calculate the cost
   given the input string.
   """
   user_input = Input()
   input_str = user_input.get()
   s, c = input_str[0], float(input_str[1])
    
   cost = Cost(s, c)
   final_cost = cost.calculate(s, c)
    
   # Output the result
   print(final_cost)

I'm trying to test this code with unit and hypothesis testing, and am using pytest-cov to generate a coverage report on it. Here is the test file:

test.py:

import math
import unittest
from cost import Cost
from hypothesis import given, settings, Verbosity
import hypothesis.strategies as some


class Test(unittest.TestCase):
    @given(s=some.text(), c=some.floats(allow_infinity=True, allow_nan=True))
    @settings(max_examples=200, verbosity=Verbosity.verbose, derandomize=True)
    def test(self, s: str, c: float) -> None:
        cost = Cost(s, c)
        final_cost = cost.calculate(s, c)

        title_len = float(len(s))

        if math.isnan(c):
            self.assertEqual(final_cost, title_len)
        elif c < 0:
            self.assertEqual(final_cost, c)
        else:
            if title_len < c:
                self.assertEqual(final_cost, title_len)
            else:
                self.assertEqual(final_cost, c)


if __name__ == '__main__':
    unittest.main()

How can I increase the coverage? Currently, it is at 89%, but I'd like it to get to 98%. As far as I understand, the float can include negative values, and hold the values of infinity and 'nan', and the string can be of any length and include any character.

0

There are 0 best solutions below