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