2

I am writing a function that can turn any input into pig latin. It is assumed that the input will be passed into the function so user input is necessary at this time. I am trying to figure out my test cases for this but am only receiving this input:

Process finished with exit code 0

Can anyone explain why that is? I've outlined my code for both the function and test functions below:

Function

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

Test Code

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
chepner
  • 497,756
  • 71
  • 530
  • 681
MTL_TOR
  • 23
  • 2
  • Well, what do you expect to be printed ? –  Nov 03 '15 at 14:16
  • You never call your test methods. Your test methods are going to fail, because there are some bugs in your code. – Alex Nov 03 '15 at 14:18
  • Well, you've defined a bunch of functions, but where do you actually call them (except from other functions)? – Tom Karzes Nov 03 '15 at 14:18
  • @TomKarzes I added test_word_containing_alphanumeric() test_word_starting_with_consonant() test_word_starting_with_vowel() test_word_starting_with_vowel_2() and I still received the same output – MTL_TOR Nov 03 '15 at 14:28

6 Answers6

1

Due to the fact that you're not calling any function, it's normal to get:

Process finished with exit code 0

To see what you actually did, call each function at the end:

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

See @Dex's answer on how to make sure your test functions get called. In addition there are a two bugs in your code that will result in test failures. Firstly, your test words should be declared as an array element otherwise you are iterating over each character in your test words rather than using the entire word. Secondly, your pig_latinify method is missing a return statement:

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()
Alex
  • 21,273
  • 10
  • 61
  • 73
  • Jaco and Dex thank you so much! I'm a beginner so this may seem like a silly question but what does: if __name__ == "__main__": reference? – MTL_TOR Nov 03 '15 at 14:36
  • @MTL_TOR, have a look a this post: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Alex Nov 03 '15 at 14:38
  • @MTL_TOR `if __name__ == '__main__'` means to only run the following code if the module is being **directly executed**, not imported. – pppery Nov 03 '15 at 14:39
  • Hm, I'm still getting the same output. Process finished with exit code. Is there something wrong with the way I've outlined my assertion error in line 3 of my function code? – MTL_TOR Nov 03 '15 at 14:49
  • The code above should run and all your tests will pass. You should verify this by making one of your test fail on purpose, for example: `assert pig_latinify("apple") == "banana"` – Alex Nov 03 '15 at 14:53
0

Replace print(result) in def pig_latinify(word): with return result or add the return result after the print(result) line.

Also, I'm not too familiar with the pytest packages but you likely will need to put calls to each of your test_word_..... functions at the end of the class.

Woodsy
  • 3,177
  • 2
  • 26
  • 50
0

From your code I see you have pytest installed. So, better way to run test is by running the script with py.test testscript.py.

Pytest will identify all function names with test_ or _test as test functions and run automatically and you don't need to call them explicitly!

You can find more details in module documentation.

WoodChopper
  • 4,265
  • 6
  • 31
  • 55
0

Maybe this is easier?

# 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()
travelingbones
  • 7,919
  • 6
  • 36
  • 43
-1

it may be the part of Python test case does not run at all.

rahul
  • 1