shithub: aubio

Download patch

ref: ad4577673ad4d2cbaa8bb98b04801b59a891f060
parent: ecd370bcb3e781b1e8ce9a9a88031a6a23bf86aa
author: Paul Brossier <piem@piem.org>
date: Thu Nov 1 20:10:29 EDT 2018

[tests] update source tests

--- a/python/tests/test_source.py
+++ b/python/tests/test_source.py
@@ -1,19 +1,17 @@
 #! /usr/bin/env python
 
-from nose2 import main
-from nose2.tools import params
+
 from numpy.testing import TestCase, assert_equal
 from aubio import source
-from .utils import list_all_sounds
+from utils import list_all_sounds
+import unittest
+from _tools import parametrize, assert_raises, assert_equal, skipTest
 
-import warnings
-warnings.filterwarnings('ignore', category=UserWarning, append=True)
-
 list_of_sounds = list_all_sounds('sounds')
 samplerates = [0, 44100, 8000, 32000]
 hop_sizes = [512, 1024, 64]
 
-path = None
+default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
 
 all_params = []
 for soundfile in list_of_sounds:
@@ -21,17 +19,13 @@
         for samplerate in samplerates:
             all_params.append((hop_size, samplerate, soundfile))
 
+no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
 
-class aubio_source_test_case_base(TestCase):
+_debug = False
 
-    def setUp(self):
-        if not len(list_of_sounds):
-            self.skipTest('add some sound files in \'python/tests/sounds\'')
-        self.default_test_sound = list_of_sounds[0]
+class Test_aubio_source_test_case:
 
-class aubio_source_test_case(aubio_source_test_case_base):
-
-    @params(*list_of_sounds)
+    @parametrize('filename', list_of_sounds)
     def test_close_file(self, filename):
         samplerate = 0 # use native samplerate
         hop_size = 256
@@ -38,7 +32,7 @@
         f = source(filename, samplerate, hop_size)
         f.close()
 
-    @params(*list_of_sounds)
+    @parametrize('filename', list_of_sounds)
     def test_close_file_twice(self, filename):
         samplerate = 0 # use native samplerate
         hop_size = 256
@@ -46,7 +40,7 @@
         f.close()
         f.close()
 
-class aubio_source_read_test_case(aubio_source_test_case_base):
+class Test_aubio_source_read:
 
     def read_from_source(self, f):
         total_frames = 0
@@ -56,17 +50,21 @@
             if read < f.hop_size:
                 assert_equal(samples[read:], 0)
                 break
-        #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
-        #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
-        #print (result_str.format(*result_params))
+        if _debug:
+            result_str = "read {:.2f}s ({:d} frames"
+            result_str += " in {:d} blocks at {:d}Hz) from {:s}"
+            result_params = total_frames / float(f.samplerate), total_frames, \
+                    total_frames//f.hop_size, f.samplerate, f.uri
+            print (result_str.format(*result_params))
         return total_frames
 
-    @params(*all_params)
+    @parametrize('hop_size, samplerate, soundfile', all_params)
     def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
         try:
             f = source(soundfile, samplerate, hop_size)
         except RuntimeError as e:
-            self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
+            err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
+            skipTest(err_msg.format(hop_size, samplerate, str(e)))
         assert f.samplerate != 0
         read_frames = self.read_from_source(f)
         if 'f_' in soundfile and samplerate == 0:
@@ -75,21 +73,21 @@
             match_f = re.findall('([0-9]*)f_', soundfile)
             if len(match_f) == 1:
                 expected_frames = int(match_f[0])
-                self.assertEqual(expected_frames, read_frames)
+                assert_equal(expected_frames, read_frames)
 
-    @params(*list_of_sounds)
+    @parametrize('p', list_of_sounds)
     def test_samplerate_none(self, p):
         f = source(p)
         assert f.samplerate != 0
         self.read_from_source(f)
 
-    @params(*list_of_sounds)
+    @parametrize('p', list_of_sounds)
     def test_samplerate_0(self, p):
         f = source(p, 0)
         assert f.samplerate != 0
         self.read_from_source(f)
 
-    @params(*list_of_sounds)
+    @parametrize('p', list_of_sounds)
     def test_zero_hop_size(self, p):
         f = source(p, 0, 0)
         assert f.samplerate != 0
@@ -96,7 +94,7 @@
         assert f.hop_size != 0
         self.read_from_source(f)
 
-    @params(*list_of_sounds)
+    @parametrize('p', list_of_sounds)
     def test_seek_to_half(self, p):
         from random import randint
         f = source(p, 0, 0)
@@ -108,7 +106,7 @@
         b = self.read_from_source(f)
         assert a == b + c
 
-    @params(*list_of_sounds)
+    @parametrize('p', list_of_sounds)
     def test_duration(self, p):
         total_frames = 0
         f = source(p)
@@ -117,43 +115,44 @@
             _, read = f()
             total_frames += read
             if read < f.hop_size: break
-        self.assertEqual(duration, total_frames)
+        assert_equal (duration, total_frames)
 
 
-class aubio_source_test_wrong_params(TestCase):
+class Test_aubio_source_wrong_params:
 
     def test_wrong_file(self):
-        with self.assertRaises(RuntimeError):
+        with assert_raises(RuntimeError):
             source('path_to/unexisting file.mp3')
 
-class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
+@unittest.skipIf(default_test_sound is None, no_sounds_msg)
+class Test_aubio_source_wrong_params_with_file(TestCase):
 
     def test_wrong_samplerate(self):
-        with self.assertRaises(ValueError):
-            source(self.default_test_sound, -1)
+        with assert_raises(ValueError):
+            source(default_test_sound, -1)
 
     def test_wrong_hop_size(self):
-        with self.assertRaises(ValueError):
-            source(self.default_test_sound, 0, -1)
+        with assert_raises(ValueError):
+            source(default_test_sound, 0, -1)
 
     def test_wrong_channels(self):
-        with self.assertRaises(ValueError):
-            source(self.default_test_sound, 0, 0, -1)
+        with assert_raises(ValueError):
+            source(default_test_sound, 0, 0, -1)
 
     def test_wrong_seek(self):
-        f = source(self.default_test_sound)
-        with self.assertRaises(ValueError):
+        f = source(default_test_sound)
+        with assert_raises(ValueError):
             f.seek(-1)
 
     def test_wrong_seek_too_large(self):
-        f = source(self.default_test_sound)
+        f = source(default_test_sound)
         try:
-            with self.assertRaises(ValueError):
+            with assert_raises(ValueError):
                 f.seek(f.duration + f.samplerate * 10)
-        except AssertionError:
-            self.skipTest('seeking after end of stream failed raising ValueError')
+        except:
+            skipTest('seeking after end of stream failed raising ValueError')
 
-class aubio_source_readmulti_test_case(aubio_source_read_test_case):
+class Test_aubio_source_readmulti(Test_aubio_source_read):
 
     def read_from_source(self, f):
         total_frames = 0
@@ -163,15 +162,18 @@
             if read < f.hop_size:
                 assert_equal(samples[:,read:], 0)
                 break
-        #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
-        #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
-        #print (result_str.format(*result_params))
+        if _debug:
+            result_str = "read {:.2f}s ({:d} frames in {:d} channels"
+            result_str += " and {:d} blocks at {:d}Hz) from {:s}"
+            result_params = total_frames / float(f.samplerate), total_frames, \
+                    f.channels, int(total_frames/f.hop_size), \
+                    f.samplerate, f.uri
+            print (result_str.format(*result_params))
         return total_frames
 
-class aubio_source_with(aubio_source_test_case_base):
+class Test_aubio_source_with:
 
-    #@params(*list_of_sounds)
-    @params(*list_of_sounds)
+    @parametrize('filename', list_of_sounds)
     def test_read_from_mono(self, filename):
         total_frames = 0
         hop_size = 2048
@@ -185,4 +187,5 @@
             assert_equal(total_frames, input_source.duration)
 
 if __name__ == '__main__':
-    main()
+    import sys, pytest
+    pytest.main(sys.argv)