Skip to content

Commit 68cf7e3

Browse files
committed
Switch to pytest from nose.
Nose was no longer supported and didn't work on python 3.10.
1 parent 4270665 commit 68cf7e3

10 files changed

+120
-137
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
- [NEW]: Support polars.DataFrame in Abstract API
1111

12+
- [NEW]: Switch tests to pytest from nose (no longer supported).
13+
1214
0.4.21
1315
======
1416

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ RUN python -m pip install -e . \
3939
ARG RUN_TESTS="1"
4040
RUN if [ "$RUN_TESTS" -ne "0" ]; then \
4141
python -m pip install -r requirements_test.txt \
42-
&& nosetests -w . ; \
42+
&& pytest . ; \
4343
else \
4444
echo "Skipping tests\n" ; \
4545
fi

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ perf:
2424
python3 tools/perf_talib.py
2525

2626
test: build
27-
LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} nosetests
27+
LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} pytest
2828

2929
sdist:
3030
python3 setup.py sdist --formats=gztar,zip

requirements_test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-r requirements.txt
22
pandas
3-
nose
3+
pytest
44
polars

talib/test_abstract.py

+42-55
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import numpy as np
2-
from nose.tools import (
3-
assert_equals,
4-
assert_true,
5-
assert_false,
6-
assert_raises,
7-
)
2+
import pytest
83

94
try:
105
from collections import OrderedDict
@@ -24,20 +19,20 @@ def test_pandas():
2419

2520
expected_k, expected_d = func.STOCH(ford_2012['high'], ford_2012['low'], ford_2012['close']) # 5, 3, 0, 3, 0
2621
output = abstract.Function('stoch', input_df).outputs
27-
assert_true(isinstance(output, pandas.DataFrame))
22+
assert isinstance(output, pandas.DataFrame)
2823
assert_np_arrays_equal(expected_k, output['slowk'])
2924
assert_np_arrays_equal(expected_d, output['slowd'])
3025
output = abstract.Function('stoch', input_dict).outputs
31-
assert_true(isinstance(output, list))
26+
assert isinstance(output, list)
3227
assert_np_arrays_equal(expected_k, output[0])
3328
assert_np_arrays_equal(expected_d, output[1])
3429

3530
expected = func.SMA(ford_2012['close'], 10)
3631
output = abstract.Function('sma', input_df, 10).outputs
37-
assert_true(isinstance(output, pandas.Series))
32+
assert isinstance(output, pandas.Series)
3833
assert_np_arrays_equal(expected, output)
3934
output = abstract.Function('sma', input_dict, 10).outputs
40-
assert_true(isinstance(output, np.ndarray))
35+
assert isinstance(output, np.ndarray)
4136
assert_np_arrays_equal(expected, output)
4237

4338

@@ -109,12 +104,13 @@ def test_doji_candle():
109104

110105
def test_MAVP():
111106
mavp = abstract.MAVP
112-
assert_raises(Exception, mavp.set_input_arrays, ford_2012)
107+
with pytest.raises(Exception):
108+
mavp.set_input_arrays(ford_2012)
113109
input_d = {}
114110
input_d['close'] = ford_2012['close']
115111
input_d['periods'] = np.arange(30)
116-
assert_true(mavp.set_input_arrays(input_d))
117-
assert_equals(mavp.input_arrays, input_d)
112+
assert mavp.set_input_arrays(input_d)
113+
assert mavp.input_arrays == input_d
118114

119115
def test_info():
120116
stochrsi = abstract.Function('STOCHRSI')
@@ -138,7 +134,7 @@ def test_info():
138134
('fastd_matype', 1),
139135
]),
140136
}
141-
assert_equals(expected, stochrsi.info)
137+
assert expected == stochrsi.info
142138

143139
expected = {
144140
'display_name': 'Bollinger Bands',
@@ -159,11 +155,11 @@ def test_info():
159155
('matype', 0),
160156
]),
161157
}
162-
assert_equals(expected, abstract.Function('BBANDS').info)
158+
assert expected == abstract.Function('BBANDS').info
163159

164160
def test_input_names():
165161
expected = OrderedDict([('price', 'close')])
166-
assert_equals(expected, abstract.Function('MAMA').input_names)
162+
assert expected == abstract.Function('MAMA').input_names
167163

168164
# test setting input_names
169165
obv = abstract.Function('OBV')
@@ -172,16 +168,17 @@ def test_input_names():
172168
('prices', ['volume']),
173169
])
174170
obv.input_names = expected
175-
assert_equals(obv.input_names, expected)
171+
assert obv.input_names == expected
176172

177173
obv.input_names = {
178174
'price': 'open',
179175
'prices': ['volume'],
180176
}
181-
assert_equals(obv.input_names, expected)
177+
assert obv.input_names == expected
182178

183179
def test_input_arrays():
184180
mama = abstract.Function('MAMA')
181+
185182
# test default setting
186183
expected = {
187184
'open': None,
@@ -190,33 +187,35 @@ def test_input_arrays():
190187
'close': None,
191188
'volume': None,
192189
}
193-
assert_equals(expected, mama.get_input_arrays())
190+
assert expected == mama.get_input_arrays()
191+
194192
# test setting/getting input_arrays
195-
assert_true(mama.set_input_arrays(ford_2012))
196-
assert_equals(mama.get_input_arrays(), ford_2012)
197-
assert_raises(Exception,
198-
mama.set_input_arrays, {'hello': 'fail', 'world': 'bye'})
193+
assert mama.set_input_arrays(ford_2012)
194+
assert mama.get_input_arrays() == ford_2012
195+
with pytest.raises(Exception):
196+
mama.set_input_arrays({'hello': 'fail', 'world': 'bye'})
199197

200198
# test only required keys are needed
201199
willr = abstract.Function('WILLR')
202200
reqd = willr.input_names['prices']
203201
input_d = dict([(key, ford_2012[key]) for key in reqd])
204-
assert_true(willr.set_input_arrays(input_d))
205-
assert_equals(willr.input_arrays, input_d)
202+
assert willr.set_input_arrays(input_d)
203+
assert willr.input_arrays == input_d
206204

207205
# test extraneous keys are ignored
208206
input_d['extra_stuffs'] = 'you should never see me'
209207
input_d['date'] = np.random.rand(100)
210-
assert_true(willr.set_input_arrays(input_d))
208+
assert willr.set_input_arrays(input_d)
211209

212210
# test missing keys get detected
213211
input_d['open'] = ford_2012['open']
214212
input_d.pop('close')
215-
assert_raises(Exception, willr.set_input_arrays, input_d)
213+
with pytest.raises(Exception):
214+
willr.set_input_arrays(input_d)
216215

217216
# test changing input_names on the Function
218217
willr.input_names = {'prices': ['high', 'low', 'open']}
219-
assert_true(willr.set_input_arrays(input_d))
218+
assert willr.set_input_arrays(input_d)
220219

221220
def test_parameters():
222221
stoch = abstract.Function('STOCH')
@@ -227,34 +226,34 @@ def test_parameters():
227226
('slowd_period', 3),
228227
('slowd_matype', 0),
229228
])
230-
assert_equals(expected, stoch.parameters)
229+
assert expected == stoch.parameters
231230

232231
stoch.parameters = {'fastk_period': 10}
233232
expected['fastk_period'] = 10
234-
assert_equals(expected, stoch.parameters)
233+
assert expected == stoch.parameters
235234

236235
stoch.parameters = {'slowk_period': 8, 'slowd_period': 5}
237236
expected['slowk_period'] = 8
238237
expected['slowd_period'] = 5
239-
assert_equals(expected, stoch.parameters)
238+
assert expected == stoch.parameters
240239

241240
stoch.parameters = {'slowd_matype': talib.MA_Type.T3}
242241
expected['slowd_matype'] = 8
243-
assert_equals(expected, stoch.parameters)
242+
assert expected == stoch.parameters
244243

245244
stoch.parameters = {
246245
'slowk_matype': talib.MA_Type.WMA,
247246
'slowd_matype': talib.MA_Type.EMA,
248247
}
249248
expected['slowk_matype'] = 2
250249
expected['slowd_matype'] = 1
251-
assert_equals(expected, stoch.parameters)
250+
assert expected == stoch.parameters
252251

253252
def test_lookback():
254-
assert_equals(abstract.Function('SMA', 10).lookback, 9)
253+
assert abstract.Function('SMA', 10).lookback == 9
255254

256255
stochrsi = abstract.Function('stochrsi', 20, 5, 3)
257-
assert_equals(stochrsi.lookback, 26)
256+
assert stochrsi.lookback == 26
258257

259258
def test_call_supports_same_signature_as_func_module():
260259
adx = abstract.Function('ADX')
@@ -263,36 +262,26 @@ def test_call_supports_same_signature_as_func_module():
263262
output = adx(ford_2012['open'], ford_2012['high'], ford_2012['low'])
264263
assert_np_arrays_equal(expected, output)
265264

266-
with assert_raises(TypeError) as e:
265+
with pytest.raises(TypeError):
267266
adx(ford_2012['open'], ford_2012['high'], ford_2012['low'], ford_2012['close'])
268-
assert 'Too many price arguments: expected 3 (open, high, low)' in str(
269-
e.exception.message)
270267

271-
with assert_raises(TypeError) as e:
268+
with pytest.raises(TypeError):
272269
adx(ford_2012['open'], ford_2012['high'])
273-
assert 'Not enough price arguments: expected 3 (open, high, low)' in str(
274-
e.exception.message)
275270

276271
def test_parameter_type_checking():
277272
sma = abstract.Function('SMA', timeperiod=10)
278273

279-
expected_error = 'Invalid parameter value for timeperiod (expected int, got float)'
280-
281-
with assert_raises(TypeError) as e:
274+
with pytest.raises(TypeError):
282275
sma(ford_2012['close'], 35.5)
283-
assert expected_error in str(e.exception.message)
284276

285-
with assert_raises(TypeError) as e:
277+
with pytest.raises(TypeError):
286278
abstract.Function('SMA', timeperiod=35.5)
287-
assert expected_error in str(e.exception.message)
288279

289-
with assert_raises(TypeError) as e:
280+
with pytest.raises(TypeError):
290281
sma.parameters = {'timeperiod': 35.5}
291-
assert expected_error in str(e.exception.message)
292282

293-
with assert_raises(TypeError) as e:
283+
with pytest.raises(TypeError):
294284
sma.set_parameters(timeperiod=35.5)
295-
assert expected_error in str(e.exception.message)
296285

297286
def test_call_doesnt_cache_parameters():
298287
sma = abstract.Function('SMA', timeperiod=10)
@@ -310,10 +299,8 @@ def test_call_doesnt_cache_parameters():
310299
assert_np_arrays_equal(expected, output)
311300

312301
def test_call_without_arguments():
313-
with assert_raises(TypeError) as e:
302+
with pytest.raises(TypeError):
314303
abstract.Function('SMA')()
315-
assert 'Not enough price arguments' in str(e.exception.message)
316304

317-
with assert_raises(TypeError) as e:
305+
with pytest.raises(TypeError):
318306
abstract.Function('SMA')(10)
319-
assert 'Not enough price arguments' in str(e.exception.message)

talib/test_data.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import numpy as np
55

6-
from nose.tools import assert_equal, assert_not_equal, assert_true
7-
86
ford_2012_dates = np.asarray([ 20120103, 20120104, 20120105, 20120106, 20120109,
97
20120110, 20120111, 20120112, 20120113, 20120117, 20120118, 20120119,
108
20120120, 20120123, 20120124, 20120125, 20120126, 20120127, 20120130,
@@ -217,9 +215,9 @@
217215
def assert_np_arrays_equal(expected, got):
218216
for i, value in enumerate(expected):
219217
if np.isnan(value):
220-
assert_true(np.isnan(got[i]))
218+
assert np.isnan(got[i])
221219
else:
222-
assert_equal(value, got[i])
220+
assert value == got[i]
223221

224222
def assert_np_arrays_not_equal(expected, got):
225223
''' Verifies expected and got have the same number of leading nan fields,
@@ -229,11 +227,11 @@ def assert_np_arrays_not_equal(expected, got):
229227
equals = []
230228
for i, value in enumerate(expected):
231229
if np.isnan(value):
232-
assert_true(np.isnan(got[i]))
230+
assert np.isnan(got[i])
233231
nans.append(value)
234232
else:
235233
try:
236-
assert_not_equal(value, got[i])
234+
assert value != got[i]
237235
except AssertionError:
238236
equals.append(got[i])
239237
if len(equals) == len(expected[len(nans):]):

0 commit comments

Comments
 (0)