Testing

  1. Create a branch testing in your gitlab repository
  2. Create a new folder called testing
  3. Copy the code bellow into testing/quadratic.py and try to understand it.
  4. Create a test module (testing/test_quadratic.py) with test for the quadratic_roots function
    1. Use pytest
    2. Test as many scenarios as possible
  5. Write down your learnings from this process in testing/learnings.md
    1. With an explanation for each test function that you wrote!
  6. Open a merge request with Manuel Reis as reviewer
Tip

You might need to explore the pytest documentation 1

Code to be tested

Here’s a simple implementation of the quadratic formula2

import math


def quadratic_roots(a: float, b: float, c: float) -> list[float]:
    """
    Finds real roots (x ∈ ℝ ) of the quadratic equation:

        ax² + bx + c = 0,  where a, b, c, x ∈ ℝ

    In case a is 0:
        - It solves the correspondent linear equation (b != 0)
        - Raises a ValueError otherwise (b == 0)

    In case the equation only has imaginary solutions:
        - Raises ValueError

    Parameters:
        a (float): Quadratic coefficient
        b (float): Linear Coefficient
        c (float): Constant term
    Returns:
        list[float]: Solutions
    """
    roots = list()
    if a == 0:
        if b == 0:
            raise ValueError("Invalid equation, 'a' and 'b' must be non-zero")
        roots.append(-c / b)
        return roots

    discriminant = b**2 - 4 * a * c

    if discriminant < 0:
        raise ValueError("No real solutions (descriminant < 0)")

    sqrt_d = math.sqrt(discriminant)
    x1 = (-b + sqrt_d) / (2 * a)
    x2 = (-b - sqrt_d) / (2 * a)
    roots.append(x1)
    roots.append(x2)
    return roots
Note

The structure of the repository should look like:

student-repo/
├── .git/
│  ...
│  ...
└── testing/
    ├── learnings.md
    ├── quadratic.py
    └── test_quadratic.py