PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] Why are my tests failing after importing a function from a module?

 August 24, 2022     module, python, unit-testing     No comments   

Issue

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 ;)


Solution

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.



Answered By - gimix
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing