为什么从模块导入函数后我的测试失败了?
Why are my tests failing after importing a function from a module?
这是我要在这里提出的第一个编程问题。我也是初学者python程序员
我正在制作一个测试程序以测试另一个程序(板)的功能。基本上,主程序(plates)请求一个字符串,然后根据一堆函数检查该字符串以查看该字符串是否有效。
这是最上面的测试程序的一小段:
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
这是我要从主要方法(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
我的测试结果是这样的:
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
我所有的“实际值”都报告“None”而不是相应的布尔值。我做错了什么?
主程序绝对按预期运行。我只是在练习单元测试。
如果你知道,你就知道 ;)
正如 Matthias 已经指出的那样,当一个函数结束时没有明确返回某些东西时,它默认 returns None
。因此,只要您检查 True
,您的断言就会成功,但当您检查 False
时,您的断言就会失败:False != None
.
向您的函数添加 return False
,或修改您的断言。
这是我要在这里提出的第一个编程问题。我也是初学者python程序员
我正在制作一个测试程序以测试另一个程序(板)的功能。基本上,主程序(plates)请求一个字符串,然后根据一堆函数检查该字符串以查看该字符串是否有效。
这是最上面的测试程序的一小段:
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
这是我要从主要方法(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
我的测试结果是这样的:
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
我所有的“实际值”都报告“None”而不是相应的布尔值。我做错了什么?
主程序绝对按预期运行。我只是在练习单元测试。 如果你知道,你就知道 ;)
正如 Matthias 已经指出的那样,当一个函数结束时没有明确返回某些东西时,它默认 returns None
。因此,只要您检查 True
,您的断言就会成功,但当您检查 False
时,您的断言就会失败:False != None
.
向您的函数添加 return False
,或修改您的断言。