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

Python测试用例根本不运行,不返回任何内容,python,unit-testing,Python,Unit Testing,我正在写一个函数,可以把任何输入转换成拉丁语。假设输入将被传递到函数中,因此此时需要用户输入。我试图找出我的测试用例,但我只收到以下输入: 进程已完成,退出代码为0 有人能解释这是为什么吗?我在下面概述了我的函数和测试函数代码: 功能 def pig_latinify(word): """ takes input from user check IF there is a vowel at beginning do this ELSE

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

进程已完成,退出代码为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

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

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

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

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

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

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

可能是Python测试用例的一部分根本没有运行。

def pig\u拉丁化(word):
中替换
print(result)
或在print(result)行后添加返回结果


另外,我对
pytest
软件包不太熟悉,但您可能需要在课程结束时调用每个
test\u word...
函数。

从您的代码中,我看到您安装了pytest。因此,运行测试的更好方法是使用
py.test testscript.py
运行脚本

Pytest将使用
test
\u test
作为测试函数标识所有函数名,并自动运行,您无需显式调用它们

您可以在模块中找到更多详细信息。

也许这更容易些

# Pig Latinify

vowels = ['a', 'e', 'i', 'o', 'u', 'y'] ## added 'y', .e.g hymn --> ymnhay
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z']


def pig_latinify():

    user_input = raw_input("Enter a word to be translated: ").lower()


    # If the first character in input is a vowel add 'yay' to input and print.
    if user_input[0] in vowels[0:]:
        print "\n%s begins with a vowel." %user_input
        pig_output = user_input + "yay"
        print "\n\t%s becomes: %s \n" %(user_input,pig_output)

    else:

        print "\n%s Doesn't begin with a vowel." %user_input
        for i,l in enumerate(user_input):
            if l in vowels: 
                x = user_input[:i] ## first part of word, to be moved
                pig_output = user_input[i:] + x + "ay"
                print "\n\t%s becomes: %s \n" %(user_input,pig_output)
                break ## exit the for loop 
    return

pig_latinify()

好吧,你希望打印什么?你从不调用你的测试方法。你的测试方法会失败,因为你的代码中有一些错误。好吧,你已经定义了一堆函数,但是你实际上在哪里调用它们(除了从其他函数)?@TomKarzes我添加了包含字母数字的测试单词以辅音开头测试单词以元音开头测试单词以元音开头测试单词以元音开头我仍然收到了同样的结果Jaco和Dex非常感谢!我是一个初学者,所以这可能看起来像一个愚蠢的问题,但什么是:if name==“main”:reference?@MTL\u TOR,看看这篇帖子:@MTL\u TOR
if\uu name\uuuu=='\uu main\uuuu'
意味着只有在直接执行模块而不是导入模块时才运行以下代码。嗯,我仍然得到相同的输出。进程以退出代码结束。我在函数代码的第3行中概述断言错误的方式是否有问题?上面的代码应该运行,您的所有测试都将通过。你应该通过故意让你的一个测试失败来验证这一点,例如:
assert pig\u-latinify(“苹果”)=“banana”
Wow这是一个多么有趣的答案!