Skip to content

Commit 0ba26c4

Browse files
committed
Remove hard requirement for py.test by vendorizing assertRaises
1 parent 826fa32 commit 0ba26c4

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

disqusapi/tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import mock
22
import os
3-
import pytest
43
import socket
5-
import unittest
64

75
import disqusapi
86
from disqusapi.compat import xrange
7+
from disqusapi.tests_compat import TestCase
98

109
extra_interface = {
1110
"reserved": {
@@ -70,7 +69,7 @@ def read(self):
7069
return self.body
7170

7271

73-
class DisqusAPITest(unittest.TestCase):
72+
class DisqusAPITest(TestCase):
7473
API_SECRET = 'b' * 64
7574
API_PUBLIC = 'c' * 64
7675
HOST = os.environ.get('DISQUS_API_HOST', disqusapi.HOST)
@@ -156,17 +155,18 @@ def test_endpoint(self):
156155

157156
def test_update_interface_legacy(self):
158157
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
159-
with pytest.raises(disqusapi.InterfaceNotDefined):
158+
with self.assertRaises(disqusapi.InterfaceNotDefined):
160159
api.interface.update(extra_interface)
161160

162161
def test_invalid_method(self):
163162
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
164-
with pytest.raises(disqusapi.InvalidHTTPMethod):
163+
with self.assertRaises(disqusapi.InvalidHTTPMethod):
165164
api.get('posts.list', method='lol', forum='disqus')
166165

167166
def test_update_interface(self):
168167
api = disqusapi.DisqusAPI(self.API_SECRET, self.API_PUBLIC)
169168
api.update_interface(extra_interface)
170169

171170
if __name__ == '__main__':
171+
import unittest
172172
unittest.main()

disqusapi/tests_compat.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import unittest
2+
import sys
3+
4+
if sys.version_info < (2, 7):
5+
# Stolen from unittest2
6+
import re
7+
8+
class _AssertRaisesBaseContext(object):
9+
10+
def __init__(self, expected, test_case, callable_obj=None,
11+
expected_regex=None):
12+
self.expected = expected
13+
self.failureException = test_case.failureException
14+
if callable_obj is not None:
15+
try:
16+
self.obj_name = callable_obj.__name__
17+
except AttributeError:
18+
self.obj_name = str(callable_obj)
19+
else:
20+
self.obj_name = None
21+
if isinstance(expected_regex, basestring):
22+
expected_regex = re.compile(expected_regex)
23+
self.expected_regex = expected_regex
24+
25+
class _AssertRaisesContext(_AssertRaisesBaseContext):
26+
"""A context manager used to implement TestCase.assertRaises* methods."""
27+
28+
def __enter__(self):
29+
return self
30+
31+
def __exit__(self, exc_type, exc_value, tb):
32+
if exc_type is None:
33+
try:
34+
exc_name = self.expected.__name__
35+
except AttributeError:
36+
exc_name = str(self.expected)
37+
raise self.failureException(
38+
"%s not raised" % (exc_name,))
39+
if not issubclass(exc_type, self.expected):
40+
# let unexpected exceptions pass through
41+
return False
42+
self.exception = exc_value # store for later retrieval
43+
if self.expected_regex is None:
44+
return True
45+
46+
expected_regex = self.expected_regex
47+
if not expected_regex.search(str(exc_value)):
48+
raise self.failureException(
49+
'%r does not match %r' %
50+
(expected_regex.pattern, str(exc_value)))
51+
return True
52+
53+
class TestCase(unittest.TestCase):
54+
def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
55+
"""Fail unless an exception of class excClass is thrown
56+
by callableObj when invoked with arguments args and keyword
57+
arguments kwargs. If a different type of exception is
58+
thrown, it will not be caught, and the test case will be
59+
deemed to have suffered an error, exactly as for an
60+
unexpected exception.
61+
62+
If called with callableObj omitted or None, will return a
63+
context object used like this::
64+
65+
with self.assertRaises(SomeException):
66+
do_something()
67+
68+
The context manager keeps a reference to the exception as
69+
the 'exception' attribute. This allows you to inspect the
70+
exception after the assertion::
71+
72+
with self.assertRaises(SomeException) as cm:
73+
do_something()
74+
the_exception = cm.exception
75+
self.assertEqual(the_exception.error_code, 3)
76+
"""
77+
if callableObj is None:
78+
return _AssertRaisesContext(excClass, self)
79+
try:
80+
callableObj(*args, **kwargs)
81+
except excClass:
82+
return
83+
84+
if hasattr(excClass, '__name__'):
85+
excName = excClass.__name__
86+
else:
87+
excName = str(excClass)
88+
raise self.failureException, "%s not raised" % excName
89+
else:
90+
TestCase = unittest.TestCase

0 commit comments

Comments
 (0)