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.
This commit is contained in:
Jonathan Kamens
2015-04-30 14:24:30 -04:00
parent 7bb2677a9d
commit dcb8aa06af
+12 -33
View File
@@ -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))