Skip to content

Commit a71b728

Browse files
committed
Add Usage chapter to README.md
1 parent 11b2214 commit a71b728

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,119 @@ This minimal library aims to simplify behavioural testing with Python's standard
1717
pip install unittest-extensions
1818
```
1919
# Usage
20+
Suppose you have some code that looks like this:
2021

22+
```py
23+
from dataclasses import dataclass
24+
25+
@dataclass
26+
class User:
27+
name: str
28+
surname: str
29+
30+
def is_relative_to(self, user: "User") -> bool:
31+
return self.surname.casefold() == user.surname.casefold()
32+
```
33+
This is a dummy example, meaning that how exactly the User and their methods are implemented does not really matter; what we actually care about here is how to test this code given the above implementation.
34+
Say we'd like to test the `is_relative_to` method with pairs of User names and surnames using the standard `unittest` library.
35+
So, here's an example of how we could do that as simply as possible:
36+
## unittest
37+
38+
```py
39+
from unittest import main, TestCase
40+
41+
42+
class TestIsRelativeToSameName(TestCase):
43+
def test_same_name(self):
44+
user1 = User("Niklas", "Strindberg")
45+
user2 = User("Niklas", "Ibsen")
46+
self.assertFalse(user1.is_relative_to(user2))
47+
48+
def test_same_empty_name(self):
49+
user1 = User("", "Stringberg")
50+
user2 = User("", "Ibsen")
51+
self.assertFalse(user1.is_relative_to(user2))
52+
53+
54+
class TestIsRelativeToSameSurname(TestCase):
55+
def test_same_surname(self):
56+
user1 = User("August", "Nietzsche")
57+
user2 = User("Henrik", "Nietzsche")
58+
self.assertTrue(user1.is_relative_to(user2))
59+
60+
def test_same_empty_surname(self):
61+
user1 = User("August", "")
62+
user2 = User("Henrik", "")
63+
self.assertTrue(user1.is_relative_to(user2))
64+
65+
def test_same_surname_case_sensitive(self):
66+
user1 = User("August", "NiEtZsChE")
67+
user2 = User("Henrik", "nietzsche")
68+
self.assertTrue(user1.is_relative_to(user2))
69+
70+
def test_surname1_contains_surname2(self):
71+
user1 = User("August", "Solzenietzsche")
72+
user2 = User("Henrik", "Nietzsche")
73+
self.assertFalse(user1.is_relative_to(user2))
74+
75+
76+
if __name__ == "__main__":
77+
main()
78+
```
79+
80+
The cases we check here are rather limited but still there is some duplication in our code; we use many lines to create our User subjects. Of course we can avoid that
81+
by creating custom assertion methods that receive only the parameters that change
82+
between tests, but that's why a testing library is here for.
83+
Here's how we could write the above code with `unittest-extensions`:
84+
85+
## unittest-extensions
86+
```py
87+
from unittest import main
88+
89+
from unittest_extensions import TestCase, args
90+
91+
92+
class TestIsRelativeToSameName(TestCase):
93+
def subject(self, name1, name2):
94+
return User(name1, "Strindberg").is_relative_to(User(name2, "Ibsen"))
95+
96+
@args({"name1": "Niklas", "name2": "Niklas"})
97+
def test_same_name(self):
98+
self.assertResultFalse()
99+
100+
@args({"name1": "", "name2": ""})
101+
def test_same_empty_name(self):
102+
self.assertResultFalse()
103+
104+
105+
class TestIsRelativeToSameSurname(TestCase):
106+
def subject(self, surname1, surname2):
107+
return User("August", surname1).is_relative_to(User("Henrik", surname2))
108+
109+
@args({"surname1": "Nietzsche", "surname2": "Nietzsche"})
110+
def test_same_surname(self):
111+
self.assertResultTrue()
112+
113+
@args({"surname1": "", "surname2": ""})
114+
def test_same_empty_surname(self):
115+
self.assertResultTrue()
116+
117+
@args({"surname1": "NiEtZsChE", "surname2": "Nietzsche"})
118+
def test_same_surname_case_sensitive(self):
119+
self.assertResultTrue()
120+
121+
@args({"surname1": "Nietzsche", "surname2": "Solszenietzsche"})
122+
def test_surname2_contains_surname1(self):
123+
self.assertResultFalse()
124+
125+
126+
if __name__ == "__main__":
127+
main()
128+
```
129+
130+
The number of lines is still the same, but the testing code has become clearer:
131+
1. The subject of our assertions in each test case is documented in the `subject` method
132+
2. Each test method contains only information we care about, i.e. the input data (names/surnames) we test and the result of the assertion (true/false).
21133

22134
# License
23135
[MIT License](https://opensource.org/license/mit/)

0 commit comments

Comments
 (0)