Python测试用例根本不运行,不返回任何内容

2024-04-20 12:03:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个函数,可以把任何输入转换成拉丁语。假设输入将被传递到函数中,因此此时需要用户输入。我正在尝试找出我的测试用例,但只收到以下输入:

进程已完成,退出代码为0

有人能解释这是为什么吗?我在下面概述了函数和测试函数的代码:

功能

def pig_latinify(word):
    """
    takes input from user
    check IF there is a vowel at beginning
        do this
    ELSE
        do this
    print result

    :param :
    :return:
    :raises:

    """
    if word.isalpha() == False:
        return AssertionError
    elif word[0] in ("a", "e", "i", "o", "u"):
        result = word + "yay"
    else:
        while word[0] not in ("a", "e", "i", "o", "u"):
            word = word[1:] + word[0]
            result = word + "ay"
    print(result)

#pig_latinify()

测试代码

import pytest
import mock
from exercise1 import pig_latinify

word_starting_with_vowel = "apple"
word_starting_with_consonant = "scratch"
word_containing_alphanumeric = "HE90LLO"

def test_word_starting_with_vowel():
    for item in word_starting_with_vowel:
        assert pig_latinify("apple") == "appleyay"


def test_word_starting_with_vowel_2():
    for item in word_starting_with_vowel:
        assert pig_latinify("is") == "isyay"


def test_word_starting_with_consonant():
    for item in word_starting_with_consonant:
        assert pig_latinify("scratch") == "atchscray"


def test_word_containing_alphanumeric():
    for item in word_containing_alphanumeric:
        try:
            pig_latinify("HE90LLO")
        except AssertionError:
            assert True

Tags: 函数intestimportfordefwithassert
3条回答

def pig_latinify(word):中的print(result)替换为return result,或将返回结果添加到打印(结果)行之后。你知道吗

另外,我对pytest包不太熟悉,但您可能需要在类的末尾对每个test_word_.....函数进行调用。你知道吗

请参阅@Dex关于如何确保调用测试函数的答案。此外,代码中还有两个错误会导致测试失败。首先,测试词应该声明为数组元素,否则您将遍历测试词中的每个字符,而不是使用整个词。其次,pig\u拉丁化方法缺少return语句:

def pig_latinify(word):
    """
    takes input from user
    check IF there is a vowel at beginning
        do this
    ELSE
        do this
    print result

    :param :
    :return:
    :raises:

    """
    if word.isalpha() == False:
        return AssertionError
    elif word[0] in ("a", "e", "i", "o", "u"):
        result = word + "yay"
    else:
        while word[0] not in ("a", "e", "i", "o", "u"):
            word = word[1:] + word[0]
            result = word + "ay"
    return result

#pig_latinify()

word_starting_with_vowel = ["apple"]
word_starting_with_consonant = ["scratch"]
word_containing_alphanumeric = ["HE90LLO"]

def test_word_starting_with_vowel():
    for item in word_starting_with_vowel:                 
        assert pig_latinify("apple") == "appleyay"


def test_word_starting_with_vowel_2():
    for item in word_starting_with_vowel:
        assert pig_latinify("is") == "isyay"


def test_word_starting_with_consonant():
    for item in word_starting_with_consonant:
        assert pig_latinify("scratch") == "atchscray"


def test_word_containing_alphanumeric():
    for item in word_containing_alphanumeric:
        try:
            pig_latinify("HE90LLO")
        except AssertionError:
            assert True

if __name__ == "__main__":
    test_word_starting_with_vowel()
    test_word_starting_with_vowel_2()
    test_word_starting_with_consonant()
    test_word_containing_alphanumeric()

由于您没有调用任何函数,因此得到:

Process finished with exit code 0

要查看实际执行的操作,请在末尾调用每个函数:

if __name__ == "__main__":
    test_word_starting_with_vowel()
    test_word_starting_with_vowel_2()
    test_word_starting_with_consonant()
    test_word_containing_alphanumeric()

相关问题 更多 >