From dcb8aa06afb5d36ba493c1f4cc881776ab53c01f Mon Sep 17 00:00:00 2001 From: Jonathan Kamens Date: Thu, 30 Apr 2015 14:23:16 -0400 Subject: [PATCH] TST: Simplify test_examples and convert to unittest TestCase Also, remove use of os.chdir() in the test, which causes other tests to break by changing the current directory in the middle of the test run. --- tests/test_examples.py | 45 +++++++++++------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/tests/test_examples.py b/tests/test_examples.py index 1bc86ff1..39e0d4bd 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -18,46 +18,25 @@ # Disable plotting # -import matplotlib -import os -from os import path - -try: - from path import walk -except ImportError: - # Assume Python 3 - from os import walk - -import fnmatch +import glob import imp +import matplotlib +from nose_parameterized import parameterized +import os +from unittest import TestCase matplotlib.use('Agg') -def test_examples(): - os.chdir(example_dir()) - for fname in all_matching_files(example_dir(), '*.py'): - yield check_example, fname - - -def all_matching_files(d, pattern): - def addfiles(fls, dir, nfiles): - nfiles = fnmatch.filter(nfiles, pattern) - nfiles = [path.join(dir, f) for f in nfiles] - fls.extend(nfiles) - - files = [] - for dirpath, dirnames, filenames in walk(d): - addfiles(files, dirpath, filenames) - return files - - def example_dir(): import zipline - d = path.dirname(zipline.__file__) - return path.join(path.abspath(d), 'examples/') + d = os.path.dirname(zipline.__file__) + return os.path.join(os.path.abspath(d), 'examples') -def check_example(p): - imp.load_source('__main__', path.basename(p)) +class ExamplesTests(TestCase): + @parameterized.expand(((os.path.basename(f).replace('.', '_'), f) for f in + glob.glob(os.path.join(example_dir(), '*.py')))) + def test_example(self, name, example): + imp.load_source('__main__', os.path.basename(example), open(example))