Showing the changes to python/tests (1 of 1 file). Show all
Unified · Split
| @@ -1,19 +1,17 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | −from nose2 import main | |
| 4 | −from nose2.tools import params | |
| 3 | + | |
| 5 | 4 | from numpy.testing import TestCase, assert_equal |
| 6 | 5 | 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 | |
| 11 | 9 | |
| 12 | 10 | list_of_sounds = list_all_sounds('sounds') |
| 13 | 11 | samplerates = [0, 44100, 8000, 32000] |
| 14 | 12 | hop_sizes = [512, 1024, 64] |
| 15 | 13 | |
| 16 | −path = None | |
| 14 | +default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None | |
| 17 | 15 | |
| 18 | 16 | all_params = [] |
| 19 | 17 | for soundfile in list_of_sounds: |
| @@ -21,24 +19,20 @@ for soundfile in list_of_sounds: · 1 unmodified line | ||
| 21 | 19 | for samplerate in samplerates: |
| 22 | 20 | all_params.append((hop_size, samplerate, soundfile)) |
| 23 | 21 | |
| 22 | +no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!" | |
| 24 | 23 | |
| 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 | |
| 31 | 25 | |
| 32 | −class aubio_source_test_case(aubio_source_test_case_base): | |
| 26 | +class Test_aubio_source_test_case: | |
| 33 | 27 | |
| 34 | − @params(*list_of_sounds) | |
| 28 | + @parametrize('filename', list_of_sounds) | |
| 35 | 29 | def test_close_file(self, filename): |
| 36 | 30 | samplerate = 0 # use native samplerate |
| 37 | 31 | hop_size = 256 |
| 38 | 32 | f = source(filename, samplerate, hop_size) |
| 39 | 33 | f.close() |
| 40 | 34 | |
| 41 | − @params(*list_of_sounds) | |
| 35 | + @parametrize('filename', list_of_sounds) | |
| 42 | 36 | def test_close_file_twice(self, filename): |
| 43 | 37 | samplerate = 0 # use native samplerate |
| 44 | 38 | hop_size = 256 |
| @@ -46,7 +40,7 @@ class aubio_source_test_case(aubio_source_test_case_base): · 1 unmodified line | ||
| 46 | 40 | f.close() |
| 47 | 41 | f.close() |
| 48 | 42 | |
| 49 | −class aubio_source_read_test_case(aubio_source_test_case_base): | |
| 43 | +class Test_aubio_source_read: | |
| 50 | 44 | |
| 51 | 45 | def read_from_source(self, f): |
| 52 | 46 | total_frames = 0 |
| @@ -56,17 +50,21 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 3 unmodified lines | ||
| 56 | 50 | if read < f.hop_size: |
| 57 | 51 | assert_equal(samples[read:], 0) |
| 58 | 52 | 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)) | |
| 62 | 59 | return total_frames |
| 63 | 60 | |
| 64 | − @params(*all_params) | |
| 61 | + @parametrize('hop_size, samplerate, soundfile', all_params) | |
| 65 | 62 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
| 66 | 63 | try: |
| 67 | 64 | f = source(soundfile, samplerate, hop_size) |
| 68 | 65 | 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))) | |
| 70 | 68 | assert f.samplerate != 0 |
| 71 | 69 | read_frames = self.read_from_source(f) |
| 72 | 70 | 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 | ||
| 75 | 73 | match_f = re.findall('([0-9]*)f_', soundfile) |
| 76 | 74 | if len(match_f) == 1: |
| 77 | 75 | expected_frames = int(match_f[0]) |
| 78 | − self.assertEqual(expected_frames, read_frames) | |
| 76 | + assert_equal(expected_frames, read_frames) | |
| 79 | 77 | |
| 80 | − @params(*list_of_sounds) | |
| 78 | + @parametrize('p', list_of_sounds) | |
| 81 | 79 | def test_samplerate_none(self, p): |
| 82 | 80 | f = source(p) |
| 83 | 81 | assert f.samplerate != 0 |
| 84 | 82 | self.read_from_source(f) |
| 85 | 83 | |
| 86 | − @params(*list_of_sounds) | |
| 84 | + @parametrize('p', list_of_sounds) | |
| 87 | 85 | def test_samplerate_0(self, p): |
| 88 | 86 | f = source(p, 0) |
| 89 | 87 | assert f.samplerate != 0 |
| 90 | 88 | self.read_from_source(f) |
| 91 | 89 | |
| 92 | − @params(*list_of_sounds) | |
| 90 | + @parametrize('p', list_of_sounds) | |
| 93 | 91 | def test_zero_hop_size(self, p): |
| 94 | 92 | f = source(p, 0, 0) |
| 95 | 93 | assert f.samplerate != 0 |
| 96 | 94 | assert f.hop_size != 0 |
| 97 | 95 | self.read_from_source(f) |
| 98 | 96 | |
| 99 | − @params(*list_of_sounds) | |
| 97 | + @parametrize('p', list_of_sounds) | |
| 100 | 98 | def test_seek_to_half(self, p): |
| 101 | 99 | from random import randint |
| 102 | 100 | f = source(p, 0, 0) |
| @@ -108,7 +106,7 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 5 unmodified lines | ||
| 108 | 106 | b = self.read_from_source(f) |
| 109 | 107 | assert a == b + c |
| 110 | 108 | |
| 111 | − @params(*list_of_sounds) | |
| 109 | + @parametrize('p', list_of_sounds) | |
| 112 | 110 | def test_duration(self, p): |
| 113 | 111 | total_frames = 0 |
| 114 | 112 | f = source(p) |
| @@ -117,43 +115,44 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 2 unmodified lines | ||
| 117 | 115 | _, read = f() |
| 118 | 116 | total_frames += read |
| 119 | 117 | if read < f.hop_size: break |
| 120 | − self.assertEqual(duration, total_frames) | |
| 118 | + assert_equal (duration, total_frames) | |
| 121 | 119 | |
| 122 | 120 | |
| 123 | −class aubio_source_test_wrong_params(TestCase): | |
| 121 | +class Test_aubio_source_wrong_params: | |
| 124 | 122 | |
| 125 | 123 | def test_wrong_file(self): |
| 126 | − with self.assertRaises(RuntimeError): | |
| 124 | + with assert_raises(RuntimeError): | |
| 127 | 125 | source('path_to/unexisting file.mp3') |
| 128 | 126 | |
| 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): | |
| 130 | 129 | |
| 131 | 130 | 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) | |
| 134 | 133 | |
| 135 | 134 | 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) | |
| 138 | 137 | |
| 139 | 138 | 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) | |
| 142 | 141 | |
| 143 | 142 | 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): | |
| 146 | 145 | f.seek(-1) |
| 147 | 146 | |
| 148 | 147 | def test_wrong_seek_too_large(self): |
| 149 | − f = source(self.default_test_sound) | |
| 148 | + f = source(default_test_sound) | |
| 150 | 149 | try: |
| 151 | − with self.assertRaises(ValueError): | |
| 150 | + with assert_raises(ValueError): | |
| 152 | 151 | 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') | |
| 155 | 154 | |
| 156 | −class aubio_source_readmulti_test_case(aubio_source_read_test_case): | |
| 155 | +class Test_aubio_source_readmulti(Test_aubio_source_read): | |
| 157 | 156 | |
| 158 | 157 | def read_from_source(self, f): |
| 159 | 158 | total_frames = 0 |
| @@ -163,15 +162,18 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 3 unmodified lines | ||
| 163 | 162 | if read < f.hop_size: |
| 164 | 163 | assert_equal(samples[:,read:], 0) |
| 165 | 164 | 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)) | |
| 169 | 172 | return total_frames |
| 170 | 173 | |
| 171 | −class aubio_source_with(aubio_source_test_case_base): | |
| 174 | +class Test_aubio_source_with: | |
| 172 | 175 | |
| 173 | − #@params(*list_of_sounds) | |
| 174 | − @params(*list_of_sounds) | |
| 176 | + @parametrize('filename', list_of_sounds) | |
| 175 | 177 | def test_read_from_mono(self, filename): |
| 176 | 178 | total_frames = 0 |
| 177 | 179 | hop_size = 2048 |
| @@ -185,4 +187,5 @@ class aubio_source_with(aubio_source_test_case_base): · 7 unmodified lines | ||
| 185 | 187 | assert_equal(total_frames, input_source.duration) |
| 186 | 188 | |
| 187 | 189 | if __name__ == '__main__': |
| 188 | − main() | |
| 190 | + import sys, pytest | |
| 191 | + pytest.main(sys.argv) | |