Skip to content

Commit

Permalink
tools: Add xmlrunner and use it for Python unittests (RobotLocomotion…
Browse files Browse the repository at this point in the history
…#14560)

* tools: Add xmlrunner and use it for Python unittests
  • Loading branch information
jwnimmer-tri authored Jan 25, 2021
1 parent 2290ae0 commit bb0a967
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
1 change: 0 additions & 1 deletion bindings/pydrake/test/math_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def check_equality(X_actual, X_expected_matrix):
self.assertEqual((X @ v).shape, (3,))
self.assertEqual((X @ v.reshape((3, 1))).shape, (3, 1))
self.assertEqual((X @ vs).shape, (3, 2))
print(help(RigidTransform.multiply))
# - Test vector multiplication.
R_AB = RotationMatrix([
[0., 1, 0],
Expand Down
9 changes: 8 additions & 1 deletion common/test_utilities/drake_py_unittest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import trace
import unittest
import warnings
import xmlrunner

try:
from pydrake.common.deprecation import DrakeDeprecationWarning
Expand Down Expand Up @@ -127,7 +128,13 @@ def run():
# warnings settings, exploting a loophole where it checks "if warnings
# is None" to check if the user passed a kwarg, but "if warning" to
# actually apply the user's kwarg.
unittest.main(module=test_name, argv=unittest_argv, warnings=False)
if "XML_OUTPUT_FILE" in os.environ:
with open(os.environ["XML_OUTPUT_FILE"], "wb") as output:
unittest.main(
module=test_name, argv=unittest_argv, warnings=False,
testRunner=xmlrunner.XMLTestRunner(output=output))
else:
unittest.main(module=test_name, argv=unittest_argv, warnings=False)

# Ensure deprecation warnings are always shown at least once.
warnings.simplefilter(args.deprecation_action, DeprecationWarning)
Expand Down
4 changes: 4 additions & 0 deletions tools/skylark/drake_py.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def drake_py_binary(

def drake_py_unittest(
name,
deps = None,
**kwargs):
"""Declares a `unittest`-based python test.
Expand All @@ -180,6 +181,9 @@ def drake_py_unittest(
srcs = srcs,
main = helper,
allow_import_unittest = True,
deps = (deps or []) + [
"@xmlrunner_py",
],
**kwargs
)

Expand Down
3 changes: 3 additions & 0 deletions tools/workspace/default.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ load("@drake//tools/workspace/uritemplate_py:repository.bzl", "uritemplate_py_re
load("@drake//tools/workspace/voxelized_geometry_tools:repository.bzl", "voxelized_geometry_tools_repository") # noqa
load("@drake//tools/workspace/vtk:repository.bzl", "vtk_repository")
load("@drake//tools/workspace/x11:repository.bzl", "x11_repository")
load("@drake//tools/workspace/xmlrunner_py:repository.bzl", "xmlrunner_py_repository") # noqa
load("@drake//tools/workspace/yaml_cpp:repository.bzl", "yaml_cpp_repository")
load("@drake//tools/workspace/zlib:repository.bzl", "zlib_repository")

Expand Down Expand Up @@ -263,6 +264,8 @@ def add_default_repositories(excludes = [], mirrors = DEFAULT_MIRRORS):
vtk_repository(name = "vtk", mirrors = mirrors)
if "x11" not in excludes:
x11_repository(name = "x11")
if "xmlrunner_py" not in excludes:
xmlrunner_py_repository(name = "xmlrunner_py", mirrors = mirrors)
if "yaml_cpp" not in excludes:
yaml_cpp_repository(name = "yaml_cpp")
if "zlib" not in excludes:
Expand Down
5 changes: 5 additions & 0 deletions tools/workspace/xmlrunner_py/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- python -*-

load("//tools/lint:lint.bzl", "add_lint_tests")

add_lint_tests()
15 changes: 15 additions & 0 deletions tools/workspace/xmlrunner_py/package.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- python -*-

load("@drake//tools/skylark:py.bzl", "py_library")

licenses(["notice"]) # BSD-2-Clause

package(
default_visibility = ["//visibility:public"],
)

py_library(
name = "xmlrunner_py",
srcs = glob(["xmlrunner/**"], allow_empty = False),
imports = ["."],
)
15 changes: 15 additions & 0 deletions tools/workspace/xmlrunner_py/repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- python -*-

load("@drake//tools/workspace:github.bzl", "github_archive")

def xmlrunner_py_repository(
name,
mirrors = None):
github_archive(
name = name,
repository = "xmlrunner/unittest-xml-reporting",
commit = "3.0.2",
sha256 = "dbe165386952ec5373d4db5b4ac0644b60b734f4b02b9e575b1d0dc873616ba4", # noqa
build_file = "@drake//tools/workspace/xmlrunner_py:package.BUILD.bazel", # noqa
mirrors = mirrors,
)

0 comments on commit bb0a967

Please sign in to comment.