]> git.parisson.com Git - timeside.git/commitdiff
Doctest: Handle alias fixture for test data files
authorThomas Fillon <thomas@parisson.com>
Thu, 20 Mar 2014 17:42:43 +0000 (18:42 +0100)
committerThomas Fillon <thomas@parisson.com>
Thu, 20 Mar 2014 17:42:43 +0000 (18:42 +0100)
Add a way to define alias for test data files to preserve readability of the doctest e.g. 'wavfile = 'test/data/file.wav') but the executed code will use 'wavfile=/home/user/real_path/test/data/file.wav'.

tests/test_run_all_doctests.py
timeside/decoder/utils.py

index 8e5e6e1eb32c80e646ccfeaf7b300e2efb056e7f..2ca5f4f8a76bb2087ba6fbdf12ace201df80d23c 100755 (executable)
@@ -38,7 +38,13 @@ def load_tests(loader, tests, ignore):
                     onerror=lambda x: None)]
 
     for module in modules_list:
-        tests.addTests(doctest.DocTestSuite(module, test_finder=finder))
+        _tmp = __import__(module, fromlist=['DOCTEST_ALIAS'])
+        try:
+            DOCTEST_ALIAS = _tmp.DOCTEST_ALIAS
+        except AttributeError:
+            DOCTEST_ALIAS = {}
+        tests.addTests(doctest.DocTestSuite(module, extraglobs=DOCTEST_ALIAS,
+                                            test_finder=finder))
 
     return tests
 
index 50e9a719f03a7e8d6449d3ac1c0dff6eb0a3a7eb..cf9e21426c212ea8cc6e88697025f9ec4c72493b 100644 (file)
@@ -174,7 +174,8 @@ def sha1sum_file(filename):
     '''
     Return the secure hash digest with sha1 algorithm for a given file
 
-    >>> print sha1sum_file('../../tests/samples/guitar.wav')
+    >>> wav_file = '../../tests/samples/guitar.wav' # doctest: +SKIP
+    >>> print sha1sum_file(wav_file)
     08301c3f9a8d60926f31e253825cc74263e52ad1
     '''
     import hashlib
@@ -195,7 +196,8 @@ def sha1sum_url(url):
     >>> url = 'https://github.com/yomguy/timeside-samples/raw/master/samples/guitar.wav'
     >>> print sha1sum_url(url)
     08301c3f9a8d60926f31e253825cc74263e52ad1
-    >>> uri = get_uri('../../tests/samples/guitar.wav')
+    >>> wav_file = '../../tests/samples/guitar.wav' # doctest: +SKIP
+    >>> uri = get_uri(wav_file)
     >>> print sha1sum_url(uri)
     08301c3f9a8d60926f31e253825cc74263e52ad1
 
@@ -227,7 +229,11 @@ def sha1sum_numpy(np_array):
     import hashlib
     return hashlib.sha1(np_array.view(np.uint8)).hexdigest()
 
+import os
+DOCTEST_ALIAS = {'wav_file': os.path.join(os.path.dirname(__file__),
+                                           '../../tests/samples/guitar.wav')}
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
+
+    doctest.testmod(extraglobs=DOCTEST_ALIAS)