From: Paul Brossier Date: Thu, 1 Nov 2018 21:30:57 +0000 (+0100) Subject: [tests] use _tools in test_mfcc X-Git-Tag: 0.4.9~71^2~45 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=38592a6b5f751b65cbf6f8def3e0068045a54bf4;p=aubio.git [tests] use _tools in test_mfcc --- diff --git a/python/tests/_tools.py b/python/tests/_tools.py new file mode 100644 index 00000000..271575d3 --- /dev/null +++ b/python/tests/_tools.py @@ -0,0 +1,50 @@ +""" +This file imports test methods from different testing modules, in this +order: + + - if 'nose2' is found in the list of loaded module, use it + - otherwise, try using 'pytest' + - if that also fails, fallback to 'numpy.testing' +""" + +import sys + +_has_pytest = False +_has_nose2 = False + +# if nose2 has already been imported, use it +if 'nose2' in sys.modules: + from nose2.tools import params, such + def parametrize(argnames, argvalues): + return params(*argvalues) + assert_raises = such.helper.assertRaises + assert_warns = such.helper.assertWarns + skipTest = such.helper.skipTest + _has_nose2 = True + print ('using nose2') + +# otherwise, check if we have pytest +if not _has_nose2: + try: + import pytest + parametrize = pytest.mark.parametrize + _has_pytest = True + assert_raises = pytest.raises + assert_warns = pytest.warns + skipTest = pytest.skip + print ('using pytest') + except: + pass + +# otherwise fallback on numpy.testing +if not _has_pytest and not _has_nose2: + from numpy.testing import dec, assert_raises, assert_warns + from numpy.testing import SkipTest + parametrize = dec.parametrize + def skipTest(msg): + raise SkipTest(msg) + print ('using numpy') + +# always use numpy's assert_equal +import numpy +assert_equal = numpy.testing.assert_equal diff --git a/python/tests/checks.py b/python/tests/checks.py deleted file mode 100644 index 271575d3..00000000 --- a/python/tests/checks.py +++ /dev/null @@ -1,50 +0,0 @@ -""" -This file imports test methods from different testing modules, in this -order: - - - if 'nose2' is found in the list of loaded module, use it - - otherwise, try using 'pytest' - - if that also fails, fallback to 'numpy.testing' -""" - -import sys - -_has_pytest = False -_has_nose2 = False - -# if nose2 has already been imported, use it -if 'nose2' in sys.modules: - from nose2.tools import params, such - def parametrize(argnames, argvalues): - return params(*argvalues) - assert_raises = such.helper.assertRaises - assert_warns = such.helper.assertWarns - skipTest = such.helper.skipTest - _has_nose2 = True - print ('using nose2') - -# otherwise, check if we have pytest -if not _has_nose2: - try: - import pytest - parametrize = pytest.mark.parametrize - _has_pytest = True - assert_raises = pytest.raises - assert_warns = pytest.warns - skipTest = pytest.skip - print ('using pytest') - except: - pass - -# otherwise fallback on numpy.testing -if not _has_pytest and not _has_nose2: - from numpy.testing import dec, assert_raises, assert_warns - from numpy.testing import SkipTest - parametrize = dec.parametrize - def skipTest(msg): - raise SkipTest(msg) - print ('using numpy') - -# always use numpy's assert_equal -import numpy -assert_equal = numpy.testing.assert_equal diff --git a/python/tests/test_mfcc.py b/python/tests/test_mfcc.py index e7f3b186..c88d2fef 100755 --- a/python/tests/test_mfcc.py +++ b/python/tests/test_mfcc.py @@ -1,7 +1,6 @@ #! /usr/bin/env python -from nose2 import main -from nose2.tools import params +from ._tools import parametrize, assert_raises from numpy import random, count_nonzero from numpy.testing import TestCase from aubio import mfcc, cvec, float_type @@ -15,28 +14,21 @@ samplerate = 44100 new_params = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] new_deflts = [1024, 40, 13, 44100] -class aubio_mfcc(TestCase): +class Test_aubio_mfcc: - def setUp(self): - self.o = mfcc() + members_args = 'name' - def test_default_creation(self): - pass - - def test_delete(self): - del self.o - - @params(*new_params) + @parametrize(members_args, new_params) def test_read_only_member(self, name): - o = self.o - with self.assertRaises((TypeError, AttributeError)): + o = mfcc() + with assert_raises((TypeError, AttributeError)): setattr(o, name, 0) - @params(*zip(new_params, new_deflts)) + @parametrize('name, expected', zip(new_params, new_deflts)) def test_default_param(self, name, expected): """ test mfcc.{:s} = {:d} """.format(name, expected) - o = self.o - self.assertEqual( getattr(o, name), expected) + o = mfcc() + assert getattr(o, name) == expected class aubio_mfcc_wrong_params(TestCase): @@ -82,9 +74,9 @@ class aubio_mfcc_compute(TestCase): #print coeffs -class aubio_mfcc_all_parameters(TestCase): +class Test_aubio_mfcc_all_parameters: - @params( + run_values = [ (2048, 40, 13, 44100), (1024, 40, 13, 44100), (512, 40, 13, 44100), @@ -100,7 +92,10 @@ class aubio_mfcc_all_parameters(TestCase): #(1024, 30, 20, 44100), (1024, 40, 40, 44100), (1024, 40, 3, 44100), - ) + ] + run_args = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] + + @parametrize(run_args, run_values) def test_run_with_params(self, buf_size, n_filters, n_coeffs, samplerate): " check mfcc can run with reasonable parameters " o = mfcc(buf_size, n_filters, n_coeffs, samplerate) @@ -111,4 +106,5 @@ class aubio_mfcc_all_parameters(TestCase): #print coeffs if __name__ == '__main__': + from unittest import main main()