Maverick
Maverick

Reputation: 107

TDD with python

I need a little help guys, I'm doing a project for school and i'm running in to an issue with my code. Not sure where i'm going wrong. Here is a little bit about the project.

Requirements: Write a function called generate_prime_factors in the module prime.py. This function will accept an integer as an argument, and return a list of integers.

Step 1: Write a test that asserts that when generate_prime_factors is called with a data type that is not an integer (e.g. a string or float), a ValueError is raised. Write the appropriate code to solve this and then commit the changes.

Step 2: Write a test that asserts that when generate_prime_factors is called with 1, an empty list is returned. Solve & commit.

Step 3: Write a test that asserts that when generate_prime_factors is called with 2, the list [2] is returned. Solve & commit.

etc.....

this is my main prime.py:

    """
    generate prime factors fuctions
    """
    if user_input == 1:
        return prime_factors
    if user_input == 2:
        return prime_factors
    if isinstance(user_input, str):
        raise ValueError
    return prime_factors

and here is my test python script:

import pytest
from prime import generate_prime_factors # Imports the prime module (prime.py)

def test_not_integer():
    """
    A test that asserts that when `generate_prime_factors` is called with a
    data type that is not an integer (e.g. a string or float), a ValueError is
    raised.
    """
    prime_factors = []
    with pytest.raises(ValueError):
        generate_prime_factors('Hello World', prime_factors)

def test_generate_prime_factor_1():
    """
    A test that asserts that when `generate_prime_factors` is called with
    `1`, an empty list is returned.
    """
    prime_factors = []
    generate_prime_factors(1, prime_factors)
    assert prime_factors == []

def test_generate_prime_factor_2():
    """
    a test that asserts that when `generate_prime_factors` is called with
    `2`, the list `[2]` is returned.
    """
    prime_factors = []
    generate_prime_factors(2, prime_factors)
    assert prime_factors == [2]

enter image description here

as you can see from the image. I keep getting pytest error, seems to be coming from the def test_generate_prime_factors_2(): method as you can see from the image two pass and 1 fail

Upvotes: 0

Views: 293

Answers (1)

Sam
Sam

Reputation: 824

In your function, you're returning prime_factors.

But in your test, you're not using that return value.

Your prime_factor value in your test is never assigned, so it remains an empty list.

Upvotes: 2

Related Questions