[tests] add checks.py, selects testing module
authorPaul Brossier <piem@piem.org>
Thu, 1 Nov 2018 21:27:02 +0000 (22:27 +0100)
committerPaul Brossier <piem@piem.org>
Thu, 1 Nov 2018 21:27:02 +0000 (22:27 +0100)
python/tests/checks.py [new file with mode: 0644]

diff --git a/python/tests/checks.py b/python/tests/checks.py
new file mode 100644 (file)
index 0000000..271575d
--- /dev/null
@@ -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