MOCKING THE MYSTERY IN UNIT TESTING
Greeting everyone are you a software engineer making apps or are you an embedded iot developer.
This article is for everyone who is tired of having no idea how and why is code is working thats write its working but only GOD knows why.
I believe the reader has some concept of unit testing in software development or test driven development technique
Now image a scenario you are developing an iot product its a device which connects to a url and you in return get data from url.
but this week weather is bad and storm is coming and internet is down and you cannot connect to your url without internet so you might as well drop its testing and just push the code to deployment and hope for best
those are the old days let me introduce you to mocking a concept
imagine you can call the same function that connects to url and get you your relevant data but this time you are mocking it and telling your code to get the return value you want isnt this great this way you can replace 3rd party dependency and also predict and test scenarios of your code behaviour
now let me show you with codes
? ? def test_check_api_connection(self)
? ? ? ? with patch("requests") as mocked_get:
? ? ? ? ? ? mocked_get.get.return_value.status_code = 200
? ? ? ? ? ? actual_result = check_api_connection("192.168.0.0:1080")
? ? ? ? ? ? expected_result = "Successful Api Get Request"
? ? ? ? ? ? self.assertEqual(actual_result, expected_result):
领英推荐
def check_api_connection(ip_address)
? ? response = None
? ? try:
? ? ? ? # format api string
? ? ? ? api_call = "https://{}/api/all/all/now".format(ip_address)
? ? ? ? api_get_request = requests.get(api_call, timeout=12)
? ? ? ? # pass status response
? ? ? ? if api_get_request.status_code == 200:
? ? ? ? ? ? response = "Successful Api Get Request"
? ? except requests.exceptions.RequestException as ex:
? ? ? ? response = "Api Not Reachable"
? ? return response:
here we have two snippets of code in python first is the test code in test code we can see that we have an assertion which compare actual_result with expected_result. The actual_result is depending upon the return value of function check_api_connection but remember we cannot check internet connection because storm is upon us and weather is bad and internet fiber is broken repair will take 3-4 business days so what can be is there a way we can replicate the dependencies i.e (requests.get(api_call, timeout=12)inside check_api_connection method?
p.s above example uses python unittest module
yes we can use mocking to handle dependencies according to our need for test the code
with patch("requests") as mocked_get:
??????mocked_get.get.return_value.status_code = 200
with above line we are mocking the return value of request.get in check_api method to return us status code as 200 this makes our
actual_result ="Successful Api Get Request"
and now we can get our test to work and pass and test out the code other functionalities like what should happen if connection is successfull we should get data right?
I believe mocking is way to move forward with tests until you have resources available to get rid of mocking and test it fully with dependencies in code
If you want to add anything else to this article feel free