簡體   English   中英

在 python pytest 中返回 None 而不是 False 或 True

[英]return None instead of False or True, in python pytest

我嘗試使用 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

但是當我簡單地使用時:

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

我運行 pytest 時的答案是: FAILED test_plates.py::test_alnum - AssertionError: assert None == True

為什么它返回None而不是TrueFalse

您的第一個 if 條件必須為真才能輸入您的代碼。 所以如果為假,程序會跳過所有代碼到function的末尾。 因為默認情況下,python function 返回無,這就是您的 function 所做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM