Difference between revisions of "Mocking Frameworks"

From Embedded Lab Vienna for IoT & Security
Jump to navigation Jump to search
Line 5: Line 5:
== Requirements ==
== Requirements ==


* Operating system:
* Visual Studio Code
* Packages: unittest.mock (Python), org.mockito.Mockito(Java) and Moq(.NET)
* Packages: unittest.mock (Python), org.mockito.Mockito(Java) and Moq(.NET)



Revision as of 01:09, 24 January 2023

Summary

This document gives basic insights about Mocking and Mocking Frameworks with their usages, advantages and disadvantages. Also, some code examples showing mock testing demo using Frameworks like Mock(Python), Mockito(Java) and Moq(.NET) are also given.

Requirements

  • Visual Studio Code
  • Packages: unittest.mock (Python), org.mockito.Mockito(Java) and Moq(.NET)

Description

Mocking

Mocking is a process used in unit testing when unit being tested has external dependencies.It creates mock objects (also known as replacement or dummy objects) that can be used in the simulation of real objects.The main purpose of this process is isolation of code being tested rather than concentrating on the behaviour or state of external dependencies.Mocking is normally required when components under test has dependencies which has no been implemented yet or if implementation is in progress (eg:REST APIs) or when components update system state(DB calls).

Mocking makes use of three types of replacement objects: fakes, stubs and mocks. The fakes are used when you want to test the behavior of a class that has no dependencies. The stubs are used to test the behavior of a class that has dependencies.They will return results based on specific set of inputs and won't respond to something outside of what is programmed for the test. The mocks are advanced version of stubs which can also additionally modify behaviors like how many times method should be called, with what data and in which order.

Mocking Frameworks

Mocking Frameworks are software libraries used to generate replacement objects like Stubs and Mocks i.e dummy implementation does not have to be written in addition to real implementation,and they also compliments Unit Testing Frameworks by isolating dependencies. But remember they are not replacement for unit testing frameworks and they should not be used to test the actual behavior of the software. Rather they are used to simulate or mock dependencies, especially to simulate external APIs and Databases in tests.

Advantages

  • Tests can be isolated and thereby improving the test quality. It helps developers to write focused and concise unit tests.
  • It can also help to run tests faster and generate test data.

Disadvantages

  • Can lead to complex and difficult-to-understand tests if not used carefully.
  • Can also lead to writing tests that do not adequately reflect the actual behavior of the software.

Frameworks in different programming languages

  • Mockito (Java)
  • Moq (.NET)
  • Mock (Python)
  • EasyMock (Java)
  • jMock (Java)
  • Sinon.js(JS)
  • pymox (Python)
  • rr (Ruby)

Examples

Mock.png Mockito.png Moq.png

Mock Demo in Python

import unittest
from unittest.mock import Mock, patch
import requests

def get_user_data(user_id):
    # Simuliere einen API-Aufruf, der Benutzerdaten abruft
    response = requests.get(f"https://gorest.co.in/public/v2/users/{user_id}")
    if response.status_code == 200:
        return response.json()
    return None

def DoesTanyaExist(user):
    if "Tanya" in user["name"]:
        return True
    return False

class TestAPI(unittest.TestCase):
    @patch("requests.get")
    def test_get_user_data(self, mock_get):
        mock_result = {"id": 1, "name": "Tanya", 'email': 'test@user.com', 'gender': 'male', 'status': 'active'}
        # Erstelle einen Mock für die response-Variable
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = mock_result

        # Setze den Rückgabewert des Mock-Objekts für den get-Aufruf
        mock_get.return_value = mock_response

        # Rufe die get_user_data-Funktion auf
        user_data = get_user_data(4171)

        print(user_data)
        # Stelle sicher, dass der Rückgabewert des Mock-Objekts zurückgegeben wurde
        self.assertEqual(user_data, mock_result)
        self.assertTrue(DoesTanyaExist(user_data),"Tanja does not exist")
        print("Tanja does exist")

if __name__ == '__main__':
    # unittest.main()
    
    user= get_user_data(4171)
    print(f"user: {user}")
    print(f"Tanja does {'' if DoesTanyaExist(user) else 'not '}exist")

References