Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
run_tests Namespace Reference

Classes

class  TestCase
 
class  TestSuite
 

Functions

 main ()
 
 write_junit_xml (test_results)
 
 print_captured_output (test_results)
 
 safe_str (obj)
 
 run_tests (tests_dir)
 

Variables

 unichr = chr
 
str __author__ = "Tamas Gal"
 
str __credits__ = "Brian Beyer"
 
str __license__ = "MIT"
 
str __email__ = "tgal@km3net.de"
 
str __status__ = "Development"
 
 TESTS_DIR = sys.argv[1]
 
str JUNIT_XML = 'out/junit_{}.xml'.format(os.path.basename(TESTS_DIR))
 
str INFO = '\033[94m'
 
str OK = '\033[92m'
 
str FAIL = '\033[91m'
 
str RST = '\033[0m'
 
str BOLD = '\033[1m'
 

Detailed Description

A test runner for Jpp.
This script traverses the tests directory and reports a summary.

Function Documentation

◆ main()

run_tests.main ( )

Definition at line 50 of file run_tests.py.

50def main():
51 test_results = run_tests(TESTS_DIR)
52 n_tests = len(test_results)
53 n_failed_tests = sum(1 for r in test_results.values() if r[0] > 0)
54 total_time = sum(r[1] for r in test_results.values())
55
56 print("\n{}"
57 "Test summary\n"
58 "============\n{}"
59 "Total number of tests: {}\n{}"
60 "Failed tests: {}{}\n"
61 "Elapsed time: {:.1f}s\n".format(
62 INFO, RST, n_tests, BOLD + FAIL if n_failed_tests > 0 else OK,
63 n_failed_tests, RST, total_time))
64
65 write_junit_xml(test_results)
66
67 if n_failed_tests > 0:
68 print_captured_output(test_results)
69 exit(1)
70 else:
71 exit(0)
72
73
void print(const TH1 &h1, std::ostream &out)
Print histogram parameters.
int main(int argc, char *argv[])
Definition Main.cc:15

◆ write_junit_xml()

run_tests.write_junit_xml ( test_results)
Generate XML file according to JUnit specs

Definition at line 74 of file run_tests.py.

74def write_junit_xml(test_results):
75 """Generate XML file according to JUnit specs"""
76 test_cases = []
77 for test_script, (exit_code, t, stdout, stderr) in test_results.items():
78 test_case = TestCase(test_script,
79 elapsed_sec=t,
80 stdout=stdout,
81 stderr=stderr)
82 if exit_code > 0:
83 test_case.add_error_info('non-zero exit-code: %d' % exit_code)
84 test_cases.append(test_case)
85 test_suite = TestSuite("Jpp Test Suite", test_cases)
86 with open(JUNIT_XML, 'w') as f:
87 TestSuite.to_file(f, [test_suite])
88
89

◆ print_captured_output()

run_tests.print_captured_output ( test_results)
Prints the STDOUT and STDERR of failing test scripts

Definition at line 90 of file run_tests.py.

90def print_captured_output(test_results):
91 """Prints the STDOUT and STDERR of failing test scripts"""
92 print("{}"
93 "Captured output of failing tests\n"
94 "================================\n{}".format(INFO, RST))
95 for test_script, (exit_code, t, stdout, stderr) in test_results.items():
96 if exit_code > 0:
97 print("{}\n{}\n".format(test_script, len(test_script) * '-'))
98 print('{}stdout:{}\n{}\n{}stderr:{}\n{}'.format(
99 OK + BOLD, RST, stdout, FAIL + BOLD, RST, stderr))
100
101

◆ safe_str()

run_tests.safe_str ( obj)

Definition at line 102 of file run_tests.py.

102def safe_str(obj):
103 return obj.decode('utf-8').encode('ascii', 'ignore').decode('ascii')
104
105

◆ run_tests()

run_tests.run_tests ( tests_dir)
Runs each script in the tests directory and returns the results.

Parameters
----------
tests_dir: str
  The path to the test dir, containing the test scripts (`*.sh`).

Returns
-------
dict: key = script path, value = (exit_code, elapsed_time, stdout, stderr)

Definition at line 106 of file run_tests.py.

106def run_tests(tests_dir):
107 """Runs each script in the tests directory and returns the results.
108
109 Parameters
110 ----------
111 tests_dir: str
112 The path to the test dir, containing the test scripts (`*.sh`).
113
114 Returns
115 -------
116 dict: key = script path, value = (exit_code, elapsed_time, stdout, stderr)
117
118 """
119 test_results = {}
120
121 for subdir in sorted(glob(join(tests_dir, '*'))):
122 component_group = basename(subdir)
123 print("\n{}{}\n{}{}".format(INFO, component_group,
124 len(component_group) * '=', RST))
125 for test_script in sorted(glob(join(subdir, '*.sh')) + glob(join(subdir, '*.csh'))):
126 print("+ {}".format(test_script), end=' => ')
127 sys.stdout.flush()
128 start_time = time()
129 proc = Popen(test_script, stdout=PIPE, stderr=PIPE)
130 out, err = [safe_str(t) for t in proc.communicate()]
131 exit_code = proc.wait()
132 delta_t = time() - start_time
133 test_results[test_script] = (exit_code, delta_t, out, err)
134 print(" ({:.2f} s) ".format(delta_t), end='')
135 sys.stdout.flush()
136 if exit_code > 0:
137 print("{}FAILED (exit code {}){}".format(FAIL, exit_code, RST))
138 sys.stdout.flush()
139 else:
140 print("{}OK{}".format(OK, RST))
141 sys.stdout.flush()
142
143 return test_results
144
145

Variable Documentation

◆ unichr

run_tests.unichr = chr

Definition at line 29 of file run_tests.py.

◆ __author__

str run_tests.__author__ = "Tamas Gal"
private

Definition at line 31 of file run_tests.py.

◆ __credits__

str run_tests.__credits__ = "Brian Beyer"
private

Definition at line 32 of file run_tests.py.

◆ __license__

str run_tests.__license__ = "MIT"
private

Definition at line 33 of file run_tests.py.

◆ __email__

str run_tests.__email__ = "tgal@km3net.de"
private

Definition at line 34 of file run_tests.py.

◆ __status__

str run_tests.__status__ = "Development"
private

Definition at line 35 of file run_tests.py.

◆ TESTS_DIR

run_tests.TESTS_DIR = sys.argv[1]

Definition at line 37 of file run_tests.py.

◆ JUNIT_XML

str run_tests.JUNIT_XML = 'out/junit_{}.xml'.format(os.path.basename(TESTS_DIR))

Definition at line 38 of file run_tests.py.

◆ INFO

run_tests.INFO = '\033[94m'

Definition at line 41 of file run_tests.py.

◆ OK

run_tests.OK = '\033[92m'

Definition at line 42 of file run_tests.py.

◆ FAIL

run_tests.FAIL = '\033[91m'

Definition at line 43 of file run_tests.py.

◆ RST

run_tests.RST = '\033[0m'

Definition at line 44 of file run_tests.py.

◆ BOLD

run_tests.BOLD = '\033[1m'

Definition at line 45 of file run_tests.py.