Home > OS >  Why are my tests failing after importing a function from a module?
Why are my tests failing after importing a function from a module?

Time:05-11

This is my first programming question to ask on here. I am also a beginner python programmer.

I'm making a test program in order to test a function from another program (plates). Basically the main program (plates) asks for a string and then checks the string against a bunch of functions to see if the string is valid or not.

This is a small snippet of the test program from the very top:

from plates import is_valid

def main():
    test_length_check()
    test_letter_start()
    test_punc_check()
    test_zero_lead()
    test_seq_check()


def test_length_check():
    assert is_valid("CS50") == True
    assert is_valid("C") == False
    assert is_valid("TOOLONGBOI") == False

This is the function that I want to test from the main method (plates):

def main():
    plate = input("Plate: ")
    if is_valid(plate):  # if is_valid == True
        print("Valid")
    else:
        print("Invalid")
        # print(arg_list_val)  # If invalid, shows what tests have failed

def is_valid(s):
    arg_list_val = [length_check(s), letter_start(s), punc_check(s),
                    zero_lead(s), seq_check(s)]  # Bool list of all 4 req checks
    if all(arg_list_val):  # If and ONLY if all req checks are True
        return True

My tests are coming out like this:

test_plates.py::test_length_check FAILED                                 [ 20%]
test_plates.py:10 (test_length_check)
None != False

Expected :False
Actual   :None
<Click to see difference>

def test_length_check():
        assert is_valid("CS50") == True
>       assert is_valid("C") == False
E       AssertionError: assert None == False
E           where None = is_valid('C')

test_plates.py:13: AssertionError

All of my "actuals" are reporting "None" instead of the corresponding bools. What am I doing wrong?

The main program definitely works as intended.. I'm just practicing unit tests. If you know, you know ;)

CodePudding user response:

As already noted by Matthias, when a function ends without explicitly returning something it returns None by default. So your assertions succeed as long as you are checking for True, but fail when you check for False: False != None.

Add a return False to your function, or modify your assertions.

  • Related