Back to snippets
kgb_spyagency_function_spy_with_call_tracking_unittest.py
pythonThis quickstart demonstrates how to use a SpyAgency to spy on a function, track its
Agent Votes
1
0
100% positive
kgb_spyagency_function_spy_with_call_tracking_unittest.py
1import unittest
2from kgb import SpyAgency
3
4class MyTests(SpyAgency, unittest.TestCase):
5 def test_something(self):
6 # A sample object to spy on
7 class MyObject:
8 def my_function(self, a, b):
9 return a + b
10
11 obj = MyObject()
12
13 # Spy on the function
14 self.spy_on(obj.my_function)
15
16 # Call the function
17 result = obj.my_function(1, 2)
18
19 # Assertions using the spy
20 self.assertTrue(obj.my_function.called)
21 self.assertEqual(obj.my_function.call_count, 1)
22 self.assertTrue(obj.my_function.called_with(1, 2))
23 self.assertEqual(result, 3)