Difference between revisions of "Mocking Frameworks"
(17 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
== Requirements == | == Requirements == | ||
* | * Visual Studio Code (or any other IDE) | ||
* Packages: unittest.mock (Python) | * Packages (depending on the Programming language and the used Framework). <br>For example: | ||
**unittest.mock (Python) | |||
** org.mockito.Mockito(Java) | |||
** Moq(.NET) | |||
== Description == | == Description == | ||
Line 12: | Line 15: | ||
=== Mocking === | === 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 | 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 not 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 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. | ||
Line 18: | Line 21: | ||
=== Mocking Frameworks === | === 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 a 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. | |||
* Tests can also been done in case the external Dependency is not reachable or has other kind of problems. | |||
== | ==== 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 ==== | |||
<!--[[File:Mock.png]] | |||
[[File:Mockito.png]] | |||
[[File:Moq.png]] --> | |||
{| class="wikitable" width="100%" style="vertical-align:top;" | |||
|- | |||
! .NET (Moq) | |||
! Java (Mockito) | |||
! Python (Mock) | |||
|- | |||
| | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using Moq; | |||
public interface IPerson | |||
{ | |||
string Greet(); | |||
} | |||
public class Person : IPerson | |||
{ | |||
private string name; | |||
public Person(string lname){ | |||
name=lname; | |||
} | |||
public string Greet() | |||
{ | |||
return "Hello, my name is "+name; | |||
} | |||
} | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
// Wir erstellen eine Instanz von IPerson | |||
IPerson person = new Person("John"); | |||
// Wir erstellen eine Mock-Instanz von IPerson | |||
var mockPerson = new Mock<IPerson>(); | |||
// Wir konfigurieren die Methode Greet() der Mock-Instanz, | |||
//um immer "Mock greeting" zurückzugeben | |||
mockPerson.Setup(x => x.Greet()).Returns("Mock greeting"); | |||
// Wir vergleichen die Ausgabe von Greet() von beiden | |||
Console.WriteLine(person.Greet()); | |||
// Ausgabe: "Hello, my name is John" | |||
Console.WriteLine(mockPerson.Object.Greet()); | |||
// Ausgabe: "Mock greeting" | |||
} | |||
} | |||
</syntaxhighlight> | |||
| | |||
<syntaxhighlight lang="java"> | |||
import org.mockito.Mockito; | |||
public class Person { | |||
private String name; | |||
public Person(String name) { | |||
this.name = name; | |||
} | |||
public String greet() { | |||
return "Hello, my name is " + this.name; | |||
} | |||
} | |||
// Wir erstellen eine Instanz von Person | |||
Person person = new Person("John"); | |||
// Wir erstellen eine Mock-Instanz von Person | |||
Person mockPerson = Mockito.mock(Person.class); | |||
// Wir konfigurieren die Methode greet() der Mock-Instanz, | |||
// um immer "Mock greeting" zurückzugeben | |||
Mockito.when(mockPerson.greet()).thenReturn("Mock greeting"); | |||
// Wir vergleichen die Ausgabe von greet() von beiden | |||
System.out.println(person.greet()); | |||
// Ausgabe: "Hello, my name is John" | |||
System.out.println(mockPerson.greet()); | |||
// Ausgabe: "Mock greeting" | |||
</syntaxhighlight> | |||
| | |||
<syntaxhighlight lang="python"> | |||
from unittest.mock import Mock | |||
class Person: | |||
def __init__(self, name): | |||
self.name = name | |||
def greet(self): | |||
return "Hello, my name is " + self.name | |||
# Wir erstellen eine Instanz von Person | |||
person = Person("John") | |||
# Wir erstellen eine Mock-Instanz von Person | |||
mock_person = Mock(spec=Person) | |||
# Wir konfigurieren die Methode greet() der Mock-Instanz, | |||
# um immer "Mock greeting" zurückzugeben | |||
mock_person.greet.return_value = "Mock greeting" | |||
# Wir vergleichen die Ausgabe von greet() | |||
print(person.greet()) | |||
# Ausgabe: "Hello, my name is John" | |||
print(mock_person.greet()) | |||
# Ausgabe: "Mock greeting" | |||
</syntaxhighlight> | |||
|} | |||
==== Mock Demo in Python ==== | |||
<syntaxhighlight lang="python"> | |||
import unittest | |||
from unittest.mock import Mock, patch | |||
import requests | |||
import json | |||
def get_users(): | |||
# API-Aufruf einer Beispiel API, der Benutzerdaten abruft | |||
response = requests.get(f"https://gorest.co.in/public/v2/users") | |||
if response.status_code == 200: | |||
return response.json() | |||
return None | |||
def DoesGowdaExist(users:list[dict]): | |||
# Testet ob ein Benutzer namens "Gowda" existiert | |||
for user in users: | |||
if "Gowda" in user["name"]: | |||
return True | |||
return False | |||
class TestAPI(unittest.TestCase): | |||
@patch("requests.get") | |||
def test_get_user_data(self, mock_get): | |||
# Unittest bei dem statt der Beipiel-API ein Mock verwendet wird | |||
mock_result = [{"id": 1, "name": "Gowda", | |||
'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 | |||
users = get_users() | |||
print(f"users: {json.dumps(users, indent=4)}") | |||
# Stelle sicher, dass der Rückgabewert des Mock-Objekts zurückgegeben wurde | |||
self.assertEqual(users, mock_result) | |||
self.assertTrue(DoesGowdaExist(users),"Gowda does not exist") | |||
print("Gowda does exist") | |||
def main(mock:bool): | |||
# Wenn mock=False ist, wird die API aufgerufen | |||
# Wenn mock=False ist wird der Unitest ausgeführt der den Aufruf der API mit einem Mock-Objekt überschreibt | |||
if mock: | |||
unittest.main() | |||
else: | |||
users= get_users() | |||
print(f"users: {json.dumps(users, indent=4)} \n\n") | |||
print(f"Gowda does {'' if DoesGowdaExist(users) else 'not '}exist") | |||
if __name__ == '__main__': | |||
main(mock=True) | |||
</syntaxhighlight> | |||
== References == | == References == |
Latest revision as of 13:49, 28 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 (or any other IDE)
- Packages (depending on the Programming language and the used Framework).
For example:- unittest.mock (Python)
- org.mockito.Mockito(Java)
- 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 not 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 a 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.
- Tests can also been done in case the external Dependency is not reachable or has other kind of problems.
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
.NET (Moq) | Java (Mockito) | Python (Mock) |
---|---|---|
using System;
using Moq;
public interface IPerson
{
string Greet();
}
public class Person : IPerson
{
private string name;
public Person(string lname){
name=lname;
}
public string Greet()
{
return "Hello, my name is "+name;
}
}
class Program
{
static void Main(string[] args)
{
// Wir erstellen eine Instanz von IPerson
IPerson person = new Person("John");
// Wir erstellen eine Mock-Instanz von IPerson
var mockPerson = new Mock<IPerson>();
// Wir konfigurieren die Methode Greet() der Mock-Instanz,
//um immer "Mock greeting" zurückzugeben
mockPerson.Setup(x => x.Greet()).Returns("Mock greeting");
// Wir vergleichen die Ausgabe von Greet() von beiden
Console.WriteLine(person.Greet());
// Ausgabe: "Hello, my name is John"
Console.WriteLine(mockPerson.Object.Greet());
// Ausgabe: "Mock greeting"
}
}
|
import org.mockito.Mockito;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String greet() {
return "Hello, my name is " + this.name;
}
}
// Wir erstellen eine Instanz von Person
Person person = new Person("John");
// Wir erstellen eine Mock-Instanz von Person
Person mockPerson = Mockito.mock(Person.class);
// Wir konfigurieren die Methode greet() der Mock-Instanz,
// um immer "Mock greeting" zurückzugeben
Mockito.when(mockPerson.greet()).thenReturn("Mock greeting");
// Wir vergleichen die Ausgabe von greet() von beiden
System.out.println(person.greet());
// Ausgabe: "Hello, my name is John"
System.out.println(mockPerson.greet());
// Ausgabe: "Mock greeting"
|
from unittest.mock import Mock
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hello, my name is " + self.name
# Wir erstellen eine Instanz von Person
person = Person("John")
# Wir erstellen eine Mock-Instanz von Person
mock_person = Mock(spec=Person)
# Wir konfigurieren die Methode greet() der Mock-Instanz,
# um immer "Mock greeting" zurückzugeben
mock_person.greet.return_value = "Mock greeting"
# Wir vergleichen die Ausgabe von greet()
print(person.greet())
# Ausgabe: "Hello, my name is John"
print(mock_person.greet())
# Ausgabe: "Mock greeting"
|
Mock Demo in Python
import unittest
from unittest.mock import Mock, patch
import requests
import json
def get_users():
# API-Aufruf einer Beispiel API, der Benutzerdaten abruft
response = requests.get(f"https://gorest.co.in/public/v2/users")
if response.status_code == 200:
return response.json()
return None
def DoesGowdaExist(users:list[dict]):
# Testet ob ein Benutzer namens "Gowda" existiert
for user in users:
if "Gowda" in user["name"]:
return True
return False
class TestAPI(unittest.TestCase):
@patch("requests.get")
def test_get_user_data(self, mock_get):
# Unittest bei dem statt der Beipiel-API ein Mock verwendet wird
mock_result = [{"id": 1, "name": "Gowda",
'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
users = get_users()
print(f"users: {json.dumps(users, indent=4)}")
# Stelle sicher, dass der Rückgabewert des Mock-Objekts zurückgegeben wurde
self.assertEqual(users, mock_result)
self.assertTrue(DoesGowdaExist(users),"Gowda does not exist")
print("Gowda does exist")
def main(mock:bool):
# Wenn mock=False ist, wird die API aufgerufen
# Wenn mock=False ist wird der Unitest ausgeführt der den Aufruf der API mit einem Mock-Objekt überschreibt
if mock:
unittest.main()
else:
users= get_users()
print(f"users: {json.dumps(users, indent=4)} \n\n")
print(f"Gowda does {'' if DoesGowdaExist(users) else 'not '}exist")
if __name__ == '__main__':
main(mock=True)