[35ec279] | 1 | #!/usr/bin/env python |
---|
| 2 | import os |
---|
| 3 | import subprocess |
---|
| 4 | import re |
---|
| 5 | import sys |
---|
[c155a16] | 6 | |
---|
| 7 | import logging |
---|
| 8 | import logging.config |
---|
| 9 | LOGGER_CONFIG_FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logging.ini') |
---|
[ebb0a55] | 10 | logging.config.fileConfig(LOGGER_CONFIG_FILE) |
---|
[463e7ffc] | 11 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 12 | |
---|
[35ec279] | 13 | try: |
---|
| 14 | import xmlrunner |
---|
| 15 | except: |
---|
[c155a16] | 16 | logger.error("xmlrunner needs to be installed to run these tests") |
---|
| 17 | logger.error("Try easy_install unittest-xml-reporting") |
---|
[35ec279] | 18 | sys.exit(1) |
---|
| 19 | |
---|
| 20 | # Check whether we have matplotlib installed |
---|
| 21 | HAS_MPL_WX = True |
---|
| 22 | try: |
---|
| 23 | import matplotlib |
---|
| 24 | import wx |
---|
| 25 | except: |
---|
| 26 | HAS_MPL_WX = False |
---|
| 27 | |
---|
| 28 | SKIPPED_DIRS = ["sasrealspace", "calculatorview"] |
---|
| 29 | if not HAS_MPL_WX: |
---|
| 30 | SKIPPED_DIRS.append("sasguiframe") |
---|
| 31 | |
---|
| 32 | #COMMAND_SEP = ';' |
---|
| 33 | #if os.name == 'nt': |
---|
| 34 | # COMMAND_SEP = '&' |
---|
| 35 | |
---|
| 36 | def run_tests(dirs=None, all=False): |
---|
| 37 | test_root = os.path.abspath(os.path.dirname(__file__)) |
---|
| 38 | run_one_py = os.path.join(test_root, 'run_one.py') |
---|
| 39 | passed = 0 |
---|
| 40 | failed = 0 |
---|
| 41 | n_tests = 0 |
---|
| 42 | n_errors = 0 |
---|
| 43 | n_failures = 0 |
---|
| 44 | |
---|
| 45 | for d in (dirs if dirs else os.listdir(test_root)): |
---|
| 46 | |
---|
| 47 | # Check for modules to be skipped |
---|
| 48 | if d in SKIPPED_DIRS: |
---|
| 49 | continue |
---|
| 50 | |
---|
[aaad4c95] | 51 | |
---|
[35ec279] | 52 | # Go through modules looking for unit tests |
---|
| 53 | module_dir = os.path.join(test_root, d, "test") |
---|
| 54 | if os.path.isdir(module_dir): |
---|
| 55 | for f in os.listdir(module_dir): |
---|
| 56 | file_path = os.path.join(module_dir,f) |
---|
| 57 | if os.path.isfile(file_path) and f.startswith("utest_") and f.endswith(".py"): |
---|
| 58 | module_name,_ = os.path.splitext(f) |
---|
| 59 | code = '"%s" %s %s'%(sys.executable, run_one_py, file_path) |
---|
| 60 | proc = subprocess.Popen(code, shell=True, stdout=subprocess.PIPE, stderr = subprocess.STDOUT) |
---|
| 61 | std_out, std_err = proc.communicate() |
---|
| 62 | #print std_out |
---|
| 63 | #sys.exit() |
---|
| 64 | has_failed = True |
---|
| 65 | m = re.search("Ran ([0-9]+) test", std_out) |
---|
| 66 | if m is not None: |
---|
| 67 | has_failed = False |
---|
| 68 | n_tests += int(m.group(1)) |
---|
| 69 | |
---|
| 70 | m = re.search("FAILED \(errors=([0-9]+)\)", std_out) |
---|
| 71 | if m is not None: |
---|
| 72 | has_failed = True |
---|
| 73 | n_errors += int(m.group(1)) |
---|
| 74 | |
---|
| 75 | m = re.search("FAILED \(failures=([0-9]+)\)", std_out) |
---|
| 76 | if m is not None: |
---|
| 77 | has_failed = True |
---|
| 78 | n_failures += int(m.group(1)) |
---|
| 79 | |
---|
| 80 | if has_failed: |
---|
| 81 | failed += 1 |
---|
| 82 | print "Result for %s (%s): FAILED" % (module_name, module_dir) |
---|
| 83 | print std_out |
---|
| 84 | else: |
---|
| 85 | passed += 1 |
---|
| 86 | print "Result for %s: SUCCESS" % module_name |
---|
| 87 | |
---|
| 88 | print "\n----------------------------------------------" |
---|
| 89 | if n_tests == 0: |
---|
| 90 | print "No tests." |
---|
| 91 | else: |
---|
| 92 | print "Results by test modules:" |
---|
| 93 | print " PASSED: %d" % passed |
---|
| 94 | ratio = 100.0*failed/(failed+passed) |
---|
| 95 | print " FAILED: %d (%.0f%%)" % (failed,ratio) |
---|
| 96 | |
---|
| 97 | print "Results by tests:" |
---|
| 98 | print " Tests run: %d" % n_tests |
---|
| 99 | print " Tests failed: %d" % n_failures |
---|
| 100 | print " Test errors: %d" % n_errors |
---|
| 101 | print "----------------------------------------------" |
---|
| 102 | |
---|
| 103 | return failed |
---|
| 104 | |
---|
| 105 | if __name__ == '__main__': |
---|
| 106 | all = (len(sys.argv) > 1 and sys.argv[1] == '-all') |
---|
| 107 | dirs = sys.argv[1:] if not all else sys.argv[2:] |
---|
| 108 | if run_tests(dirs=dirs, all=all)>0: |
---|
| 109 | sys.exit(1) |
---|
| 110 | |
---|