Skip to content

Commit 684fb15

Browse files
committed
TST: switch to using pytest
- also start using codecov
1 parent 841f6d1 commit 684fb15

File tree

5 files changed

+77
-88
lines changed

5 files changed

+77
-88
lines changed

.coveragerc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[run]
2+
source=cycler
3+
branch=True
4+
[report]
5+
omit =
6+
*test*

.travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ matrix:
1212

1313
install:
1414
- python setup.py install
15-
- pip install coveralls six
15+
- pip install pytest pytest-cov coverage six
1616

1717
script:
18-
- python run_tests.py
18+
- coverage run run_tests.py
19+
- coverage report -m
1920

2021
after_success:
21-
coveralls
22+
- bash <(curl -s https://codecov.io/bash)

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ install:
4444
- "python -c \"import struct; print(struct.calcsize('P') * 8)\""
4545

4646
# Install the build and runtime dependencies of the project.
47-
- "%CMD_IN_ENV% pip install -v six nose coveralls"
47+
- "%CMD_IN_ENV% pip install -v six nose pytest pytest-cov coverage"
4848

4949
# Install the generated wheel package to test it
5050
- "python setup.py install"

run_tests.py

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
#!/usr/bin/env python
2-
# This file is closely based on tests.py from matplotlib
3-
#
4-
# This allows running the matplotlib tests from the command line: e.g.
5-
#
6-
# $ python run_tests.py -v -d
7-
#
8-
# The arguments are identical to the arguments accepted by nosetests.
9-
#
10-
# See https://nose.readthedocs.org/ for a detailed description of
11-
# these options.
12-
import nose
13-
14-
15-
env = {"NOSE_WITH_COVERAGE": 1,
16-
'NOSE_COVER_PACKAGE': ['cycler'],
17-
'NOSE_COVER_HTML': 1}
18-
plugins = []
19-
20-
21-
def run():
22-
23-
nose.main(addplugins=[x() for x in plugins], env=env)
24-
2+
import sys
3+
import pytest
254

265
if __name__ == '__main__':
27-
run()
6+
# show output results from every test function
7+
args = []
8+
args.extend(sys.argv[1:])
9+
# call pytest and exit with the return code from pytest so that
10+
# travis will fail correctly if tests fail
11+
sys.exit(pytest.main(args))

test_cycler.py

+58-60
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
import six
44
from six.moves import zip, range
55
from cycler import cycler, Cycler, concat
6-
from nose.tools import (assert_equal, assert_not_equal,
7-
assert_raises, assert_true)
6+
import pytest
87
from itertools import product, cycle, chain
98
from operator import add, iadd, mul, imul
109
from collections import defaultdict
1110

1211

1312
def _cycler_helper(c, length, keys, values):
14-
assert_equal(len(c), length)
15-
assert_equal(len(c), len(list(c)))
16-
assert_equal(c.keys, set(keys))
17-
13+
assert len(c) == length
14+
assert len(c) == len(list(c))
15+
assert c.keys == set(keys)
1816
for k, vals in zip(keys, values):
1917
for v, v_target in zip(c, vals):
20-
assert_equal(v[k], v_target)
18+
assert v[k] == v_target
2119

2220

2321
def _cycles_equal(c1, c2):
24-
assert_equal(list(c1), list(c2))
22+
assert list(c1) == list(c2)
2523

2624

2725
def test_creation():
@@ -42,8 +40,10 @@ def test_compose():
4240
yield _cycler_helper, c2+c1, 3, ['c', 'lw'], [list('rgb'), range(3)]
4341
yield _cycles_equal, c2+c1, c1+c2
4442
# miss-matched add lengths
45-
assert_raises(ValueError, add, c1, c3)
46-
assert_raises(ValueError, add, c3, c1)
43+
with pytest.raises(ValueError):
44+
c1 + c3
45+
with pytest.raises(ValueError):
46+
c3 + c1
4747

4848
# multiplication
4949
target = zip(*product(list('rgb'), range(3)))
@@ -92,16 +92,16 @@ def test_constructor():
9292
def test_failures():
9393
c1 = cycler(c='rgb')
9494
c2 = cycler(c=c1)
95-
assert_raises(ValueError, add, c1, c2)
96-
assert_raises(ValueError, iadd, c1, c2)
97-
assert_raises(ValueError, mul, c1, c2)
98-
assert_raises(ValueError, imul, c1, c2)
99-
assert_raises(TypeError, iadd, c2, 'aardvark')
100-
assert_raises(TypeError, imul, c2, 'aardvark')
95+
pytest.raises(ValueError, add, c1, c2)
96+
pytest.raises(ValueError, iadd, c1, c2)
97+
pytest.raises(ValueError, mul, c1, c2)
98+
pytest.raises(ValueError, imul, c1, c2)
99+
pytest.raises(TypeError, iadd, c2, 'aardvark')
100+
pytest.raises(TypeError, imul, c2, 'aardvark')
101101

102102
c3 = cycler(ec=c1)
103103

104-
assert_raises(ValueError, cycler, c=c2+c3)
104+
pytest.raises(ValueError, cycler, c=c2+c3)
105105

106106

107107
def test_simplify():
@@ -123,9 +123,9 @@ def test_multiply():
123123

124124
def test_mul_fails():
125125
c1 = cycler(c='rgb')
126-
assert_raises(TypeError, mul, c1, 2.0)
127-
assert_raises(TypeError, mul, c1, 'a')
128-
assert_raises(TypeError, mul, c1, [])
126+
pytest.raises(TypeError, mul, c1, 2.0)
127+
pytest.raises(TypeError, mul, c1, 'a')
128+
pytest.raises(TypeError, mul, c1, [])
129129

130130

131131
def test_getitem():
@@ -140,15 +140,14 @@ def test_getitem():
140140

141141
def test_fail_getime():
142142
c1 = cycler(lw=range(15))
143-
assert_raises(ValueError, Cycler.__getitem__, c1, 0)
144-
assert_raises(ValueError, Cycler.__getitem__, c1, [0, 1])
143+
pytest.raises(ValueError, Cycler.__getitem__, c1, 0)
144+
pytest.raises(ValueError, Cycler.__getitem__, c1, [0, 1])
145145

146146

147147
def _repr_tester_helper(rpr_func, cyc, target_repr):
148148
test_repr = getattr(cyc, rpr_func)()
149149

150-
assert_equal(six.text_type(test_repr),
151-
six.text_type(target_repr))
150+
assert six.text_type(test_repr) == six.text_type(target_repr)
152151

153152

154153
def test_repr():
@@ -172,13 +171,13 @@ def test_repr():
172171
def test_call():
173172
c = cycler(c='rgb')
174173
c_cycle = c()
175-
assert_true(isinstance(c_cycle, cycle))
174+
assert isinstance(c_cycle, cycle)
176175
j = 0
177176
for a, b in zip(2*c, c_cycle):
178177
j += 1
179-
assert_equal(a, b)
178+
assert a == b
180179

181-
assert_equal(j, len(c) * 2)
180+
assert j == len(c) * 2
182181

183182

184183
def test_copying():
@@ -202,21 +201,21 @@ def test_copying():
202201

203202
c_after = (c1 + c2) * c3
204203

205-
assert_equal(c1, cycler('c', [1, 2, 3]))
206-
assert_equal(c2, cycler('lw', ['r', 'g', 'b']))
207-
assert_equal(c3, cycler('foo', [['y', 'g', 'blue'], ['b', 'k']]))
208-
assert_equal(c_before, (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
209-
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']])))
210-
assert_equal(c_after, (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
211-
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']])))
204+
assert c1 == cycler('c', [1, 2, 3])
205+
assert c2 == cycler('lw', ['r', 'g', 'b'])
206+
assert c3 == cycler('foo', [['y', 'g', 'blue'], ['b', 'k']])
207+
assert c_before == (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
208+
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']]))
209+
assert c_after == (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
210+
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']]))
212211

213212
# Make sure that changing the key for a specific cycler
214213
# doesn't break things for a composed cycler
215214
c = (c1 + c2) * c3
216215
c4 = cycler('bar', c3)
217-
assert_equal(c, (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
218-
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']])))
219-
assert_equal(c3, cycler('foo', [['y', 'g', 'blue'], ['b', 'k']]))
216+
assert c == (cycler(c=[1, 2, 3], lw=['r', 'g', 'b']) *
217+
cycler('foo', [['y', 'g', 'blue'], ['b', 'k']]))
218+
assert c3 == cycler('foo', [['y', 'g', 'blue'], ['b', 'k']])
220219

221220

222221
def test_keychange():
@@ -225,35 +224,35 @@ def test_keychange():
225224
c3 = cycler('ec', 'yk')
226225

227226
c3.change_key('ec', 'edgecolor')
228-
assert_equal(c3, cycler('edgecolor', c3))
227+
assert c3 == cycler('edgecolor', c3)
229228

230229
c = c1 + c2
231230
c.change_key('lw', 'linewidth')
232231
# Changing a key in one cycler should have no
233232
# impact in the original cycler.
234-
assert_equal(c2, cycler('lw', [1, 2, 3]))
235-
assert_equal(c, c1 + cycler('linewidth', c2))
233+
assert c2 == cycler('lw', [1, 2, 3])
234+
assert c == c1 + cycler('linewidth', c2)
236235

237236
c = (c1 + c2) * c3
238237
c.change_key('c', 'color')
239-
assert_equal(c1, cycler('c', 'rgb'))
240-
assert_equal(c, (cycler('color', c1) + c2) * c3)
238+
assert c1 == cycler('c', 'rgb')
239+
assert c == (cycler('color', c1) + c2) * c3
241240

242241
# Perfectly fine, it is a no-op
243242
c.change_key('color', 'color')
244-
assert_equal(c, (cycler('color', c1) + c2) * c3)
243+
assert c == (cycler('color', c1) + c2) * c3
245244

246245
# Can't change a key to one that is already in there
247-
assert_raises(ValueError, Cycler.change_key, c, 'color', 'lw')
246+
pytest.raises(ValueError, Cycler.change_key, c, 'color', 'lw')
248247
# Can't change a key you don't have
249-
assert_raises(KeyError, Cycler.change_key, c, 'c', 'foobar')
248+
pytest.raises(KeyError, Cycler.change_key, c, 'c', 'foobar')
250249

251250

252251
def _eq_test_helper(a, b, res):
253252
if res:
254-
assert_equal(a, b)
253+
assert a == b
255254
else:
256-
assert_not_equal(a, b)
255+
assert a != b
257256

258257

259258
def test_eq():
@@ -273,34 +272,34 @@ def test_eq():
273272

274273

275274
def test_cycler_exceptions():
276-
assert_raises(TypeError, cycler)
277-
assert_raises(TypeError, cycler, 'c', 'rgb', lw=range(3))
278-
assert_raises(TypeError, cycler, 'c')
279-
assert_raises(TypeError, cycler, 'c', 'rgb', 'lw', range(3))
275+
pytest.raises(TypeError, cycler)
276+
pytest.raises(TypeError, cycler, 'c', 'rgb', lw=range(3))
277+
pytest.raises(TypeError, cycler, 'c')
278+
pytest.raises(TypeError, cycler, 'c', 'rgb', 'lw', range(3))
280279

281280

282281
def test_starange_init():
283282
c = cycler('r', 'rgb')
284283
c2 = cycler('lw', range(3))
285284
cy = Cycler(list(c), list(c2), zip)
286-
assert_equal(cy, c + c2)
285+
assert cy == c + c2
287286

288287

289288
def test_concat():
290289
a = cycler('a', range(3))
291290
b = cycler('a', 'abc')
292291
for con, chn in zip(a.concat(b), chain(a, b)):
293-
assert_equal(con, chn)
292+
assert con == chn
294293

295294
for con, chn in zip(concat(a, b), chain(a, b)):
296-
assert_equal(con, chn)
295+
assert con == chn
297296

298297

299298
def test_concat_fail():
300299
a = cycler('a', range(3))
301300
b = cycler('b', range(3))
302-
assert_raises(ValueError, concat, a, b)
303-
assert_raises(ValueError, a.concat, b)
301+
pytest.raises(ValueError, concat, a, b)
302+
pytest.raises(ValueError, a.concat, b)
304303

305304

306305
def _by_key_helper(cy):
@@ -310,23 +309,22 @@ def _by_key_helper(cy):
310309
for k, v in sty.items():
311310
target[k].append(v)
312311

313-
assert_equal(res, target)
312+
assert res == target
314313

315314

316315
def test_by_key_add():
317316
input_dict = dict(c=list('rgb'), lw=[1, 2, 3])
318317
cy = cycler(c=input_dict['c']) + cycler(lw=input_dict['lw'])
319318
res = cy.by_key()
320-
assert_equal(res, input_dict)
319+
assert res == input_dict
321320
yield _by_key_helper, cy
322321

323322

324323
def test_by_key_mul():
325324
input_dict = dict(c=list('rg'), lw=[1, 2, 3])
326325
cy = cycler(c=input_dict['c']) * cycler(lw=input_dict['lw'])
327326
res = cy.by_key()
328-
assert_equal(input_dict['lw'] * len(input_dict['c']),
329-
res['lw'])
327+
assert input_dict['lw'] * len(input_dict['c']) == res['lw']
330328
yield _by_key_helper, cy
331329

332330

0 commit comments

Comments
 (0)