MOCKING THE MYSTERY IN UNIT TESTING

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

#embedded #softwaretesting #unittesting #mocking #embeddedsoftware #embeddedsystems #iot

要查看或添加评论,请登录

Isfandyar Qureshi的更多文章

  • Embedded Talks Ep 5: Multi-Level Feedback Queue Scheduling

    Embedded Talks Ep 5: Multi-Level Feedback Queue Scheduling

    In Episode 4, we discussed various scheduling algorithms and their trade-offs. However, a fundamental problem with…

  • Embedded Talks Ep 4: Scheduling Algorithms

    Embedded Talks Ep 4: Scheduling Algorithms

    An operating system must provide an scheduler that is tasked with job of handling multiple process/tasks Before we dive…

  • Embedded Talks Ep3 : Context Switching

    Embedded Talks Ep3 : Context Switching

    Context switching refers to the process where cpu halt program currently executing save its context and switch to…

  • Embedded Talks Ep2 : A Process/TASK/THREAD

    Embedded Talks Ep2 : A Process/TASK/THREAD

    Concepts an embedded engineer must know: Process: A process is simply a running program; at any instant of in time…

  • Embedded Talk Ep.1

    Embedded Talk Ep.1

    Concepts an embedded software engineer must know Operating System: An operating system is a piece of software that…

  • WHY IT SUCKS TO BE EMBEDDED ENGINEER!!!!

    WHY IT SUCKS TO BE EMBEDDED ENGINEER!!!!

    This article is a personal experience and observation over my 6-7 years as an electronics/embedded engineer. Your…

    69 条评论
  • Sending Struct on SPI?

    Sending Struct on SPI?

    Another short article for embedded developers like me who are learning We all use structures in our C / C++ programing…

    3 条评论
  • Common Issue with SPI

    Common Issue with SPI

    Spi is very common protocol and is used very often it has its perk of multiple sensors on common bus etc Hardware used…

    1 条评论
  • IS YOUR SPI GITCHY ?????

    IS YOUR SPI GITCHY ?????

    #embeddedsystems #spi #embeddedengineer Have you even face a scenario where your spi run sometimes very well and…

    8 条评论
  • RTOS DEBUGGING IN REAL TIME USING OZONE AND SYSTEM VIEWER

    RTOS DEBUGGING IN REAL TIME USING OZONE AND SYSTEM VIEWER

    Finally got my stlink-V2 on nucleo board to debug rtos providing me to see at what instance which task is running at…

社区洞察

其他会员也浏览了