Skip to content

Commit a500fcd

Browse files
authored
Remove unittest2 dependency (#764)
1 parent e06bbac commit a500fcd

17 files changed

+77
-122
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ before_script:
5555
- export COVERALLS_PARALLEL=true
5656

5757
script:
58-
- coverage run ./runtests.py
58+
- pytest -v --cov-config .coveragerc pymysql
5959
- if [ "${TEST_AUTH}" = "yes" ];
6060
then pytest -v --cov-config .coveragerc tests;
6161
fi

Pipfile

Lines changed: 0 additions & 13 deletions
This file was deleted.

pymysql/tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
from pymysql.tests.thirdparty import *
1515

1616
if __name__ == "__main__":
17-
import unittest2
18-
unittest2.main()
17+
import unittest
18+
unittest.main()

pymysql/tests/base.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,33 @@
33
import os
44
import re
55
import warnings
6-
7-
import unittest2
6+
import unittest
87

98
import pymysql
109
from .._compat import CPYTHON
1110

1211

13-
class PyMySQLTestCase(unittest2.TestCase):
12+
if CPYTHON:
13+
import atexit
14+
gc.set_debug(gc.DEBUG_UNCOLLECTABLE)
15+
16+
@atexit.register
17+
def report_uncollectable():
18+
import gc
19+
if not gc.garbage:
20+
print("No garbages!")
21+
return
22+
print('uncollectable objects')
23+
for obj in gc.garbage:
24+
print(obj)
25+
if hasattr(obj, '__dict__'):
26+
print(obj.__dict__)
27+
for ref in gc.get_referrers(obj):
28+
print("referrer:", ref)
29+
print('---')
30+
31+
32+
class PyMySQLTestCase(unittest.TestCase):
1433
# You can specify your test environment creating a file named
1534
# "databases.json" or editing the `databases` variable below.
1635
fname = os.path.join(os.path.dirname(__file__), "databases.json")
@@ -97,7 +116,6 @@ def safe_gc_collect(self):
97116
"""Ensure cycles are collected via gc.
98117
99118
Runs additional times on non-CPython platforms.
100-
101119
"""
102120
gc.collect()
103121
if not CPYTHON:

pymysql/tests/test_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import warnings
66

7-
from unittest2 import SkipTest
7+
import pytest
88

99
from pymysql import util
1010
import pymysql.cursors
@@ -143,7 +143,7 @@ def test_datetime_microseconds(self):
143143

144144
conn = self.connect()
145145
if not self.mysql_server_is(conn, (5, 6, 4)):
146-
raise SkipTest("target backend does not support microseconds")
146+
pytest.skip("target backend does not support microseconds")
147147
c = conn.cursor()
148148
dt = datetime.datetime(2013, 11, 12, 9, 9, 9, 123450)
149149
c.execute("create table test_datetime (id int, ts datetime(6))")
@@ -256,7 +256,7 @@ def test_json(self):
256256
args["charset"] = "utf8mb4"
257257
conn = pymysql.connect(**args)
258258
if not self.mysql_server_is(conn, (5, 7, 0)):
259-
raise SkipTest("JSON type is not supported on MySQL <= 5.6")
259+
pytest.skip("JSON type is not supported on MySQL <= 5.6")
260260

261261
self.safe_create_table(conn, "test_json", """\
262262
create table test_json (

pymysql/tests/test_connection.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
import sys
33
import time
4-
import unittest2
4+
import pytest
55
import pymysql
66
from pymysql.tests import base
77
from pymysql._compat import text_type
@@ -100,14 +100,14 @@ class TestAuthentication(base.PyMySQLTestCase):
100100
def test_plugin(self):
101101
conn = self.connect()
102102
if not self.mysql_server_is(conn, (5, 5, 0)):
103-
raise unittest2.SkipTest("MySQL-5.5 required for plugins")
103+
pytest.skip("MySQL-5.5 required for plugins")
104104
cur = conn.cursor()
105105
cur.execute("select plugin from mysql.user where concat(user, '@', host)=current_user()")
106106
for r in cur:
107107
self.assertIn(conn._auth_plugin_name, (r[0], 'mysql_native_password'))
108108

109-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
110-
@unittest2.skipIf(socket_found, "socket plugin already installed")
109+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
110+
@pytest.mark.skipif(socket_found, reason="socket plugin already installed")
111111
def testSocketAuthInstallPlugin(self):
112112
# needs plugin. lets install it.
113113
cur = self.connect().cursor()
@@ -124,13 +124,13 @@ def testSocketAuthInstallPlugin(self):
124124
self.realtestSocketAuth()
125125
except pymysql.err.InternalError:
126126
TestAuthentication.socket_found = False
127-
raise unittest2.SkipTest('we couldn\'t install the socket plugin')
127+
pytest.skip('we couldn\'t install the socket plugin')
128128
finally:
129129
if TestAuthentication.socket_found:
130130
cur.execute("uninstall plugin %s" % self.socket_plugin_name)
131131

132-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
133-
@unittest2.skipUnless(socket_found, "no socket plugin")
132+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
133+
@pytest.mark.skipif(not socket_found, reason="no socket plugin")
134134
def testSocketAuth(self):
135135
self.realtestSocketAuth()
136136

@@ -179,8 +179,8 @@ def __init__(self, con):
179179
self.con=con
180180

181181

182-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
183-
@unittest2.skipIf(two_questions_found, "two_questions plugin already installed")
182+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
183+
@pytest.mark.skipif(two_questions_found, reason="two_questions plugin already installed")
184184
def testDialogAuthTwoQuestionsInstallPlugin(self):
185185
# needs plugin. lets install it.
186186
cur = self.connect().cursor()
@@ -189,13 +189,13 @@ def testDialogAuthTwoQuestionsInstallPlugin(self):
189189
TestAuthentication.two_questions_found = True
190190
self.realTestDialogAuthTwoQuestions()
191191
except pymysql.err.InternalError:
192-
raise unittest2.SkipTest('we couldn\'t install the two_questions plugin')
192+
pytest.skip('we couldn\'t install the two_questions plugin')
193193
finally:
194194
if TestAuthentication.two_questions_found:
195195
cur.execute("uninstall plugin two_questions")
196196

197-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
198-
@unittest2.skipUnless(two_questions_found, "no two questions auth plugin")
197+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
198+
@pytest.mark.skipif(not two_questions_found, reason="no two questions auth plugin")
199199
def testDialogAuthTwoQuestions(self):
200200
self.realTestDialogAuthTwoQuestions()
201201

@@ -209,8 +209,8 @@ def realTestDialogAuthTwoQuestions(self):
209209
pymysql.connect(user='pymysql_2q', **self.db)
210210
pymysql.connect(user='pymysql_2q', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
211211

212-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
213-
@unittest2.skipIf(three_attempts_found, "three_attempts plugin already installed")
212+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
213+
@pytest.mark.skipif(three_attempts_found, reason="three_attempts plugin already installed")
214214
def testDialogAuthThreeAttemptsQuestionsInstallPlugin(self):
215215
# needs plugin. lets install it.
216216
cur = self.connect().cursor()
@@ -219,13 +219,13 @@ def testDialogAuthThreeAttemptsQuestionsInstallPlugin(self):
219219
TestAuthentication.three_attempts_found = True
220220
self.realTestDialogAuthThreeAttempts()
221221
except pymysql.err.InternalError:
222-
raise unittest2.SkipTest('we couldn\'t install the three_attempts plugin')
222+
pytest.skip('we couldn\'t install the three_attempts plugin')
223223
finally:
224224
if TestAuthentication.three_attempts_found:
225225
cur.execute("uninstall plugin three_attempts")
226226

227-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
228-
@unittest2.skipUnless(three_attempts_found, "no three attempts plugin")
227+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
228+
@pytest.mark.skipif(not three_attempts_found, reason="no three attempts plugin")
229229
def testDialogAuthThreeAttempts(self):
230230
self.realTestDialogAuthThreeAttempts()
231231

@@ -250,10 +250,10 @@ def realTestDialogAuthThreeAttempts(self):
250250
with self.assertRaises(pymysql.err.OperationalError):
251251
pymysql.connect(user='pymysql_3a', auth_plugin_map={b'dialog': TestAuthentication.Dialog}, **self.db)
252252

253-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
254-
@unittest2.skipIf(pam_found, "pam plugin already installed")
255-
@unittest2.skipIf(os.environ.get('PASSWORD') is None, "PASSWORD env var required")
256-
@unittest2.skipIf(os.environ.get('PAMSERVICE') is None, "PAMSERVICE env var required")
253+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
254+
@pytest.mark.skipif(pam_found, reason="pam plugin already installed")
255+
@pytest.mark.skipif(os.environ.get('PASSWORD') is None, reason="PASSWORD env var required")
256+
@pytest.mark.skipif(os.environ.get('PAMSERVICE') is None, reason="PAMSERVICE env var required")
257257
def testPamAuthInstallPlugin(self):
258258
# needs plugin. lets install it.
259259
cur = self.connect().cursor()
@@ -262,16 +262,16 @@ def testPamAuthInstallPlugin(self):
262262
TestAuthentication.pam_found = True
263263
self.realTestPamAuth()
264264
except pymysql.err.InternalError:
265-
raise unittest2.SkipTest('we couldn\'t install the auth_pam plugin')
265+
pytest.skip('we couldn\'t install the auth_pam plugin')
266266
finally:
267267
if TestAuthentication.pam_found:
268268
cur.execute("uninstall plugin pam")
269269

270270

271-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
272-
@unittest2.skipUnless(pam_found, "no pam plugin")
273-
@unittest2.skipIf(os.environ.get('PASSWORD') is None, "PASSWORD env var required")
274-
@unittest2.skipIf(os.environ.get('PAMSERVICE') is None, "PAMSERVICE env var required")
271+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
272+
@pytest.mark.skipif(not pam_found, reason="no pam plugin")
273+
@pytest.mark.skipif(os.environ.get('PASSWORD') is None, reason="PASSWORD env var required")
274+
@pytest.mark.skipif(os.environ.get('PAMSERVICE') is None, reason="PAMSERVICE env var required")
275275
def testPamAuth(self):
276276
self.realTestPamAuth()
277277

@@ -311,16 +311,16 @@ def realTestPamAuth(self):
311311
# select old_password("crummy p\tassword");
312312
#| old_password("crummy p\tassword") |
313313
#| 2a01785203b08770 |
314-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
315-
@unittest2.skipUnless(mysql_old_password_found, "no mysql_old_password plugin")
314+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
315+
@pytest.mark.skipif(not mysql_old_password_found, reason="no mysql_old_password plugin")
316316
def testMySQLOldPasswordAuth(self):
317317
conn = self.connect()
318318
if self.mysql_server_is(conn, (5, 7, 0)):
319-
raise unittest2.SkipTest('Old passwords aren\'t supported in 5.7')
319+
pytest.skip('Old passwords aren\'t supported in 5.7')
320320
# pymysql.err.OperationalError: (1045, "Access denied for user 'old_pass_user'@'localhost' (using password: YES)")
321321
# from login in MySQL-5.6
322322
if self.mysql_server_is(conn, (5, 6, 0)):
323-
raise unittest2.SkipTest('Old passwords don\'t authenticate in 5.6')
323+
pytest.skip('Old passwords don\'t authenticate in 5.6')
324324
db = self.db.copy()
325325
db['password'] = "crummy p\tassword"
326326
c = conn.cursor()
@@ -354,8 +354,8 @@ def testMySQLOldPasswordAuth(self):
354354
cur.execute("SELECT VERSION()")
355355
c.execute('set global secure_auth=%r' % secure_auth_setting)
356356

357-
@unittest2.skipUnless(socket_auth, "connection to unix_socket required")
358-
@unittest2.skipUnless(sha256_password_found, "no sha256 password authentication plugin found")
357+
@pytest.mark.skipif(not socket_auth, reason="connection to unix_socket required")
358+
@pytest.mark.skipif(not sha256_password_found, reason="no sha256 password authentication plugin found")
359359
def testAuthSHA256(self):
360360
conn = self.connect()
361361
c = conn.cursor()

pymysql/tests/test_err.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import unittest2
1+
import unittest
22

33
from pymysql import err
44

55

66
__all__ = ["TestRaiseException"]
77

88

9-
class TestRaiseException(unittest2.TestCase):
9+
class TestRaiseException(unittest.TestCase):
1010

1111
def test_raise_mysql_exception(self):
1212
data = b"\xff\x15\x04#28000Access denied"

pymysql/tests/test_issues.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import warnings
44
import sys
55

6+
import pytest
7+
68
import pymysql
79
from pymysql import cursors
810
from pymysql._compat import text_type
911
from pymysql.tests import base
10-
import unittest2
1112

1213
try:
1314
import imp
@@ -145,7 +146,7 @@ def test_issue_16(self):
145146
finally:
146147
c.execute("drop table issue16")
147148

148-
@unittest2.skip("test_issue_17() requires a custom, legacy MySQL configuration and will not be run.")
149+
@pytest.mark.skip("test_issue_17() requires a custom, legacy MySQL configuration and will not be run.")
149150
def test_issue_17(self):
150151
"""could not connect mysql use passwod"""
151152
conn = self.connect()
@@ -189,7 +190,7 @@ def test_issue_33(self):
189190
c.execute(u"select name from hei\xdfe")
190191
self.assertEqual(u"Pi\xdfata", c.fetchone()[0])
191192

192-
@unittest2.skip("This test requires manual intervention")
193+
@pytest.mark.skip("This test requires manual intervention")
193194
def test_issue_35(self):
194195
conn = self.connect()
195196
c = conn.cursor()

pymysql/tests/test_nextset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest2
1+
import pytest
22

33
import pymysql
44
from pymysql import util
@@ -50,7 +50,7 @@ def test_ok_and_next(self):
5050
self.assertEqual([(2,)], list(cur))
5151
self.assertFalse(bool(cur.nextset()))
5252

53-
@unittest2.expectedFailure
53+
@pytest.mark.xfail
5454
def test_multi_cursor(self):
5555
con = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
5656
cur1 = con.cursor()

pymysql/tests/thirdparty/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from .test_MySQLdb import *
22

33
if __name__ == "__main__":
4-
try:
5-
import unittest2 as unittest
6-
except ImportError:
7-
import unittest
4+
import unittest
85
unittest.main()

pymysql/tests/thirdparty/test_MySQLdb/capabilities.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
"""
77
import sys
88
from time import time
9-
try:
10-
import unittest2 as unittest
11-
except ImportError:
12-
import unittest
9+
import unittest
1310

1411
PY2 = sys.version_info[0] == 2
1512

pymysql/tests/thirdparty/test_MySQLdb/dbapi20.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
__version__ = '$Revision$'[11:-2]
1515
__author__ = 'Stuart Bishop <zen@shangri-la.dropbear.id.au>'
1616

17-
try:
18-
import unittest2 as unittest
19-
except ImportError:
20-
import unittest
21-
2217
import time
18+
import unittest
2319

2420
# $Log$
2521
# Revision 1.1.2.1 2006/02/25 03:44:32 adustman

pymysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_capabilities.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from . import capabilities
2-
try:
3-
import unittest2 as unittest
4-
except ImportError:
5-
import unittest
2+
import unittest
63
import pymysql
74
from pymysql.tests import base
85
import warnings

pymysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_dbapi20.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
import pymysql
33
from pymysql.tests import base
44

5-
try:
6-
import unittest2 as unittest
7-
except ImportError:
8-
import unittest
5+
import unittest
96

107

118
class test_MySQLdb(dbapi20.DatabaseAPI20Test):

0 commit comments

Comments
 (0)