2017-07-25 4 views
-1

J'ai des difficultés à écrire des cas de test pour la classe ZabbixAPILayer mentionnée ci-dessous. Je ne sais pas comment je peux me moquer du 'zabbix_conn_obj' là-bas. Toute aide serait appréciée. Merci!Comment simuler un objet pour des cas de test en python

fichier: ExternalAPI/apilayer.py

from zabbix.api import ZabbixAPI 

import json 
import time 
class ZabbixAPILayer(object): 

    def uptime(self,arg,zabbix_conn_obj): 
     try: 
      getUpdateItem = zabbix_conn_obj.do_request("item.get", {"host":arg}) 

      lastclock=getUpdateItem['result'][37].get('lastclock') 
      lastclock=int(lastclock) 

      curclock=int(time.time()) 

      check_val=curclock-lastclock 
      limit=60*1000 
      if check_val<limit: 
       lastval=getUpdateItem['result'][37].get('lastvalue') 
       return time.strftime("%H:%M:%S", time.gmtime(float(getUpdateItem['result'][37].get('lastvalue')))) 

      else: 
       return "-" 

     except: 
      return "NOT AVAILABLE" 
    ..... 

class APILayer(ZabbixAPILayer): 
    def __init__(self): 
     self.zabbix_conn_obj=ZabbixAPI(url=settings.ZABBIX_URL, user=settings.ZABBIX_USER, password=settings.ZABBIX_PWD) 

    def uptime(self,arg): 
     return super(APILayer,self).uptime(arg,self.zabbix_conn_obj) 
..... 

fichier: base/admin.py

...... 
from ..externalapis.apilayer import APILayer 
...... 
gen_obj= APILayer() 

gen_obj.uptime() 
...... 
+0

Commencer par 'zabbix_conn_obj = Mock()'. – Goyo

+0

Un test est toujours spécifique au test, veuillez donc fournir le test que vous voulez simuler. –

+0

Génial. Merci! compris et a obtenu ce travail! – user3465593

Répondre

0

Merci pour vos commentaires. Ça a marché! Voici comment je le fais

import mock 

... 

def test_uptime(self): 
    zabbix_conn_obj = mock.Mock() 
    json_blob = {} # This is the json blob i'm using as return value from do request 
    zabbix_conn_obj.do_request = mock.Mock(return_value=json_blob) 
    obj = ZabbixAPILayer() 
    value = obj.uptime("TestHOST",zabbix_conn_obj) 
    desired_result = '' #whatever my desired value is 
    self.assertEqual(value, desired_result)