2012-11-20 86 views
0

我正在使用postgresql,我用MagicMock來測試,但我不確定我是否已經瞭解了模擬的概念。這是我的示例代碼(我有一個數據庫名=測試,表= py_test和用戶=西蒙娜):概念:嘲諷數據庫python

import psycopg2 
    import sys 
    from mock import Mock, patch 
    import unittest 
    from mock import MagicMock 
    from collections import Counter 
    import doctest 


    class db(object): 
     def __init__(self,database, user): 
      self.con = None 
      self.database = database 
      self.user = user 

     def test_connection(self): 
      """Connection DB""" 
      try: 
       self.con = psycopg2.connect(database=self.database, user=self.user) 
       return True 
      except psycopg2.DatabaseError, e: 
       print 'Error %s' % e  
       return False 

     def test_empty_table(self,table): 
      """empty table?""" 
      try: 
       cur = self.con.cursor() 
       cur.execute('SELECT * from ' + table) 
       ver = cur.fetchone() 
       return ver 
      except psycopg2.DatabaseError, e: 
       print 'Error %s' % e  

     def test_data_type(self, table, column): 
      """data type""" 
      try: 
       cur = self.con.cursor() 
       cur.execute("SELECT data_type from information_schema.columns where table_name = '"+ table + "' and column_name= '"+column+"'") 
       ver = cur.fetchone() 
       return ver 
      except psycopg2.DatabaseError, e: 
       print 'Error %s' % e  

     def __del__(self): 
      if self.con: 
       self.con.close() 

    class test_db(unittest.TestCase): 

     def testing(self): 
      tdb = db('test','simone') 
      self.assertTrue(tdb.test_connection(), 1) 
      self.assertTrue(tdb.test_empty_table('py_test'), 1) 
      self.assertTrue(tdb.test_data_type('py_test','id'), int) 

    class test_mock(object): 
     def __init__(self, db): 
      self.db = db 
     def execute(self, nomedb, user, table, field): 
      self.db(nomedb, user) 
      self.db.test_connection() 
      self.db.test_empty_table(table) 
      self.db.test_data_type(table, field) 


    if __name__ == "__main__": 
     c = MagicMock() 
     d = test_mock(c) 
     d.execute('test','simone','py_test','id') 
     method_count = Counter([str(method) for method in c.method_calls]) 
     print c.method_calls 
     print method_count 
     print c.mock_calls 
+6

那麼,什麼是問題?你的代碼不工作或什麼? – GSP

回答

2

也許我給你用嘲諷包的Mockito的其他一些例子:

import sphinxsearch 
import unittest 
from mockito import mock, when, unstub, verify 

class SearchManagerTest(unittest.TestCase): 

    def setUp(self): 
     self.sphinx_client = mock() 
     when(sphinxsearch).SphinxClient().thenReturn(self.sphinx_client) 

    def tearDown(self): 
     unstub() 

    def test_search_manager(self): 
     # given 
     value = {'id': 142564} 
     expected_result = 'some value returned from SphinxSearch' 

     # when 
     search_manager = SearchManager() 
     result = search_manager.get(value) 

     # then 
     verify(self.sphinx_client).SetServer('127.0.0.1', 9312) 
     verify(self.sphinx_client).SetMatchMode(sphinxsearch.SPH_MATCH_ALL) 
     verify(self.sphinx_client).SetRankingMode(sphinxsearch.SPH_RANK_WORDCOUNT) 
     self.assertEqual(result, expected_result) 

主要的概念是替換一些模塊(模擬),測試一些其他地方(它有它自己的unittest模塊)並記錄一些行爲。

更換模塊你用模擬物使用:

self.sphinx_client = mock() 

,然後在這個模擬記錄,如果你調用特定的方法,這個方法會返回一些數據 - 如果你需要檢查簡單的值,例如字符串或嘲笑數據行爲:

when(sphinxsearch).SphinxClient().thenReturn(self.sphinx_client) 

在這種情況下,你告訴大家,如果導入sphinxsearch模塊,並調用SphinxClient()上它,你就會得到嘲笑的對象。

然後主測試進來。你調用方法或對象來測試(SearchManager在這裏)。它的機身與一些給出值測試:

self.search_manager = SearchManager() 

部分驗證是否在那裏做了一些動作:

verify(self.sphinx_client).SetServer('127.0.0.1', 9312) 
verify(self.sphinx_client).SetMatchMode(sphinxsearch.SPH_MATCH_ALL) 
verify(self.sphinx_client).SetRankingMode(sphinxsearch.SPH_RANK_WORDCOUNT) 

這裏 - 如果SetServer叫上self.sphinx_client參數爲'127.0.0.1'和。另外兩條線就像上面那樣自我解釋。

在這裏,我們做正常的檢查:

self.assertEqual(result, expected_result)