Python测试用例根本不运行,什么都不返回

时间:2015-11-03 14:12:36

标签: python unit-testing

我正在编写一个可以将任何输入转换为猪拉丁语的函数。假设输入将被传递到函数中,因此此时需要用户输入。我试图弄清楚我的测试用例,但我只收到这个输入:

处理完成,退出代码为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

6 个答案:

答案 0 :(得分:1)

由于您没有调用任何功能,因此获得以下功能是正常的:

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

答案 1 :(得分:0)

请参阅@ Dex关于如何确保调用测试函数的答案。此外,代码中有两个错误会导致测试失败。首先,您的测试单词应声明为数组元素,否则您将迭代测试单词中的每个字符,而不是使用整个单词。其次,你的pig_latinify方法缺少一个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()

答案 2 :(得分:0)

print(result)中的def pig_latinify(word):替换为return result,或在打印(结果)行后添加返回结果。

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

答案 3 :(得分:0)

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

Pytest将使用test__test作为测试函数识别所有函数名称,并自动运行,您无需明确调用它们!

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

答案 4 :(得分:0)

也许这更容易?

PictureStore pictureStore = new PictureStore(hdocument);
// bla bla ...
for (int cr=0; cr < par.numCharacterRuns(); cr++) {
    CharacterRun characterRun = par.getCharacterRun(cr);
    Field field = hdocument.getFields().getFieldByStartOffset(FieldsDocumentPart.MAIN, characterRun.getStartOffset());
    if (field != null && field.getType() == 0x3A) { // 0x3A is type "EMBED"   
        Picture pic = pictureStore.getPicture(field.secondSubrange(characterRun));
    }
}

答案 5 :(得分:-1)

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