2016-04-21 156 views
0

我是python的新手。我想測試一個採用原始輸入並在輸入上調用另一個函數的函數。我知道這可以使用模擬測試,但我不知道如何。Python單元測試:如何測試調用其他函數的函數?

我的功能是這樣的:

def show_main_menu(self): 
    """shows main menu to user""" 
    print '1:create account \t 2:Log in \t any other to exit \n' 
    while True: 
     try: 
      option = int(raw_input('Enter your option: \t')) 
     except ValueError: 
      print 'please enter valid input \n' 
      continue 
     else: 
      break 
    if option == 1: 
     self.add_account() 
     print '\n ***successfully created account***\n' 
     self.menu_post_transaction() 
    elif option == 2: 
     self.login() 
    else: 
     print 'Thank you for using our services' 
     exit() 

如何使用unittest我測試這個功能呢?

+0

的可能的複製[我怎麼能模擬輸入標準輸入的PyUnit?](http://stackoverflow.com/questions/6271947/how-can-i-simulate-input-to-stdin-for-pyunit) – user590028

回答

1

主菜單模塊:

import unittest 
from your_tests_directory import add_account_test, login_test 

def menu(): 
    loader = unittest.TestLoader() 
    suite = unittest.TestSuite() 

    option = int(raw_input('Enter your option: \t')) 
    if option == 1: 
     module = add_account_test 
    else: 
     module = login_test 

    suite.addTest(loader.loadTestsFromModule(module)) 
    unittest.TextTestRunner(verbosity=2).run(suite) 

if __name__ == '__main__': 
    menu() 

add_account_test模塊:

import unittest 
from other_module import add_account, menu_post_transaction 

class AddAccountTest(unittest.TestCase): 
    def test_account(self): 
     add_account() 
     print '\n ***successfully created account***\n' 
     menu_post_transaction() 

login_test模塊:

import unittest 
from other_module import login 

class LoginTest(unittest.TestCase): 
    def test_login(self): 
     login() 
在 'other_module' 你會把所有的實際功能

...

0

它c烏爾德是這樣的:

from unittest.mock import patch 
from unittest import TestCase 

@patch('yourmodule.show_main_menu', return_value='1') 
def test_answer_one(self, input): 
    self.assertEqual(show_main_menu(), '\n ***successfully created account***\n') 

@patch('yourmodule.show_main_menu', return_value='5') 
def test_answer_five(self, input): 
    self.assertEqual(show_main_menu(), 'Thank you for using our services') 
1

我強烈建議你重構你的代碼位,使之可測試的,僅通過提取raw_input相關代碼的功能,你不會需要模擬什麼,_get_raw_input()將是可重複使用。

在測試你只需要通過自己的函數中的參數get_option簡單地返回一個任意選擇。

PS:show_main_menu功能,可以在一些改名像launch_action

#!/usr/bin/env python2 
# coding=utf-8 

def _get_raw_input(prompt): 
    while True: 
     try: 
      return int(raw_input(prompt)) 
     except ValueError: 
      print 'please enter valid input \n' 
      continue 
     else: 
      break 

class Foo: 
    def menu_post_transaction(self): print 'menu_post_transaction' 
    def add_account(self): print "### 1. add_account" 
    def login(self): print "### 2. login" 

    def show_main_menu(self, get_option=_get_raw_input): 
      """shows main menu to user""" 
      print '1:create account \t 2:Log in \t any other to exit \n' 
      option = get_option('Enter your option: \t') 
      if option == 1: 
       self.add_account() 
       print '\n ***successfully created account***\n' 
       self.menu_post_transaction() 
      elif option == 2: 
       self.login() 
      else: 
       print 'Thank you for using our services' 

if __name__ == "__main__": 

    Foo().show_main_menu(lambda _: 1) 
    Foo().show_main_menu(lambda _: 2) 
    Foo().show_main_menu(lambda _: 3) 

這是一個一般的好習慣獨立於語言。

相關問題