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