File: run_unit_tests.py

package info (click to toggle)
lcm 1.3.1%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,788 kB
  • sloc: ansic: 16,184; java: 6,843; cs: 2,266; cpp: 1,594; python: 989; makefile: 348; xml: 252; sh: 59
file content (83 lines) | stat: -rwxr-xr-x 2,573 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
import os
import shutil
import subprocess
import sys
import unittest
import xml.etree.ElementTree

import xmlrunner

def run_gtest(name):
    xml_fname = name.replace(os.path.sep, "_")
    subprocess.check_call("%s --gtest_output=xml:lcm_unittest/%s.xml" % \
            (name, xml_fname), shell=True)

def run_tests():
    # Python unit tests
    print("Running Python unit tests")
    py_results_file = open(os.path.join("lcm_unittest", "lcm_python.xml"), "w")
    sys.path.append("python")
    tests = unittest.TestLoader().discover("python", "*_test.py")
    runner = xmlrunner.XMLTestRunner(py_results_file)
    runner.run(tests, "LCM_PYTHON")
    py_results_file.close()

    # C unit tests
    print("Running C unit tests")
    run_gtest(os.path.join("c", "memq_test"))
    run_gtest(os.path.join("c", "eventlog_test"))

    # C++ unit tests
    print("Running C++ unit tests")
    run_gtest(os.path.join("cpp", "memq_test"))

def summarize_results():
    # Parse and summarize unit test results
    num_failures = 0
    num_tests = 0
    num_errors = 0
    for bname in os.listdir("lcm_unittest"):
        fname = os.path.join("lcm_unittest", bname)
        tree = xml.etree.ElementTree.parse(fname)
        root = tree.getroot()
        for testsuite in root.iter("testsuite"):
            print("Test suite: %s" % testsuite.attrib["name"])
            suite_attrib = testsuite.attrib
            num_failures += int(suite_attrib["failures"])
            num_tests += int(suite_attrib["tests"])
            num_errors += int(suite_attrib["tests"])
            for testcase in testsuite.iter("testcase"):
                failures = list(testcase.iter("failure"))

                if not failures:
                    print("      OK   %s.%s" % (testsuite.attrib["name"],
                            testcase.attrib["name"]))
                else:
                    print(" FAIL      %s.%s" % (testsuite.attrib["name"],
                            testcase.attrib["name"]))
                    for failure in failures:
                        print(failure.text)
        print("")

    print("Tests passed: %d / %d" % (num_tests - num_failures, num_tests))

    if num_failures:
        print("Not all tests passed")
        return 1
    else:
        print("All unit tests passed")
        return 0

def main():
    # Setup output directory
    if os.path.exists("lcm_unittest"):
        shutil.rmtree("lcm_unittest")

    os.mkdir("lcm_unittest")

    run_tests()
    return summarize_results()

if __name__ == "__main__":
    sys.exit(main())