33
44import importlib
55import os
6- import unittest
7- import socket
6+ import pytest
87
98import gevent
109from gevent import monkey
1110from instana import apply_gevent_monkey_patch
1211
12+ # Teardown not working as expected, run each testcase separately
13+ class TestGEventAutoTrace :
1314
14- class TestGEventAutoTrace (unittest .TestCase ):
15- def setUp (self ):
15+ @pytest .fixture (autouse = True )
16+ def setup_environment (self ):
17+ """Setup test environment before each test"""
1618 # Ensure that the test suite is operational even when Django is installed
1719 # but not running or configured
1820 os .environ ['DJANGO_SETTINGS_MODULE' ] = ''
19-
21+
2022 self .default_patched_modules = ('socket' , 'time' , 'select' , 'os' ,
2123 'threading' , 'ssl' , 'subprocess' , 'signal' , 'queue' ,)
22-
23- def tearDown (self ):
24+
25+ yield
26+
27+ # Teardown
2428 if os .environ .get ('INSTANA_GEVENT_MONKEY_OPTIONS' ):
2529 os .environ .pop ('INSTANA_GEVENT_MONKEY_OPTIONS' )
26-
30+
2731 # Clean up after gevent monkey patches, by restore from the saved dict
2832 for modname in monkey .saved .keys ():
2933 try :
@@ -35,37 +39,34 @@ def tearDown(self):
3539 pass
3640 monkey .saved = {}
3741
38-
3942 def test_default_patch_all (self ):
4043 apply_gevent_monkey_patch ()
4144 for module_name in self .default_patched_modules :
42- self .assertTrue (monkey .is_module_patched (module_name ),
43- f"{ module_name } is not patched" )
45+ assert monkey .is_module_patched (module_name ), f"{ module_name } is not patched"
4446
4547 def test_instana_monkey_options_only_time (self ):
4648 os .environ ['INSTANA_GEVENT_MONKEY_OPTIONS' ] = (
4749 'time,no-socket,no-select,no-os,no-select,no-threading,no-os,'
4850 'no-ssl,no-subprocess,' 'no-signal,no-queue' )
4951 apply_gevent_monkey_patch ()
50-
51- self . assertTrue ( monkey .is_module_patched ('time' ), "time module is not patched" )
52+
53+ assert monkey .is_module_patched ('time' ), "time module is not patched"
5254 not_patched_modules = (m for m in self .default_patched_modules if m not in ('time' , 'threading' ))
53-
55+
5456 for module_name in not_patched_modules :
55- self .assertFalse (monkey .is_module_patched (module_name ),
56- f"{ module_name } is patched, when it shouldn't be" )
57-
57+ assert not monkey .is_module_patched (module_name ), \
58+ f"{ module_name } is patched, when it shouldn't be"
5859
5960 def test_instana_monkey_options_only_socket (self ):
6061 os .environ ['INSTANA_GEVENT_MONKEY_OPTIONS' ] = (
6162 '--socket, --no-time, --no-select, --no-os, --no-queue, --no-threading,'
6263 '--no-os, --no-ssl, no-subprocess, --no-signal, --no-select,' )
6364 apply_gevent_monkey_patch ()
64-
65- self . assertTrue ( monkey .is_module_patched ('socket' ), "socket module is not patched" )
65+
66+ assert monkey .is_module_patched ('socket' ), "socket module is not patched"
6667 not_patched_modules = (m for m in self .default_patched_modules if m not in ('socket' , 'threading' ))
67-
68+
6869 for module_name in not_patched_modules :
69- self . assertFalse ( monkey .is_module_patched (module_name ),
70- f"{ module_name } is patched, when it shouldn't be" )
70+ assert not monkey .is_module_patched (module_name ), \
71+ f"{ module_name } is patched, when it shouldn't be"
7172
0 commit comments