如何为我的函数编写单元测试以获取用户输入

时间:2017-07-12 11:28:37

标签: python unit-testing

"""A program to do FLAMES by accepting two names.
F-Friends, L-Lovers, A-Admirers, M-Married Couple, E-Enemies, S-Secret Lovers

Modified Version : Jan 11 2017
Programmer : Selikem v 0.1.0
"""

def Flames():
    print("\nAre you READY FOR FLAMES????")

    boyName = input("Enter the Guy's Name: ")
    girlName = input("Enter the Lady's Name: ")

    bName = list(boyName.lower())  #Splits name into individual characters and places it in a list
    gName = list(girlName.lower())

    #Below, We remove common letters found in both names.
    for i in bName:
        while i in gName:
            bName.remove(i)
            gName.remove(i)
            break

    for i in gName:
        while i in bName:
            bName.remove(i)
            gName.remove(i)
            break       

    #Below Checks whether there are common letters again and removes them.
    while True:
        for i in bName:
            while i in gName:
                bName.remove(i)
                gName.remove(i)
                break

        for i in gName:
            while i in bName:
                bName.remove(i)
                gName.remove(i)
        break

    flame = bName + gName #Variable to store remaining characters after cancellation of common ones.
    #flame = set(flame)
    #print(len(flame))
    #print(bName)
    #print(gName)
    #print(flame)

    while len(flame) < 50:

        if len(flame) == 1 or len(flame) == 7 or len(flame) == 13 or len(flame) == 19 or len(flame) == 25:
            FLAMES = "FRIENDS"

        elif len(flame) == 2 or len(flame) == 8 or len(flame) == 14 or len(flame) == 20 or len(flame) == 26:
            FLAMES = "LOVERS"

        elif len(flame) == 3 or len(flame) == 9 or len(flame) == 15 or len(flame) == 21 or len(flame) == 27:
            FLAMES = "ADMIRERS"

        elif len(flame) == 4 or len(flame) == 10 or len(flame) == 16 or len(flame) == 22 or len(flame) == 28:
            FLAMES = "MARRIED COUPLE"

        elif len(flame) == 5 or len(flame) == 11 or len(flame) == 17 or len(flame) == 23 or len(flame) == 29:
            FLAMES = "ENEMIES"

        elif len(flame) == 6 or len(flame) == 12 or len(flame) == 18 or len(flame) == 24 or len(flame) == 30:
            FLAMES = "SECRET ADMIRERS"

        print("\n{} and {} are ".format(boyName, girlName) +  FLAMES)   

        break

def Play_Again():
    """Function that permits user to play FLAMES again"""
    while True:
                print("\nDo you want to play again? :")

                answer = input("(Enter y/n to continue): " )
                if answer == 'y' :
                    Flames()

                else:
                    print("\nTHANK YOU FOR PLAYING!")
                    break

Flames()
Play_Again() 

1 个答案:

答案 0 :(得分:0)

将UI代码(询问用户输入,预先输出)与域模型(计算某些内容的代码部分)分开。然后你可以对领域模型进行单元测试。