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