10000 ref: Do not use test classes for tornado tests (#292) · etherscan-io/sentry-python@d719ce0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d719ce0

Browse files
authored
ref: Do not use test classes for tornado tests (getsentry#292)
1 parent c359d69 commit d719ce0

File tree

2 files changed

+103
-87
lines changed

2 files changed

+103
-87
lines changed

scripts/runtox.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ else
1414
searchstring="$1"
1515
fi
1616

17-
exec $TOXPATH -p auto -e $(tox -l | grep "$searchstring" | tr '\n' ',') -- "${@:2}"
17+
exec $TOXPATH -p auto -e $($TOXPATH -l | grep "$searchstring" | tr '\n' ',') -- "${@:2}"

tests/integrations/tornado/test_tornado.py

Lines changed: 102 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,115 +8,131 @@
88
from tornado.testing import AsyncHTTPTestCase
99

1010

11+
@pytest.fixture
12+
def tornado_testcase(request):
13+
# Take the unittest class provided by tornado and manually call its setUp
14+
# and tearDown.
15+
#
16+
# The pytest plugins for tornado seem too complicated to use, as they for
17+
# some reason assume I want to write my tests in async code.
18+
def inner(app):
19+
class TestBogus(AsyncHTTPTestCase):
20+
def get_app(self):
21+
return app
22+
23+
def bogustest(self):
24+
# We need to pass a valid test method name to the ctor, so this
25+
# is the method. It does nothing.
26+
pass
27+
28+
self = TestBogus("bogustest")
29+
self.setUp()
30+
request.addfinalizer(self.tearDown)
31+
return self
32+
33+
return inner
34+
35+
1136
class CrashingHandler(RequestHandler):
1237
def get(self):
1338
with configure_scope() as scope:
1439
scope.set_tag("foo", 42)
1540
1 / 0
1641

1742

18-
class TestBasic(AsyncHTTPTestCase):
19-
@pytest.fixture(autouse=True)
20-
def initialize(self, sentry_init, capture_events):
21-
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
22-
self.events = capture_events()
23-
24-
def get_app(self):
25-
26-
return Application([(r"/hi", CrashingHandler)])
27-
28-
def test_basic(self):
29-
response = self.fetch(
30-
"/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"}
31-
)
32-
assert response.code == 500
33-
34-
event, = self.events
35-
exception, = event["exception"]["values"]
36-
assert exception["type"] == "ZeroDivisionError"
37-
38-
request = event["request"]
39-
host = request["headers"]["Host"]
40-
assert event["request"] == {
41-
"env": {"REMOTE_ADDR": "127.0.0.1"},
42-
"headers": {
43-
"Accept-Encoding": "gzip",
44-
"Connection": "close",
45-
"Host": host,
46-
"Cookie": "name=value; name2=value2; name3=value3",
47-
},
48-
"cookies": {"name": "value", "name2": "value2", "name3": "value3"},
49-
"method": "GET",
50-
"query_string": "foo=bar",
51-
"url": "http://{host}/hi".format(host=host),
52-
}
53-
54-
assert event["tags"] == {"foo": 42}
55-
assert (
56-
event["transaction"]
57-
== "tests.integrations.tornado.test_tornado.CrashingHandler.get"
58-
)
43+
def test_basic(tornado_testcase, sentry_init, capture_events):
44+
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
45+
events = capture_events()
46+
client = tornado_testcase(Application([(r"/hi", CrashingHandler)]))
47+
48+
response = client.fetch(
49+
"/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"}
50+
)
51+
assert response.code == 500
52+
53+
event, = events
54+
exception, = event["exception"]["values"]
55+
assert exception["type"] == "ZeroDivisionError"
56+
57+
request = event["request"]
58+
host = request["headers"]["Host"]
59+
assert event["request"] == {
60+
"env": {"REMOTE_ADDR": "127.0.0.1"},
61+
"headers": {
62+
"Accept-Encoding": "gzip",
63+
"Connection": "close",
64+
"Host": host,
65+
"Cookie": "name=value; name2=value2; name3=value3",
66+
},
67+
"cookies": {"name": "value", "name2": "value2", "name3": "value3"},
68+
"method": "GET",
69+
"query_string": "foo=bar",
70+
"url": "http://{host}/hi".format(host=host),
71+
}
72+
73+
assert event["tags"] == {"foo": 42}
74+
assert (
75+
event["transaction"]
76+
== "tests.integrations.tornado.test_tornado.CrashingHandler.get"
77+
)
78+
79+
with configure_scope() as scope:
80+
assert not scope._tags
5981

60-
with configure_scope() as scope:
61-
assert not scope._tags
6282

83+
def test_400_not_logged(tornado_testcase, sentry_init, capture_events):
84+
sentry_init(integrations=[TornadoIntegration()])
85+
events = capture_events()
6386

64-
class Test400NotLogged(AsyncHTTPTestCase):
65-
@pytest.fixture(autouse=True)
66-
def initialize(self, sentry_init, capture_events):
67-
sentry_init(integrations=[TornadoIntegration()])
68-
self.events = capture_events()
87+
class CrashingHandler(RequestHandler):
88+
def get(self):
89+
raise HTTPError(400, "Oops")
6990

70-
def get_app(self):
71-
class CrashingHandler(RequestHandler):
72-
def get(self):
73-
raise HTTPError(400, "Oops")
91+
client = tornado_testcase(Application([(r"/", CrashingHandler)]))
7492

75-
return Application([(r"/", CrashingHandler)])
93+
response = client.fetch("/")
94+
assert response.code == 400
7695

77-
def test_400_not_logged(self):
78-
response = self.fetch("/")
79-
assert response.code == 400
96+
assert not events
8097

81-
assert not self.events
8298

99+
def test_user_auth(tornado_testcase, sentry_init, capture_events):
100+
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
101+
events = capture_events()
83102

84-
class TestUserAuth(AsyncHTTPTestCase):
85-
@pytest.fixture(autouse=True)
86-
def initialize(self, sentry_init, capture_events):
87-
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
88-
self.events = capture_events()
103+
class UserHandler(RequestHandler):
104+
def get(self):
105+
1 / 0
89106

90-
def get_app(self):
91-
class UserHandler(RequestHandler):
92-
def get(self):
93-
1 / 0
107+
def get_current_user(self):
108+
return 42
94109

95-
def get_current_user(self):
96-
return 42
110+
class NoUserHandler(RequestHandler):
111+
def get(self):
112+
1 / 0
97113

98-
class NoUserHandler(RequestHandler):
99-
def get(self):
100-
1 / 0
114+
client = tornado_testcase(
115+
Application([(r"/auth", UserHandler), (r"/noauth", NoUserHandler)])
116+
)
101117

102-
return Application([(r"/auth", UserHandler), (r"/noauth", NoUserHandler)])
118+
# has user
119+
response = client.fetch("/auth")
120+
assert response.code == 500
103121

104-
def test_has_user(self):
105-
response = self.fetch("/auth")
106-
assert response.code == 500
122+
event, = events
123+
exception, = event["exception"]["values"]
124+
assert exception["type"] == "ZeroDivisionError"
107125

108-
event, = self.events
109-
exception, = event["exception"]["values"]
110-
assert exception["type"] == "ZeroDivisionError"
126+
assert event["user"] == {"is_authenticated": True}
111127

112-
assert event["user"] == {"is_authenticated": True}
128+
events.clear()
113129

114-
def test_has_no_user(self):
115-
response = self.fetch("/noauth")
116-
assert response.code == 500
130+
# has no user
131+
response = client.fetch("/noauth")
132+
assert response.code == 500
117133

118-
event, = self.events
119-
exception, = event["exception"]["values"]
120-
assert exception["type"] == "ZeroDivisionError"
134+
event, = events
135+
exception, = event["exception"]["values"]
136+
assert exception["type"] == "ZeroDivisionError"
121137

122-
assert "user" not in event
138+
assert "user" not in event

0 commit comments

Comments
 (0)
0