History

[tests] update source tests

Paul Brossier committed 8 years ago

Commit ad457761 parent ecd370b1 file · +55 −52Browse files

Showing the changes to python/tests (1 of 1 file). Show all

Unified · Split

python/tests/test_source.py+55−52
@@ -1,19 +1,17 @@
11 #! /usr/bin/env python
22
3−from nose2 import main
4−from nose2.tools import params
3+
54 from numpy.testing import TestCase, assert_equal
65 from aubio import source
7−from .utils import list_all_sounds
8−
9−import warnings
10−warnings.filterwarnings('ignore', category=UserWarning, append=True)
6+from utils import list_all_sounds
7+import unittest
8+from _tools import parametrize, assert_raises, assert_equal, skipTest
119
1210 list_of_sounds = list_all_sounds('sounds')
1311 samplerates = [0, 44100, 8000, 32000]
1412 hop_sizes = [512, 1024, 64]
1513
16−path = None
14+default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
1715
1816 all_params = []
1917 for soundfile in list_of_sounds:
@@ -21,24 +19,20 @@ for soundfile in list_of_sounds: · 1 unmodified line
2119 for samplerate in samplerates:
2220 all_params.append((hop_size, samplerate, soundfile))
2321
22+no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
2423
25−class aubio_source_test_case_base(TestCase):
26−
27− def setUp(self):
28− if not len(list_of_sounds):
29− self.skipTest('add some sound files in \'python/tests/sounds\'')
30− self.default_test_sound = list_of_sounds[0]
24+_debug = False
3125
32−class aubio_source_test_case(aubio_source_test_case_base):
26+class Test_aubio_source_test_case:
3327
34− @params(*list_of_sounds)
28+ @parametrize('filename', list_of_sounds)
3529 def test_close_file(self, filename):
3630 samplerate = 0 # use native samplerate
3731 hop_size = 256
3832 f = source(filename, samplerate, hop_size)
3933 f.close()
4034
41− @params(*list_of_sounds)
35+ @parametrize('filename', list_of_sounds)
4236 def test_close_file_twice(self, filename):
4337 samplerate = 0 # use native samplerate
4438 hop_size = 256
@@ -46,7 +40,7 @@ class aubio_source_test_case(aubio_source_test_case_base): · 1 unmodified line
4640 f.close()
4741 f.close()
4842
49−class aubio_source_read_test_case(aubio_source_test_case_base):
43+class Test_aubio_source_read:
5044
5145 def read_from_source(self, f):
5246 total_frames = 0
@@ -56,17 +50,21 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 3 unmodified lines
5650 if read < f.hop_size:
5751 assert_equal(samples[read:], 0)
5852 break
59− #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
60− #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
61− #print (result_str.format(*result_params))
53+ if _debug:
54+ result_str = "read {:.2f}s ({:d} frames"
55+ result_str += " in {:d} blocks at {:d}Hz) from {:s}"
56+ result_params = total_frames / float(f.samplerate), total_frames, \
57+ total_frames//f.hop_size, f.samplerate, f.uri
58+ print (result_str.format(*result_params))
6259 return total_frames
6360
64− @params(*all_params)
61+ @parametrize('hop_size, samplerate, soundfile', all_params)
6562 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
6663 try:
6764 f = source(soundfile, samplerate, hop_size)
6865 except RuntimeError as e:
69− self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
66+ err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
67+ skipTest(err_msg.format(hop_size, samplerate, str(e)))
7068 assert f.samplerate != 0
7169 read_frames = self.read_from_source(f)
7270 if 'f_' in soundfile and samplerate == 0:
@@ -75,28 +73,28 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 2 unmodified lines
7573 match_f = re.findall('([0-9]*)f_', soundfile)
7674 if len(match_f) == 1:
7775 expected_frames = int(match_f[0])
78− self.assertEqual(expected_frames, read_frames)
76+ assert_equal(expected_frames, read_frames)
7977
80− @params(*list_of_sounds)
78+ @parametrize('p', list_of_sounds)
8179 def test_samplerate_none(self, p):
8280 f = source(p)
8381 assert f.samplerate != 0
8482 self.read_from_source(f)
8583
86− @params(*list_of_sounds)
84+ @parametrize('p', list_of_sounds)
8785 def test_samplerate_0(self, p):
8886 f = source(p, 0)
8987 assert f.samplerate != 0
9088 self.read_from_source(f)
9189
92− @params(*list_of_sounds)
90+ @parametrize('p', list_of_sounds)
9391 def test_zero_hop_size(self, p):
9492 f = source(p, 0, 0)
9593 assert f.samplerate != 0
9694 assert f.hop_size != 0
9795 self.read_from_source(f)
9896
99− @params(*list_of_sounds)
97+ @parametrize('p', list_of_sounds)
10098 def test_seek_to_half(self, p):
10199 from random import randint
102100 f = source(p, 0, 0)
@@ -108,7 +106,7 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 5 unmodified lines
108106 b = self.read_from_source(f)
109107 assert a == b + c
110108
111− @params(*list_of_sounds)
109+ @parametrize('p', list_of_sounds)
112110 def test_duration(self, p):
113111 total_frames = 0
114112 f = source(p)
@@ -117,43 +115,44 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 2 unmodified lines
117115 _, read = f()
118116 total_frames += read
119117 if read < f.hop_size: break
120− self.assertEqual(duration, total_frames)
118+ assert_equal (duration, total_frames)
121119
122120
123−class aubio_source_test_wrong_params(TestCase):
121+class Test_aubio_source_wrong_params:
124122
125123 def test_wrong_file(self):
126− with self.assertRaises(RuntimeError):
124+ with assert_raises(RuntimeError):
127125 source('path_to/unexisting file.mp3')
128126
129−class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
127+@unittest.skipIf(default_test_sound is None, no_sounds_msg)
128+class Test_aubio_source_wrong_params_with_file(TestCase):
130129
131130 def test_wrong_samplerate(self):
132− with self.assertRaises(ValueError):
133− source(self.default_test_sound, -1)
131+ with assert_raises(ValueError):
132+ source(default_test_sound, -1)
134133
135134 def test_wrong_hop_size(self):
136− with self.assertRaises(ValueError):
137− source(self.default_test_sound, 0, -1)
135+ with assert_raises(ValueError):
136+ source(default_test_sound, 0, -1)
138137
139138 def test_wrong_channels(self):
140− with self.assertRaises(ValueError):
141− source(self.default_test_sound, 0, 0, -1)
139+ with assert_raises(ValueError):
140+ source(default_test_sound, 0, 0, -1)
142141
143142 def test_wrong_seek(self):
144− f = source(self.default_test_sound)
145− with self.assertRaises(ValueError):
143+ f = source(default_test_sound)
144+ with assert_raises(ValueError):
146145 f.seek(-1)
147146
148147 def test_wrong_seek_too_large(self):
149− f = source(self.default_test_sound)
148+ f = source(default_test_sound)
150149 try:
151− with self.assertRaises(ValueError):
150+ with assert_raises(ValueError):
152151 f.seek(f.duration + f.samplerate * 10)
153− except AssertionError:
154− self.skipTest('seeking after end of stream failed raising ValueError')
152+ except:
153+ skipTest('seeking after end of stream failed raising ValueError')
155154
156−class aubio_source_readmulti_test_case(aubio_source_read_test_case):
155+class Test_aubio_source_readmulti(Test_aubio_source_read):
157156
158157 def read_from_source(self, f):
159158 total_frames = 0
@@ -163,15 +162,18 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 3 unmodified lines
163162 if read < f.hop_size:
164163 assert_equal(samples[:,read:], 0)
165164 break
166− #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
167− #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
168− #print (result_str.format(*result_params))
165+ if _debug:
166+ result_str = "read {:.2f}s ({:d} frames in {:d} channels"
167+ result_str += " and {:d} blocks at {:d}Hz) from {:s}"
168+ result_params = total_frames / float(f.samplerate), total_frames, \
169+ f.channels, int(total_frames/f.hop_size), \
170+ f.samplerate, f.uri
171+ print (result_str.format(*result_params))
169172 return total_frames
170173
171−class aubio_source_with(aubio_source_test_case_base):
174+class Test_aubio_source_with:
172175
173− #@params(*list_of_sounds)
174− @params(*list_of_sounds)
176+ @parametrize('filename', list_of_sounds)
175177 def test_read_from_mono(self, filename):
176178 total_frames = 0
177179 hop_size = 2048
@@ -185,4 +187,5 @@ class aubio_source_with(aubio_source_test_case_base): · 7 unmodified lines
185187 assert_equal(total_frames, input_source.duration)
186188
187189 if __name__ == '__main__':
188− main()
190+ import sys, pytest
191+ pytest.main(sys.argv)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t