[1810613] | 1 | import os |
---|
| 2 | import subprocess |
---|
| 3 | import re |
---|
[08dcf6c8] | 4 | import sys |
---|
[849fa92] | 5 | try: |
---|
| 6 | import xmlrunner |
---|
| 7 | except: |
---|
| 8 | print "xmlrunner needs to be installed to run these tests" |
---|
[c04af1b] | 9 | print "Try easy_install unittest-xml-reporting" |
---|
[1810613] | 10 | |
---|
[61f557b] | 11 | SKIPPED_DIRS = ["sansrealspace", "calculatorview"] |
---|
[295ddf5] | 12 | SANSVIEW_DIR = os.pardir |
---|
[111e52a] | 13 | COMMAND_SEP = ';' |
---|
| 14 | if os.name == 'nt': |
---|
| 15 | COMMAND_SEP = '&' |
---|
[1810613] | 16 | |
---|
| 17 | def run_tests(): |
---|
| 18 | passed = 0 |
---|
| 19 | failed = 0 |
---|
| 20 | n_tests = 0 |
---|
| 21 | n_errors = 0 |
---|
| 22 | n_failures = 0 |
---|
| 23 | |
---|
| 24 | for d in os.listdir(SANSVIEW_DIR): |
---|
| 25 | |
---|
| 26 | # Check for modules to be skipped |
---|
| 27 | if d in SKIPPED_DIRS: |
---|
| 28 | continue |
---|
| 29 | |
---|
| 30 | # Go through modules looking for unit tests |
---|
[295ddf5] | 31 | module_dir = os.path.join(os.getcwd(),SANSVIEW_DIR,d,"test") |
---|
[1810613] | 32 | if os.path.isdir(module_dir): |
---|
| 33 | for f in os.listdir(module_dir): |
---|
| 34 | file_path = os.path.join(module_dir,f) |
---|
| 35 | if os.path.isfile(file_path) and f.startswith("utest_") and f.endswith(".py"): |
---|
| 36 | module_name,_ = os.path.splitext(f) |
---|
[08dcf6c8] | 37 | code = "cd %s%s%s -c \"import sys;import xmlrunner;import unittest;sys.path.insert(0, '%s');" % (module_dir, COMMAND_SEP, sys.executable, module_dir) |
---|
[1810613] | 38 | code += "from %s import *;" % module_name |
---|
[6232a6f] | 39 | code += "unittest.main(testRunner=xmlrunner.XMLTestRunner(output='logs'))\"" |
---|
[1810613] | 40 | proc = subprocess.Popen(code, shell=True, stdout=subprocess.PIPE, stderr = subprocess.STDOUT) |
---|
| 41 | std_out, std_err = proc.communicate() |
---|
[6232a6f] | 42 | has_failed = True |
---|
[117a1d6] | 43 | m = re.search("Ran ([0-9]+) test", std_out) |
---|
[1810613] | 44 | if m is not None: |
---|
[6232a6f] | 45 | has_failed = False |
---|
[1810613] | 46 | n_tests += int(m.group(1)) |
---|
| 47 | |
---|
| 48 | m = re.search("FAILED \(errors=([0-9]+)\)", std_out) |
---|
| 49 | if m is not None: |
---|
| 50 | has_failed = True |
---|
| 51 | n_errors += int(m.group(1)) |
---|
| 52 | |
---|
| 53 | m = re.search("FAILED \(failures=([0-9]+)\)", std_out) |
---|
| 54 | if m is not None: |
---|
| 55 | has_failed = True |
---|
| 56 | n_failures += int(m.group(1)) |
---|
| 57 | |
---|
| 58 | if has_failed: |
---|
| 59 | failed += 1 |
---|
[117a1d6] | 60 | print "Result for %s (%s): FAILED" % (module_name, module_dir) |
---|
[1810613] | 61 | print std_out |
---|
| 62 | else: |
---|
| 63 | passed += 1 |
---|
| 64 | print "Result for %s: SUCCESS" % module_name |
---|
| 65 | |
---|
| 66 | print "\n----------------------------------------------" |
---|
| 67 | print "Results by test modules:" |
---|
| 68 | print " PASSED: %d" % passed |
---|
| 69 | ratio = 100.0*failed/(failed+passed) |
---|
| 70 | print " FAILED: %d (%2.2g%%)" % (failed,ratio) |
---|
| 71 | |
---|
| 72 | print "Results by tests:" |
---|
| 73 | print " Tests run: %d" % n_tests |
---|
| 74 | print " Tests failed: %d" % n_failures |
---|
| 75 | print " Test errors: %d" % n_errors |
---|
| 76 | print "----------------------------------------------" |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | if __name__ == '__main__': |
---|
| 80 | run_tests() |
---|