|
1 | 1 | import os
|
| 2 | +from os.path import basename, join |
2 | 3 | import sys
|
3 | 4 | import unittest
|
4 |
| -if sys.version_info >= (3, ): |
5 |
| - from io import StringIO |
6 |
| -else: |
7 |
| - from cStringIO import StringIO |
8 |
| -import re |
9 | 5 | import glob
|
10 | 6 | import config
|
| 7 | +import importlib |
11 | 8 |
|
12 |
| -class IbmDbTest(unittest.TestCase): |
13 |
| - |
14 |
| - slash = '/' |
15 |
| - |
16 |
| - # Currently, this function serves no purpose. |
17 |
| - # However, this function has to be defined if the |
18 |
| - # unittest.TestCase is inherited in the given class. |
19 |
| - # For future reference, this function is called |
20 |
| - # everytime a test is ran in this testsuite. |
21 |
| - def setUp(self): |
22 |
| - pass |
| 9 | +# Test runner for ibm_db. Normally one could use something like: |
| 10 | +# |
| 11 | +# Run all in the tests directory: |
| 12 | +# python -m unittest discover -v -s tests |
| 13 | +# |
| 14 | +# Run all tests matching specified pattern or specific test: |
| 15 | +# python -m unittest discover -v -s tests -p 'test_*FetchAssocSelect*.py' |
| 16 | +# python -m unittest discover -v -s tests -p test_006_ConnPassingOpts.py |
| 17 | +# |
| 18 | +# However, for running all the tests, we need to ensure that test_000_PrepareDb |
| 19 | +# is run first to set up the database. Possibly this could be moved to a |
| 20 | +# separate task which is run by itself prior to running any tests, which |
| 21 | +# would simplify things a bit. |
23 | 22 |
|
24 |
| - # This function gets a list of all the test files located |
25 |
| - # in the current_dir/config.test_dir directory. |
26 |
| - def getFileList(self): |
27 |
| - if (sys.platform[0:3] == 'win'): |
28 |
| - self.slash = '\\' |
29 |
| - dir = config.test_dir + self.slash |
30 |
| - if (os.environ.get("SINGLE_PYTHON_TEST", None)): |
31 |
| - testfile = dir + os.environ.get("SINGLE_PYTHON_TEST", None) |
32 |
| - filelist = glob.glob(testfile) |
33 |
| - else: |
34 |
| - filelist = glob.glob(dir + "test_*.py") |
35 |
| - |
36 |
| - for i in range(0, len(filelist)): |
37 |
| - filelist[i] = filelist[i].replace('.py', '') |
38 |
| - filelist[i] = filelist[i].replace(config.test_dir + self.slash, '') |
39 |
| - filelist.sort() |
40 |
| - return filelist |
41 |
| - |
42 |
| - # This function is called to run all the tests. |
43 |
| - def runTest(self): |
44 |
| - filelist = self.getFileList(); |
| 23 | +# Override standard test-loading behavior |
| 24 | +def load_tests(loader, tests, pattern): |
45 | 25 | suite = unittest.TestSuite()
|
46 | 26 |
|
47 |
| - sys.path = [os.path.dirname(os.path.abspath(__file__)) + self.slash + config.test_dir] + sys.path[0:] |
| 27 | + test_glob = os.environ.get("SINGLE_PYTHON_TEST", "test_*.py") |
| 28 | + files = glob.glob(join(config.test_dir, test_glob)) |
| 29 | + tests = [ basename(_).replace('.py', '') for _ in files ] |
| 30 | + tests.sort() |
| 31 | + |
| 32 | + for test in tests: |
| 33 | + mod = importlib.import_module(test) |
| 34 | + suite.addTest(mod.IbmDbTestCase(test)) |
48 | 35 |
|
49 |
| - for i in range(0, len(filelist)): |
50 |
| - exec("import %s" % filelist[i]) |
51 |
| - testFuncName = filelist[i].replace(config.test_dir + self.slash, '') |
52 |
| - exec("suite.addTest(%s.IbmDbTestCase(testFuncName))" % filelist[i]) |
53 |
| - |
54 |
| - unittest.TextTestRunner(verbosity=2).run(suite) |
| 36 | + return suite |
55 | 37 |
|
56 |
| -obj = IbmDbTest() |
57 |
| -suite = obj.runTest() |
| 38 | +if __name__ == '__main__': |
| 39 | + sys.path.insert(0, config.test_dir) |
| 40 | + unittest.main(verbosity=2) |
0 commit comments