userver: samples/testsuite-support/tests/test_testpoint.py
Loading...
Searching...
No Matches
samples/testsuite-support/tests/test_testpoint.py
1import pytest
3
4import testsuite.plugins.testpoint
5
6
7# /// [Testpoint - fixture]
8async def test_basic(service_client, testpoint: testsuite.plugins.testpoint.TestpointFixture):
9 @testpoint('simple-testpoint')
10 def simple_testpoint(data: dict):
11 assert data == {'payload': 'Hello, world!'}
12
13 response = await service_client.get('/testpoint')
14 assert response.status == 200
15 assert 'application/json' in response.headers['Content-Type']
16 assert simple_testpoint.times_called == 1
17 # /// [Testpoint - fixture]
18
19
20# /// [Sample TESTPOINT_CALLBACK usage python]
21async def test_injection(service_client, testpoint: testsuite.plugins.testpoint.TestpointFixture):
22 @testpoint('injection-point')
23 def injection_point(data: dict):
24 return {'value': 'injected'}
25
26 response = await service_client.get('/testpoint')
27 assert response.status == 200
28 assert 'application/json' in response.headers['Content-Type']
29 assert response.json() == {'value': 'injected'}
30
31 # testpoint supports callqueue interface
32 assert injection_point.times_called == 1
33 # /// [Sample TESTPOINT_CALLBACK usage python]
34
35
36async def test_disabled_testpoint(service_client, testpoint):
37 response = await service_client.get('/testpoint')
38 assert response.status == 200
39
40 @testpoint('injection-point')
41 def injection_point(data: dict):
42 return {'value': 'injected'}
43
44 # /// [Unregistered testpoint usage]
45 with pytest.raises(
47 ):
48 assert injection_point.times_called == 0
49 # /// [Unregistered testpoint usage]
50
51 await service_client.update_server_state()
52 assert injection_point.times_called == 0
53
54
55async def test_manual_testpoint_registration(service_client, testpoint):
56 # /// [Manual registration]
57 @testpoint('injection-point')
58 def injection_point(data):
59 return {'value': 'injected'}
60
61 await service_client.update_server_state()
62 assert injection_point.times_called == 0
63 # /// [Manual registration]
64
65 response = await service_client.get('/testpoint')
66 assert response.status == 200
67
68 assert injection_point.times_called == 1