vendredi 22 juillet 2016

How to write mock/stud for server python script?

I am new to unit testing in python. Trying to write test case for below scenario. I have read that unittest doesn't have any server login or accessing of database, because unit testing is mainly for code coverage and if we access server or database it would be more of functional testing. I could find some technical terms like, test doubles - mock, stub or fake which can be used for such scenarios. But my knowledge is falling short here. Can someone help me to write mock or stub to test below scenario??? What I am doing is: I am going and login to a server by giving IP address and fetching some settings and match those fetched settings to some input settings which I already have in a input file. One function [host_server_details] is login to server and fetching details and filling to a dictionary and another [_verify_server_settings] is actually matching dictionary and returning true on match. and finally we have main function which simply return output of second function. How can I avoid server login part and do something locally? Is there any way to write mock or stub to make unit test fast and simple which cover 100% code? Any tool to integrate with code which report about code coverage as well? How about Coverage.py? I DO NOT WANT TO MODIFY OR ADD EXTRA CODE TO THIS FILE until unless it is highly unavoidable!!

P.S: One non technical question: Can I tag any developer on this question? I know a developer who can answer this? Thanks in advance.

# !/usr/bin/python
#<All necasarry imports>
class VerifyServerConfig():

def __init__(self, host_details, config):
    self.host = x.x.x.x
    self.ilo_login = username
    self.ilo_password = password
    self.input_settings = config['server_settings']  [ In config file I have       given server_settings: process1: "Disable", process2:"disable" etc]

def host_server_details(self):
    server_details = {}
    server_rest_obj = RestClientBase("") #Pass IP and othe credentials to   login to given server IP and fetch details
    response = server_rest_obj.get("<<Get server details>>")  #Assume things are working fine here
    if response.status == 200:
        server_details = response.dict  #filling server details to dictionary
    else:
        print("coudn't login")
    return server_details

def _verify_server_settings(self):
    server_property_matches = False
    server_properties = self.host_server_details()
    if verify_settings_obj.compare_dictionary(__name__,self.input_settings,server_properties): #compare dictionary here using already written compare function
        server_property_matches = True  # if dictionary matches set variable True
    return server_property_matches

def main(self):
    is_successful = self._verify_server_settings()
    return is_successful                                                                                                                                                                            

Aucun commentaire:

Enregistrer un commentaire