Skip to content

Commit c476a98

Browse files
authored
MAINT, TST: Remove nose imports and use pytest (#610)
* MAINT, TST: Remove nose imports and use pytest for testing * CI: Use pytest * MAINT: Remove unused imports * MAINT: Import error * ENH: Apply suggestions * STY: Fix pep8
1 parent c5a81d5 commit c476a98

File tree

7 files changed

+60
-119
lines changed

7 files changed

+60
-119
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
- name: Run Tests (pytest)
4747
run: |
4848
pytest quantecon/game_theory
49+
pytest quantecon/markov
4950
- name: Coveralls Parallel
5051
uses: AndreMiras/coveralls-python-action@develop
5152
if: runner.os == 'Linux'

quantecon/markov/tests/test_approximation.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
Tests for approximation.py file (i.e. tauchen)
33
44
"""
5-
import sys
6-
import unittest
75
import numpy as np
86
from quantecon.markov import tauchen, rouwenhorst
7+
from numpy.testing import assert_, assert_allclose
8+
99
#from quantecon.markov.approximation import rouwenhorst
1010

1111

12-
class TestTauchen(unittest.TestCase):
12+
class TestTauchen:
1313

14-
def setUp(self):
14+
def setup(self):
1515
self.rho, self.sigma_u = np.random.rand(2)
1616
self.n = np.random.random_integers(3, 25)
1717
self.m = np.random.random_integers(4)
@@ -29,33 +29,32 @@ def testStateCenter(self):
2929
for b in [0., 1., -1.]:
3030
mu = b / (1 - self.rho)
3131
mc = tauchen(self.rho, self.sigma_u, b, self.m, self.n)
32-
self.assertTrue(np.allclose(mu, np.mean(mc.state_values),
33-
atol=self.tol))
32+
assert_allclose(mu, np.mean(mc.state_values), atol=self.tol)
3433

3534
def testShape(self):
3635
i, j = self.P.shape
3736

38-
self.assertTrue(i == j)
37+
assert_(i == j)
3938

4039
def testDim(self):
4140
dim_x = self.x.ndim
4241
dim_P = self.P.ndim
4342

44-
self.assertTrue(dim_x == 1 and dim_P == 2)
43+
assert_(dim_x == 1 and dim_P == 2)
4544

4645
def test_transition_mat_row_sum_1(self):
47-
self.assertTrue(np.allclose(np.sum(self.P, axis=1), 1, atol=self.tol))
46+
assert_allclose(np.sum(self.P, axis=1), 1, atol=self.tol)
4847

4948
def test_positive_probs(self):
50-
self.assertTrue(np.all(self.P) > -self.tol)
49+
assert_(np.all(self.P > -self.tol))
5150

5251
def test_states_sum_0(self):
53-
self.assertTrue(abs(np.sum(self.x)) < self.tol)
52+
assert_(abs(np.sum(self.x)) < self.tol)
5453

5554

56-
class TestRouwenhorst(unittest.TestCase):
55+
class TestRouwenhorst:
5756

58-
def setUp(self):
57+
def setup(self):
5958
self.rho, self.sigma = np.random.uniform(0, 1, size=2)
6059
self.n = np.random.random_integers(3, 25)
6160
self.ybar = np.random.random_integers(0, 10)
@@ -71,22 +70,22 @@ def tearDown(self):
7170
def testShape(self):
7271
i, j = self.P.shape
7372

74-
self.assertTrue(i == j)
73+
assert_(i == j)
7574

7675
def testDim(self):
7776
dim_x = self.x.ndim
7877
dim_P = self.P.ndim
79-
self.assertTrue(dim_x == 1 and dim_P == 2)
78+
assert_(dim_x == 1 and dim_P == 2)
8079

8180
def test_transition_mat_row_sum_1(self):
82-
self.assertTrue(np.allclose(np.sum(self.P, axis=1), 1, atol=self.tol))
81+
assert_allclose(np.sum(self.P, axis=1), 1, atol=self.tol)
8382

8483
def test_positive_probs(self):
85-
self.assertTrue(np.all(self.P) > -self.tol)
84+
assert_(np.all(self.P > -self.tol))
8685

8786
def test_states_sum_0(self):
8887
tol = self.tol + self.n*(self.ybar/(1 - self.rho))
89-
self.assertTrue(abs(np.sum(self.x)) < tol)
88+
assert_(abs(np.sum(self.x)) < tol)
9089

9190
def test_control_case(self):
9291
n = 3; ybar = 1; sigma = 0.5; rho = 0.8;
@@ -97,11 +96,5 @@ def test_control_case(self):
9796
known_x = np.array([-psi+5.0, 5., psi+5.0])
9897
known_P = np.array(
9998
[[0.81, 0.18, 0.01], [0.09, 0.82, 0.09], [0.01, 0.18, 0.81]])
100-
self.assertTrue(sum(mc_rouwenhorst.x - known_x) <
101-
self.tol and sum(sum(mc_rouwenhorst.P - known_P)) < self.tol)
102-
103-
104-
if __name__ == '__main__':
105-
suite = unittest.TestLoader().loadTestsFromTestCase(
106-
[TestTauchen, TestRouwenhorst])
107-
unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
99+
assert_(np.sum(mc_rouwenhorst.x - known_x) < self.tol and
100+
np.sum(mc_rouwenhorst.P - known_P) < self.tol)

quantecon/markov/tests/test_core.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from scipy import sparse
1212
import itertools
1313
from numpy.testing import (
14-
assert_allclose, assert_array_equal, assert_array_less, assert_raises
14+
assert_allclose, assert_array_equal, assert_array_less, assert_raises,
15+
assert_
1516
)
16-
from nose.tools import eq_, ok_, raises
1717

1818
from quantecon.markov import (
1919
MarkovChain, mc_compute_stationary, mc_sample_path
@@ -27,7 +27,7 @@ def list_of_array_equal(s, t):
2727
s, t: lists of numpy.ndarrays
2828
2929
"""
30-
eq_(len(s), len(t))
30+
assert_(len(s) == len(t))
3131
all(assert_array_equal(x, y) for x, y in zip(s, t))
3232

3333

@@ -191,7 +191,7 @@ class Test_markovchain_stationary_distributions_KMRMarkovMatrix2():
191191
p = 1/3
192192
TOL = 1e-2
193193

194-
def setUp(self):
194+
def setup(self):
195195
""" Setup a KMRMarkovMatrix and Compute Stationary Values """
196196
self.P = KMR_Markov_matrix_sequential(self.N, self.p, self.epsilon)
197197
self.mc = MarkovChain(self.P)
@@ -315,7 +315,7 @@ def test_simulate_ergodicity():
315315

316316
x = mc.simulate(ts_length, init=init, num_reps=num_reps, random_state=seed)
317317
frequency_1 = x[:, -1].mean()
318-
ok_(np.abs(frequency_1 - stationary_dist[1]) < tol)
318+
assert_(np.abs(frequency_1 - stationary_dist[1]) < tol)
319319

320320

321321
def test_simulate_for_matrices_with_C_F_orders():
@@ -405,11 +405,11 @@ def test_mc_sample_path_lln():
405405

406406
frequency_1 = mc_sample_path(P, init=init, sample_size=sample_size,
407407
random_state=seed).mean()
408-
ok_(np.abs(frequency_1 - stationary_dist[1]) < tol)
408+
assert_(np.abs(frequency_1 - stationary_dist[1]) < tol)
409409

410410

411411
class TestMCStateValues:
412-
def setUp(self):
412+
def setup(self):
413413
state_values = [[0, 1], [2, 3], [4, 5]] # Pass python list
414414
self.state_values = np.array(state_values)
415415

@@ -519,29 +519,28 @@ def test_get_index():
519519
P = [[0.4, 0.6], [0.2, 0.8]]
520520
mc = MarkovChain(P)
521521

522-
eq_(mc.get_index(0), 0)
523-
eq_(mc.get_index(1), 1)
522+
assert_(mc.get_index(0) == 0)
523+
assert_(mc.get_index(1) == 1)
524524
assert_raises(ValueError, mc.get_index, 2)
525525
assert_array_equal(mc.get_index([1, 0]), [1, 0])
526526
assert_raises(ValueError, mc.get_index, [[1]])
527527

528528
mc.state_values = [1, 2]
529-
eq_(mc.get_index(1), 0)
530-
eq_(mc.get_index(2), 1)
529+
assert_(mc.get_index(1) == 0)
530+
assert_(mc.get_index(2) == 1)
531531
assert_raises(ValueError, mc.get_index, 0)
532532
assert_array_equal(mc.get_index([2, 1]), [1, 0])
533533
assert_raises(ValueError, mc.get_index, [[1]])
534534

535535
mc.state_values = [[1, 2], [3, 4]]
536-
eq_(mc.get_index([1, 2]), 0)
536+
assert_(mc.get_index([1, 2]) == 0)
537537
assert_raises(ValueError, mc.get_index, 1)
538538
assert_array_equal(mc.get_index([[3, 4], [1, 2]]), [1, 0])
539539

540540

541-
@raises(ValueError)
542541
def test_raises_value_error_non_2dim():
543542
"""Test with non 2dim input"""
544-
MarkovChain(np.array([0.4, 0.6]))
543+
assert_raises(ValueError, MarkovChain, np.array([0.4, 0.6]))
545544

546545

547546
def test_raises_value_error_non_sym():
@@ -581,13 +580,3 @@ def test_raises_non_homogeneous_state_values():
581580
P = [[0.4, 0.6], [0.2, 0.8]]
582581
state_values = [(0, 1), 2]
583582
assert_raises(ValueError, MarkovChain, P, state_values=state_values)
584-
585-
586-
if __name__ == '__main__':
587-
import sys
588-
import nose
589-
590-
argv = sys.argv[:]
591-
argv.append('--verbose')
592-
argv.append('--nocapture')
593-
nose.main(argv=argv, defaultTest=__file__)

quantecon/markov/tests/test_ddp.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"""
55
import numpy as np
66
import scipy.sparse as sparse
7-
from numpy.testing import assert_array_equal, assert_allclose, assert_raises
8-
from nose.tools import ok_
7+
from numpy.testing import (assert_array_equal, assert_allclose, assert_raises,
8+
assert_)
99

1010
from quantecon.markov import DiscreteDP, backward_induction
1111

1212

1313
class TestDiscreteDP:
14-
def setUp(self):
14+
def setup(self):
1515
# From Puterman 2005, Section 3.1
1616
beta = 0.95
1717

@@ -57,8 +57,8 @@ def test_value_iteration(self):
5757
epsilon=self.epsilon)
5858

5959
# Check v is an epsilon/2-approximation of v_star
60-
ok_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
61-
ok_(np.abs(res_init.v - self.v_star).max() < self.epsilon/2)
60+
assert_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
61+
assert_(np.abs(res_init.v - self.v_star).max() < self.epsilon/2)
6262

6363
# Check sigma == sigma_star
6464
assert_array_equal(res.sigma, self.sigma_star)
@@ -90,8 +90,8 @@ def test_modified_policy_iteration(self):
9090
epsilon=self.epsilon)
9191

9292
# Check v is an epsilon/2-approximation of v_star
93-
ok_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
94-
ok_(np.abs(res_init.v - self.v_star).max() < self.epsilon/2)
93+
assert_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
94+
assert_(np.abs(res_init.v - self.v_star).max() < self.epsilon/2)
9595

9696
# Check sigma == sigma_star
9797
assert_array_equal(res.sigma, self.sigma_star)
@@ -104,7 +104,7 @@ def test_modified_policy_iteration_k0(self):
104104
epsilon=self.epsilon, k=k)
105105

106106
# Check v is an epsilon/2-approximation of v_star
107-
ok_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
107+
assert_(np.abs(res.v - self.v_star).max() < self.epsilon/2)
108108

109109
# Check sigma == sigma_star
110110
assert_array_equal(res.sigma, self.sigma_star)
@@ -194,7 +194,7 @@ def test_ddp_sorting():
194194

195195

196196
class TestFiniteHorizon:
197-
def setUp(self):
197+
def setup(self):
198198
# From Puterman 2005, Section 3.2, Section 4.6.1
199199
# "single-product stochastic inventory control"
200200
s_indices = [0, 0, 0, 0, 1, 1, 1, 2, 2, 3]
@@ -338,13 +338,3 @@ def test_ddp_to_sa_and_to_product():
338338

339339
for k in ["v", "sigma", "num_iter"]:
340340
assert_allclose(sol1[k], sol2[k])
341-
342-
343-
if __name__ == '__main__':
344-
import sys
345-
import nose
346-
347-
argv = sys.argv[:]
348-
argv.append('--verbose')
349-
argv.append('--nocapture')
350-
nose.main(argv=argv, defaultTest=__file__)

quantecon/markov/tests/test_gth_solve.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
44
"""
55
import numpy as np
6-
from numpy.testing import assert_array_equal
7-
from nose.tools import eq_, ok_, raises
6+
from numpy.testing import (assert_array_equal, assert_raises, assert_,
7+
assert_allclose)
88

99
from quantecon.markov import gth_solve
1010

@@ -130,22 +130,22 @@ def __init__(self):
130130

131131
class StationaryDistSumOne(AddDescription):
132132
def __call__(self, x):
133-
ok_(np.allclose(sum(x), 1, atol=TOL))
133+
assert_allclose(np.sum(x), 1, atol=TOL)
134134

135135

136136
class StationaryDistNonnegative(AddDescription):
137137
def __call__(self, x):
138-
eq_(np.prod(x >= 0-TOL), 1)
138+
assert_(np.prod(x >= 0-TOL) == 1)
139139

140140

141141
class StationaryDistLeftEigenVec(AddDescription):
142142
def __call__(self, A, x):
143-
ok_(np.allclose(np.dot(x, A), x, atol=TOL))
143+
assert_allclose(np.dot(x, A), x, atol=TOL)
144144

145145

146146
class StationaryDistEqualToKnown(AddDescription):
147147
def __call__(self, y, x):
148-
ok_(np.allclose(y, x, atol=TOL))
148+
assert_allclose(y, x, atol=TOL)
149149

150150

151151
def test_matrices_with_C_F_orders():
@@ -171,23 +171,11 @@ def test_matrices_with_C_F_orders():
171171
assert_array_equal(computed_F, stationary_dist)
172172

173173

174-
@raises(ValueError)
175174
def test_raises_value_error_non_2dim():
176175
"""Test with non 2dim input"""
177-
gth_solve(np.array([0.4, 0.6]))
176+
assert_raises(ValueError, gth_solve, np.array([0.4, 0.6]))
178177

179178

180-
@raises(ValueError)
181179
def test_raises_value_error_non_square():
182180
"""Test with non square input"""
183-
gth_solve(np.array([[0.4, 0.6]]))
184-
185-
186-
if __name__ == '__main__':
187-
import sys
188-
import nose
189-
190-
argv = sys.argv[:]
191-
argv.append('--verbose')
192-
argv.append('--nocapture')
193-
nose.main(argv=argv, defaultTest=__file__)
181+
assert_raises(ValueError, gth_solve, np.array([[0.4, 0.6]]))

0 commit comments

Comments
 (0)