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