0

私はPythonで1か月プログラミングを行っていますが、テストケースを作成したことはありません。次のプログラムのテスト決定に2つのテストセットを書き込もうとしています。

#!/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つのテストセットを作成するにはどうすればよいですか?ありがとう!

4

2 に答える 2

0

テスト例を探しているようですね。これはPython2.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()
于 2012-10-05T05:06:30.027 に答える
0

質問の2番目のバージョンの編集:

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が文字列の場合、リストの代わりに辞書を使用できます。

于 2012-10-05T04:07:02.430 に答える