[Scipy-svn] r3794 - in branches/testing_cleanup/scipy: io io/tests testing
scipy-svn@scip...
scipy-svn@scip...
Mon Jan 7 18:00:16 CST 2008
Author: matthew.brett@gmail.com
Date: 2008-01-07 18:00:03 -0600 (Mon, 07 Jan 2008)
New Revision: 3794
Added:
branches/testing_cleanup/scipy/testing/nosetester.py
branches/testing_cleanup/scipy/testing/nulltester.py
branches/testing_cleanup/scipy/testing/pkgtester.py
Removed:
branches/testing_cleanup/scipy/io/tests/test_datasource.py
Modified:
branches/testing_cleanup/scipy/io/__init__.py
branches/testing_cleanup/scipy/testing/__init__.py
Log:
First pass at module level nose tests
Modified: branches/testing_cleanup/scipy/io/__init__.py
===================================================================
--- branches/testing_cleanup/scipy/io/__init__.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/io/__init__.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -84,5 +84,6 @@
from mmio import mminfo, mmread, mmwrite
__all__ = filter(lambda s:not s.startswith('_'),dir())
-from scipy.testing.moduletest import test_func
-test = test_func
+from scipy.testing.pkgtester import Tester
+t = Tester()
+test = Tester().test
Deleted: branches/testing_cleanup/scipy/io/tests/test_datasource.py
===================================================================
--- branches/testing_cleanup/scipy/io/tests/test_datasource.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/io/tests/test_datasource.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -1,251 +0,0 @@
-import os
-import sys
-import struct
-import bz2
-import gzip
-from tempfile import mkdtemp, mkstemp, NamedTemporaryFile
-from shutil import rmtree
-from urlparse import urlparse
-
-from scipy.testing import *
-
-# HACK: import the src datasource
-# Until datasource is robust enough to include in scipy.io package
-sys.path.insert(0, os.path.abspath('..'))
-import scipy.io.datasource as datasource
-del sys.path[0]
-
-def urlopen_stub(url, data=None):
- '''Stub to replace urlopen for testing.'''
- if url == valid_httpurl():
- tmpfile = NamedTemporaryFile(prefix='urltmp_')
- return tmpfile
- else:
- raise datasource.URLError('Name or service not known')
-
-# Rebind urlopen during testing. For a 'real' test, uncomment the rebinding
-# below.
-datasource.urlopen = urlopen_stub
-
-# A valid website for more robust testing
-http_path = 'http://www.google.com/'
-http_file = 'index.html'
-
-http_fakepath = 'http://fake.abc.web/site/'
-http_fakefile = 'fake.txt'
-
-magic_line = 'three is the magic number'
-
-
-# Utility functions used by many TestCases
-def valid_textfile(filedir):
- # Generate and return a valid temporary file.
- fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir, text=True)
- os.close(fd)
- return path
-
-def invalid_textfile(filedir):
- # Generate and return an invalid filename.
- fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir)
- os.close(fd)
- os.remove(path)
- return path
-
-def valid_httpurl():
- return http_path+http_file
-
-def invalid_httpurl():
- return http_fakepath+http_fakefile
-
-def valid_baseurl():
- return http_path
-
-def invalid_baseurl():
- return http_fakepath
-
-def valid_httpfile():
- return http_file
-
-def invalid_httpfile():
- return http_fakefile
-
-class TestDataSourceOpen(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
- self.ds = datasource.DataSource(self.tmpdir)
-
- def tearDown(self):
- rmtree(self.tmpdir)
- del self.ds
-
- def test_ValidHTTP(self):
- assert self.ds.open(valid_httpurl())
-
- def test_InvalidHTTP(self):
- self.assertRaises(IOError, self.ds.open, invalid_httpurl())
-
- def test_ValidFile(self):
- local_file = valid_textfile(self.tmpdir)
- assert self.ds.open(local_file)
-
- def test_InvalidFile(self):
- invalid_file = invalid_textfile(self.tmpdir)
- self.assertRaises(IOError, self.ds.open, invalid_file)
-
- def test_ValidGzipFile(self):
- # Test datasource's internal file_opener for Gzip files.
- filepath = os.path.join(self.tmpdir, 'foobar.txt.gz')
- fp = gzip.open(filepath, 'w')
- fp.write(magic_line)
- fp.close()
- fp = self.ds.open(filepath)
- result = fp.readline()
- fp.close()
- self.assertEqual(magic_line, result)
-
- def test_ValidBz2File(self):
- # Test datasource's internal file_opener for BZip2 files.
- filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2')
- fp = bz2.BZ2File(filepath, 'w')
- fp.write(magic_line)
- fp.close()
- fp = self.ds.open(filepath)
- result = fp.readline()
- fp.close()
- self.assertEqual(magic_line, result)
-
-
-class TestDataSourceExists(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
- self.ds = datasource.DataSource(self.tmpdir)
-
- def tearDown(self):
- rmtree(self.tmpdir)
- del self.ds
-
- def test_ValidHTTP(self):
- assert self.ds.exists(valid_httpurl())
-
- def test_InvalidHTTP(self):
- self.assertEqual(self.ds.exists(invalid_httpurl()), False)
-
- def test_ValidFile(self):
- # Test valid file in destpath
- tmpfile = valid_textfile(self.tmpdir)
- assert self.ds.exists(tmpfile)
- # Test valid local file not in destpath
- localdir = mkdtemp()
- tmpfile = valid_textfile(localdir)
- assert self.ds.exists(tmpfile)
- rmtree(localdir)
-
- def test_InvalidFile(self):
- tmpfile = invalid_textfile(self.tmpdir)
- self.assertEqual(self.ds.exists(tmpfile), False)
-
-
-class TestDataSourceAbspath(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
- self.ds = datasource.DataSource(self.tmpdir)
-
- def tearDown(self):
- rmtree(self.tmpdir)
- del self.ds
-
- def test_ValidHTTP(self):
- scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
- local_path = os.path.join(self.tmpdir, netloc, upath.strip(os.sep))
- self.assertEqual(local_path, self.ds.abspath(valid_httpurl()))
-
- def test_ValidFile(self):
- tmpfile = valid_textfile(self.tmpdir)
- tmpfilename = os.path.split(tmpfile)[-1]
- # Test with filename only
- self.assertEqual(tmpfile, self.ds.abspath(os.path.split(tmpfile)[-1]))
- # Test filename with complete path
- self.assertEqual(tmpfile, self.ds.abspath(tmpfile))
-
- def test_InvalidHTTP(self):
- scheme, netloc, upath, pms, qry, frg = urlparse(invalid_httpurl())
- invalidhttp = os.path.join(self.tmpdir, netloc, upath.strip(os.sep))
- self.assertNotEqual(invalidhttp, self.ds.abspath(valid_httpurl()))
-
- def test_InvalidFile(self):
- invalidfile = valid_textfile(self.tmpdir)
- tmpfile = valid_textfile(self.tmpdir)
- tmpfilename = os.path.split(tmpfile)[-1]
- # Test with filename only
- self.assertNotEqual(invalidfile, self.ds.abspath(tmpfilename))
- # Test filename with complete path
- self.assertNotEqual(invalidfile, self.ds.abspath(tmpfile))
-
-
-class TestRespositoryAbspath(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
- self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
-
- def tearDown(self):
- rmtree(self.tmpdir)
- del self.repos
-
- def test_ValidHTTP(self):
- scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
- local_path = os.path.join(self.repos._destpath, netloc, \
- upath.strip(os.sep))
- filepath = self.repos.abspath(valid_httpfile())
- self.assertEqual(local_path, filepath)
-
-
-class TestRepositoryExists(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
- self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
-
- def tearDown(self):
- rmtree(self.tmpdir)
- del self.repos
-
- def test_ValidFile(self):
- # Create local temp file
- tmpfile = valid_textfile(self.tmpdir)
- assert self.repos.exists(tmpfile)
-
- def test_InvalidFile(self):
- tmpfile = invalid_textfile(self.tmpdir)
- self.assertEqual(self.repos.exists(tmpfile), False)
-
- def test_RemoveHTTPFile(self):
- assert self.repos.exists(valid_httpurl())
-
- def test_CachedHTTPFile(self):
- localfile = valid_httpurl()
- # Create a locally cached temp file with an URL based
- # directory structure. This is similar to what Repository.open
- # would do.
- scheme, netloc, upath, pms, qry, frg = urlparse(localfile)
- local_path = os.path.join(self.repos._destpath, netloc)
- os.mkdir(local_path, 0700)
- tmpfile = valid_textfile(local_path)
- assert self.repos.exists(tmpfile)
-
-
-class TestOpenFunc(TestCase):
- def setUp(self):
- self.tmpdir = mkdtemp()
-
- def tearDown(self):
- rmtree(self.tmpdir)
-
- def test_DataSourceOpen(self):
- local_file = valid_textfile(self.tmpdir)
- # Test case where destpath is passed in
- assert datasource.open(local_file, destpath=self.tmpdir)
- # Test case where default destpath is used
- assert datasource.open(local_file)
-
-
-if __name__ == "__main__":
- unittest.main()
Modified: branches/testing_cleanup/scipy/testing/__init__.py
===================================================================
--- branches/testing_cleanup/scipy/testing/__init__.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/testing/__init__.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -8,7 +8,11 @@
import unittest
from unittest import TestCase
-import nose
+try:
+ import nose
+except ImportError:
+ print 'Need nose testing framework installed for scipy tests'
+ raise
import decorators as dec
from numpy.testing.utils import *
Added: branches/testing_cleanup/scipy/testing/nosetester.py
===================================================================
--- branches/testing_cleanup/scipy/testing/nosetester.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/testing/nosetester.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -0,0 +1,55 @@
+''' Nose tester object '''
+import os
+import sys
+
+import nose
+
+class NoseTester(object):
+ """ Scipy nose tests site manager.
+
+ Usage: NoseTester(<package>).test()
+
+ <package> is package path, package name or its module object.
+ """
+ def __init__(self, package=None):
+ if package is None:
+ f = sys._getframe(1)
+ package = f.f_locals.get('__file__',f.f_globals.get('__file__',None))
+ assert package is not None
+ self.package_path = os.path.dirname(package)
+ else:
+ self._process_package(package)
+
+ def _process_package(self, package):
+ ''' Package can be module, path, or package name '''
+ try:
+ pfile = package.__file__
+ except AttributeError:
+ pass
+ else:
+ self.package_path = os.path.dirname(pfile)
+ return
+ if os.path.isabs(package):
+ self.package_path = package
+ return
+ if package.find(os.path.sep) == -1:
+ # Try scipy package name
+ import scipy
+ scipy.pkgload(package)
+ try:
+ module = getattr(scipy, package)
+ except AttributeError:
+ pass
+ else:
+ self.package_path = os.path.dirname(module.__file__)
+ return
+ # Default to relative path
+ self.package_path = os.path.abspath(package)
+ return
+
+ def test(self, labels=None, *args, **kwargs):
+ if labels is None:
+ labels = []
+ argv = ['', self.package_path]
+ nose.run(argv=argv)
+
Added: branches/testing_cleanup/scipy/testing/nulltester.py
===================================================================
--- branches/testing_cleanup/scipy/testing/nulltester.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/testing/nulltester.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -0,0 +1,8 @@
+''' Null tester (when nose not importable '''
+
+class NullTester(object):
+ def __init__(self, *args, **kwargs):
+ pass
+ def test(self, labels=None, *args, **kwargs):
+ raise ImportError, 'Need nose testing on path for tests'
+
Added: branches/testing_cleanup/scipy/testing/pkgtester.py
===================================================================
--- branches/testing_cleanup/scipy/testing/pkgtester.py 2008-01-07 21:41:21 UTC (rev 3793)
+++ branches/testing_cleanup/scipy/testing/pkgtester.py 2008-01-08 00:00:03 UTC (rev 3794)
@@ -0,0 +1,7 @@
+''' Define test function for scipy package '''
+try:
+ import nose
+ from scipy.testing.nosetester import NoseTester as Tester
+except ImportError:
+ from scipy.testing.nulltester import NullTester as Tester
+
More information about the Scipy-svn
mailing list