简体   繁体   中英

return None instead of False or True, in python pytest

I try to write a small test for my code using pytest:

def is_valid(s):
    if 2 <= len(s) <= 6 and s[0:2].isalpha() and s.isalnum():
        for item in s:

            if item.isdigit():
                pos = s.index(item)
                if s[pos:].isdigit() and int(item) != 0:
                    return True
                else:
                    return False

        return True

but when I simply use:

import f
def test_word:
    assert f.is_valid('cs50') == True

the answer when I run pytest is: FAILED test_plates.py::test_alnum - AssertionError: assert None == True

Why does it return None instead of True or False ?

Your first if conditions must be true to enter your code. So if it is false, the program skip all the code to the end of the function. Since by default, a python function return None, that's what your function does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM