Skip to content

Commit dfaa28b

Browse files
committed
convert standalone tests to use pytest instead of nose
1 parent d280152 commit dfaa28b

File tree

6 files changed

+41
-95
lines changed

6 files changed

+41
-95
lines changed

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,5 @@
6161
],
6262
py_modules=["tabulate"],
6363
entry_points={"console_scripts": console_scripts},
64-
extras_require={"widechars": ["wcwidth"]},
65-
test_suite="nose.collector",
64+
extras_require={"widechars": ["wcwidth"]}
6665
)

test/common.py

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
try:
2-
from nose.plugins.skip import SkipTest
3-
except ImportError:
4-
try:
5-
from unittest.case import SkipTest # Python >= 2.7
6-
except ImportError:
7-
try:
8-
from unittest2.case import SkipTest # Python < 2.7
9-
except ImportError:
1+
import pytest
2+
from pytest import skip, raises
103

11-
class SkipTest(Exception):
12-
"""Raise this exception to mark a test as skipped.
13-
"""
144

15-
pass
5+
def assert_equal(expected, result):
6+
print("Expected:\n%s\n" % expected)
7+
print("Got:\n%s\n" % result)
8+
assert expected == result
169

1710

18-
try:
19-
from nose.tools import assert_equal, assert_in, assert_raises
20-
21-
22-
except ImportError:
23-
24-
def assert_equal(expected, result):
25-
print("Expected:\n%s\n" % expected)
26-
print("Got:\n%s\n" % result)
27-
assert expected == result
28-
29-
def assert_in(result, expected_set):
30-
nums = range(1, len(expected_set) + 1)
31-
for i, expected in zip(nums, expected_set):
32-
print("Expected %d:\n%s\n" % (i, expected))
33-
print("Got:\n%s\n" % result)
34-
assert result in expected_set
35-
36-
class assert_raises(object):
37-
def __init__(self, exception_type):
38-
self.watch_exception_type = exception_type
39-
40-
def __enter__(self):
41-
pass
42-
43-
def __exit__(self, exception_type, exception_value, traceback):
44-
if isinstance(exception_value, self.watch_exception_type):
45-
return True # suppress exception
46-
elif exception_type is None:
47-
msg = "%s not raised" % self.watch_exception_type.__name__
48-
raise AssertionError(msg)
49-
# otherwise propagate whatever other exception is raised
11+
def assert_in(result, expected_set):
12+
nums = range(1, len(expected_set) + 1)
13+
for i, expected in zip(nums, expected_set):
14+
print("Expected %d:\n%s\n" % (i, expected))
15+
print("Got:\n%s\n" % result)
16+
assert (result in expected_set)

test/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import unicode_literals
77
from tabulate import tabulate, tabulate_formats, simple_separated_format
88
from platform import python_version_tuple
9-
from common import SkipTest
9+
from common import skip
1010

1111

1212
try:
@@ -31,7 +31,7 @@ def test_tabulate_formats():
3131

3232
def _check_signature(function, expected_sig):
3333
if not signature:
34-
raise SkipTest()
34+
skip("")
3535
actual_sig = signature(function)
3636
print("expected: %s\nactual: %s\n" % (expected_sig, str(actual_sig)))
3737
for (e, ev), (a, av) in zip(expected_sig, actual_sig.parameters.items()):

test/test_input.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import print_function
66
from __future__ import unicode_literals
77
from tabulate import tabulate
8-
from common import assert_equal, assert_in, assert_raises, SkipTest
8+
from common import assert_equal, assert_in, raises, skip
99

1010

1111
def test_iterable_of_iterables():
@@ -123,8 +123,7 @@ def test_numpy_2d():
123123
result = tabulate(na, ["a", "b", "c"])
124124
assert_equal(expected, result)
125125
except ImportError:
126-
print("test_numpy_2d is skipped")
127-
raise SkipTest() # this test is optional
126+
skip("test_numpy_2d is skipped")
128127

129128

130129
def test_numpy_2d_firstrow():
@@ -139,8 +138,7 @@ def test_numpy_2d_firstrow():
139138
result = tabulate(na, headers="firstrow")
140139
assert_equal(expected, result)
141140
except ImportError:
142-
print("test_numpy_2d_firstrow is skipped")
143-
raise SkipTest() # this test is optional
141+
skip("test_numpy_2d_firstrow is skipped")
144142

145143

146144
def test_numpy_2d_keys():
@@ -161,8 +159,7 @@ def test_numpy_2d_keys():
161159
result = tabulate(na, headers="keys")
162160
assert_equal(expected, result)
163161
except ImportError:
164-
print("test_numpy_2d_keys is skipped")
165-
raise SkipTest() # this test is optional
162+
skip("test_numpy_2d_keys is skipped")
166163

167164

168165
def test_numpy_record_array():
@@ -188,8 +185,7 @@ def test_numpy_record_array():
188185
result = tabulate(na)
189186
assert_equal(expected, result)
190187
except ImportError:
191-
print("test_numpy_2d_keys is skipped")
192-
raise SkipTest() # this test is optional
188+
skip("test_numpy_2d_keys is skipped")
193189

194190

195191
def test_numpy_record_array_keys():
@@ -215,8 +211,7 @@ def test_numpy_record_array_keys():
215211
result = tabulate(na, headers="keys")
216212
assert_equal(expected, result)
217213
except ImportError:
218-
print("test_numpy_2d_keys is skipped")
219-
raise SkipTest() # this test is optional
214+
skip("test_numpy_2d_keys is skipped")
220215

221216

222217
def test_numpy_record_array_headers():
@@ -242,8 +237,7 @@ def test_numpy_record_array_headers():
242237
result = tabulate(na, headers=["person", "years", "cm"])
243238
assert_equal(expected, result)
244239
except ImportError:
245-
print("test_numpy_2d_keys is skipped")
246-
raise SkipTest() # this test is optional
240+
skip("test_numpy_2d_keys is skipped")
247241

248242

249243
def test_pandas():
@@ -263,8 +257,7 @@ def test_pandas():
263257
result = tabulate(df, headers=["string", "number"])
264258
assert_equal(expected, result)
265259
except ImportError:
266-
print("test_pandas is skipped")
267-
raise SkipTest() # this test is optional
260+
skip("test_pandas is skipped")
268261

269262

270263
def test_pandas_firstrow():
@@ -281,8 +274,7 @@ def test_pandas_firstrow():
281274
result = tabulate(df, headers="firstrow")
282275
assert_equal(expected, result)
283276
except ImportError:
284-
print("test_pandas_firstrow is skipped")
285-
raise SkipTest() # this test is optional
277+
skip("test_pandas_firstrow is skipped")
286278

287279

288280
def test_pandas_keys():
@@ -304,8 +296,7 @@ def test_pandas_keys():
304296
result = tabulate(df, headers="keys")
305297
assert_equal(expected, result)
306298
except ImportError:
307-
print("test_pandas_keys is skipped")
308-
raise SkipTest() # this test is optional
299+
skip("test_pandas_keys is skipped")
309300

310301

311302
def test_sqlite3():
@@ -327,8 +318,7 @@ def test_sqlite3():
327318
Bob 27 175"""
328319
assert_equal(expected, result)
329320
except ImportError:
330-
print("test_sqlite3 is skipped")
331-
raise SkipTest() # this test is optional
321+
skip("test_sqlite3 is skipped")
332322

333323

334324
def test_sqlite3_keys():
@@ -352,8 +342,7 @@ def test_sqlite3_keys():
352342
Bob 27 175"""
353343
assert_equal(expected, result)
354344
except ImportError:
355-
print("test_sqlite3_keys is skipped")
356-
raise SkipTest() # this test is optional
345+
skip("test_sqlite3_keys is skipped")
357346

358347

359348
def test_list_of_namedtuples():
@@ -450,7 +439,7 @@ def test_list_of_dicts_with_list_of_headers():
450439
"Input: ValueError on a list of headers with a list of dicts (issue #23)"
451440
table = [{"letters": "ABCDE", "digits": 12345}]
452441
headers = ["DIGITS", "LETTERS"]
453-
with assert_raises(ValueError):
442+
with raises(ValueError):
454443
tabulate(table, headers=headers)
455444

456445

test/test_output.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import unicode_literals
77
import tabulate as tabulate_module
88
from tabulate import tabulate, simple_separated_format
9-
from common import assert_equal, assert_raises, SkipTest
9+
from common import assert_equal, raises, skip
1010

1111

1212
# _test_table shows
@@ -235,8 +235,7 @@ def test_grid_wide_characters():
235235
try:
236236
import wcwidth # noqa
237237
except ImportError:
238-
print("test_grid_wide_characters is skipped")
239-
raise SkipTest() # this test is optional
238+
skip("test_grid_wide_characters is skipped")
240239
headers = list(_test_table_headers)
241240
headers[1] = "配列"
242241
expected = "\n".join(
@@ -1005,7 +1004,6 @@ def test_moinmoin_headerless():
10051004

10061005
_test_table_html_headers = ["<strings>", "<&numbers&>"]
10071006
_test_table_html = [["spam >", 41.9999], ["eggs &", 451.0]]
1008-
assert_equal.__self__.maxDiff = None
10091007

10101008

10111009
def test_html():
@@ -1310,8 +1308,7 @@ def test_pandas_with_index():
13101308
result = tabulate(df, headers="keys")
13111309
assert_equal(expected, result)
13121310
except ImportError:
1313-
print("test_pandas_with_index is skipped")
1314-
raise SkipTest() # this test is optional
1311+
skip("test_pandas_with_index is skipped")
13151312

13161313

13171314
def test_pandas_without_index():
@@ -1333,8 +1330,7 @@ def test_pandas_without_index():
13331330
result = tabulate(df, headers="keys", showindex=False)
13341331
assert_equal(expected, result)
13351332
except ImportError:
1336-
print("test_pandas_without_index is skipped")
1337-
raise SkipTest() # this test is optional
1333+
skip("test_pandas_without_index is skipped")
13381334

13391335

13401336
def test_pandas_rst_with_index():
@@ -1358,8 +1354,7 @@ def test_pandas_rst_with_index():
13581354
result = tabulate(df, tablefmt="rst", headers="keys")
13591355
assert_equal(expected, result)
13601356
except ImportError:
1361-
print("test_pandas_rst_with_index is skipped")
1362-
raise SkipTest() # this test is optional
1357+
skip("test_pandas_rst_with_index is skipped")
13631358

13641359

13651360
def test_pandas_rst_with_named_index():
@@ -1384,8 +1379,7 @@ def test_pandas_rst_with_named_index():
13841379
result = tabulate(df, tablefmt="rst", headers="keys")
13851380
assert_equal(expected, result)
13861381
except ImportError:
1387-
print("test_pandas_rst_with_index is skipped")
1388-
raise SkipTest() # this test is optional
1382+
skip("test_pandas_rst_with_index is skipped")
13891383

13901384

13911385
def test_dict_like_with_index():
@@ -1418,9 +1412,8 @@ def test_list_of_lists_with_supplied_index():
14181412
assert_equal(result, expected)
14191413
# TODO: make it a separate test case
14201414
# the index must be as long as the number of rows
1421-
assert_raises(
1422-
ValueError, lambda: tabulate(dd, headers=["a", "b"], showindex=[1, 2])
1423-
)
1415+
with raises(ValueError):
1416+
tabulate(dd, headers=["a", "b"], showindex=[1, 2])
14241417

14251418

14261419
def test_list_of_lists_with_index_firstrow():
@@ -1433,9 +1426,8 @@ def test_list_of_lists_with_index_firstrow():
14331426
assert_equal(result, expected)
14341427
# TODO: make it a separate test case
14351428
# the index must be as long as the number of rows
1436-
assert_raises(
1437-
ValueError, lambda: tabulate(dd, headers="firstrow", showindex=[1, 2])
1438-
)
1429+
with raises(ValueError):
1430+
tabulate(dd, headers="firstrow", showindex=[1, 2])
14391431

14401432

14411433
def test_disable_numparse_default():

test/test_regression.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import print_function
66
from __future__ import unicode_literals
77
from tabulate import tabulate, _text_type, _long_type, TableFormat, Line, DataRow
8-
from common import assert_equal, assert_in, SkipTest
8+
from common import assert_equal, assert_in, skip
99

1010

1111
def test_ansi_color_in_table_cells():
@@ -273,8 +273,7 @@ def test_mix_normal_and_wide_characters():
273273
)
274274
assert_equal(result, expected)
275275
except ImportError:
276-
print("test_mix_normal_and_wide_characters is skipped (requires wcwidth lib)")
277-
raise SkipTest()
276+
skip("test_mix_normal_and_wide_characters is skipped (requires wcwidth lib)")
278277

279278

280279
def test_align_long_integers():
@@ -295,7 +294,7 @@ def test_numpy_array_as_headers():
295294
expected = "foo bar"
296295
assert_equal(result, expected)
297296
except ImportError:
298-
raise SkipTest()
297+
raise skip("")
299298

300299

301300
def test_boolean_columns():

0 commit comments

Comments
 (0)