2012-10-05 21 views
0

我一直在Python编程一个月,但我从来没有写过测试用例。我想写2个测试集到我下面的程序的测试决定:如何编写测试集来测试Python程序中的决定?

#!/usr/bin/python3 

def books(): 
    a = input("Enter number of books:") 
    inp = int(a) 
    if inp==0: 
     print("You earned 0 points") 
    elif inp==1: 
     print("You earned 5 points") 
    elif inp==2: 
     print("You earned 15 points") 
    elif inp==3: 
     print("You earned 30 points") 
    elif inp==4: 
     print("You earned 60 points") 
    else: 
     print("No Negatives") 

books() 

我怎样写2测试集来测试这一方案的决定?谢谢!

+0

你为什么要在函数中的参数'books' – avasal

+0

错误!编辑我的问题。 –

回答

0

编辑对这个问题的第二个版本:

def books(): 

    points = [0,5,15,30,60]; # list of the points 
    inp = 0; 

    while inp < 0 or inp >= len(points): 
     a = input("Enter number of books:") 
     inp = int(a) 

    print("You earned",points[inp],"points") 

books() 

您将能够避免列表中,如果有图书的数量和点的数量有直接关系,但我不知道你的算法。

如果inp的值是字符串,那么您可以使用字典而不是列表。

+0

谢谢你的回答!但似乎我发布了错误的代码。我编辑了我的问题。你可以看看它,并编辑你的答案。谢谢! –

+0

答复已更新。 – cdarke

0

这听起来像你正在寻找一个测试的例子。我会告诉你,这是在Python 2.7中完成的,但我相信它也可以在3中工作(我所做的只是将print语句更改为一个函数 - 不知道其他函数是否工作:))。我对你的代码进行了一些编辑(正如cdarke提到的那样,有更简单的方法来设置点数,但我会保持接近你的原始代码,以便你可以看到你可以测试它的步骤)。这里是你的代码,有一些修改(注释):

def Books(num_books): 
    # We let books take a parameter so that we can test it with different 
    # input values. 
    try: 
     # Convert to int and then choose your response 
     num_books = int(num_books) 
     if num_books < 0: 
      response = "No Negatives" 
     elif num_books == 0: 
      response = "You earned 0 points" 
     elif num_books == 1: 
      response = "You earned 5 points" 
     elif num_books == 2: 
      response = "You earned 15 points" 
     elif num_books == 3: 
      response = "You earned 30 points" 
     elif num_books == 4: 
      response = "You earned 60 points" 
     else: 
      response = "That's a lot of books" 

    except ValueError: 
     # This could be handled in a better way, but this lets you handle 
     # the case when a user enters a non-number (and lets you test the 
     # exception) 
     raise ValueError("Please enter a number.") 

    return response 


def main(): 
    # Here we wrap the main code in the main function to prevent it from 
    # executing when it is imported (which is what happens in the test case) 

    # Get the input from the user here - this way you can bypass entering a 
    # number in your tests. 
    a = input("Enter number of books: ") 

    # Print the result 
    print(Books(a)) 


if __name__ == '__main__': 
    main() 

然后测试,你可以简单地运行作为python my_program_test.py

import my_program 
import unittest 

class MyProgramTest(unittest.TestCase): 

    def testBooks(self): 
     # The results you know you want 
     correct_results = { 
      0: "You earned 0 points", 
      1: "You earned 5 points", 
      2: "You earned 15 points", 
      3: "You earned 30 points", 
      4: "You earned 60 points", 
      5: "That's a lot of books" 
      } 

     # Now iterate through the dict, verifying that you get what you expect 
     for num, value in correct_results.iteritems(): 
      self.assertEqual(my_program.Books(num), value) 


    def testNegatives(self): 
     # Test a negative number to ensure you get the right response 
     num = -3 
     self.assertEqual(my_program.Books(num), "No Negatives") 


    def testNumbersOnly(self): 
     # This is kind of forced in there, but this tests to make sure that 
     # the proper exception is raised when a non-number is entered 
     non_number = "Not a number" 
     self.assertRaises(ValueError, my_program.Books, non_number) 

if __name__ == '__main__': 
    unittest.main() 
+0

@新文件夹:这个答案适用于Python 2,从您的'print'语句来判断您可能正在使用Python 3.请注意'raw_input'是'input'的旧Python。 (虽然我注意到@RocketDonkey使用了'print'的函数版本,它通常只在'from __future__ import print_function'中用于旧Python中) – cdarke

+0

@cdarke是的,我从Python 2.7开始编写它,并试图调整代码,所以它会为他工作3 - 有没有什么在我的3不会工作(我会修复raw_input,好点)?我想我应该开始在某个时候使用它:) – RocketDonkey

+0

Nah,只是'输入'SFAIK。抱歉挑剔。异常处理有一些变化,'unittest'中有一些新的特性,但我认为现有的应该在你使用它们的时候工作。 – cdarke

相关问题