Skip to content

Commit

Permalink
Expand on the ability to include the same test in two suites (#165)
Browse files Browse the repository at this point in the history
This is to expand the ability of the user to create their own "define_test()" to allow non-default behavior for creating tests within the suite. The goal for this was to add the same test class to two different suites and run them with different configurations.

Add documentation about expected use of the define_test() macro

run tools/format.sh

Co-authored-by: Daniel Wagner-Hall <[email protected]>
  • Loading branch information
tjoneslo and illicitonion authored Mar 16, 2023
1 parent a9509d8 commit dda039c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 13 deletions.
18 changes: 13 additions & 5 deletions java/private/create_jvm_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ _RUNNERS = [
"junit5",
]

_LIBRARY_ATTRS = [
"data",
"javacopts",
"plugins",
"resources",
]

def create_jvm_test_suite(
name,
srcs,
test_suffixes,
package,
library_attributes,
define_library,
define_test,
library_attributes = _LIBRARY_ATTRS,
runner = "junit4",
deps = None,
runtime_deps = [],
Expand Down Expand Up @@ -50,10 +57,11 @@ def create_jvm_test_suite(
calculated from the bazel package.
library_attributes: Attributes to pass to `define_library`.
define_library: A function that creates a `*_library` target.
define_test: A function that creates a `*_test` target.
define_test: A function that creates a `*_test` target and returns the name of the created target.
(See java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/suite_tags for example use)
runner: The junit runner to use. Either "junit4" or "junit5".
deps: The list of dependencies to use when compiling.
runtime_deps: The list of runtime deps to use when compiling.
runtime_deps: The list of runtime deps to use when running tests.
tags: Tags to use for generated targets.
size: Bazel test size
"""
Expand Down Expand Up @@ -92,10 +100,9 @@ def create_jvm_test_suite(
for src in test_srcs:
suffix = src.rfind(".")
test_name = src[:suffix]
tests.append(test_name)
test_class = get_class_name(package, src, package_prefixes)

define_test(
test_name = define_test(
name = test_name,
size = size,
srcs = [src],
Expand All @@ -106,6 +113,7 @@ def create_jvm_test_suite(
visibility = ["//visibility:private"],
**kwargs
)
tests.append(test_name)

native.test_suite(
name = name,
Expand Down
10 changes: 2 additions & 8 deletions java/private/java_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ load(":junit5.bzl", "java_junit5_test")
# The key thing that this file adds is the ability to specify a
# suite of java tests by just globbing the test files.

_LIBRARY_ATTRS = [
"data",
"javacopts",
"plugins",
"resources",
]

def _define_library(name, **kwargs):
java_library(
name = name,
Expand All @@ -25,6 +18,7 @@ def _define_junit4_test(name, **kwargs):
name = name,
**kwargs
)
return name

def _define_junit5_test(name, **kwargs):
java_junit5_test(
Expand All @@ -33,6 +27,7 @@ def _define_junit5_test(name, **kwargs):
exclude_engines = kwargs.pop("exclude_engines", None),
**kwargs
)
return name

# Note: the keys in this match the keys in `create_jvm_test_suite.bzl`'s
# `_RUNNERS` constant
Expand Down Expand Up @@ -83,7 +78,6 @@ def java_test_suite(
srcs = srcs,
test_suffixes = test_suffixes,
package = package,
library_attributes = _LIBRARY_ATTRS,
define_library = _define_library,
# Default to bazel's default test runner if we don't know what people want
define_test = _TEST_GENERATORS.get(runner, _define_junit4_test),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "junit5_deps")
load(":java_test_suite.bzl", "java_test_suite")

java_test_suite(
name = "Junit5TagsTest_only_never",
size = "small",
srcs = ["Junit5TagsTest.java"],
exclude_tags = ["Never"],
deps = [
artifact("org.junit.jupiter:junit-jupiter-api", "contrib_rules_jvm_tests"),
] + junit5_deps("contrib_rules_jvm_tests"),
)

java_test_suite(
name = "Junit5TagsTest_no_sometimes",
size = "small",
srcs = ["Junit5TagsTest.java"],
exclude_tags = [
"Never",
"Sometimes",
],
deps = [
artifact("org.junit.jupiter:junit-jupiter-api", "contrib_rules_jvm_tests"),
] + junit5_deps("contrib_rules_jvm_tests"),
)

java_test_suite(
name = "Junit5TagsTest_only_always",
size = "small",
srcs = ["Junit5TagsTest.java"],
include_tags = ["Always"],
deps = [
artifact("org.junit.jupiter:junit-jupiter-api", "contrib_rules_jvm_tests"),
] + junit5_deps("contrib_rules_jvm_tests"),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5.suite_tags;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class Junit5TagsTest {

@Test
@Tag("Always")
public void alwaysRun() {
assertTrue(true);
}

@Test
@Tag("Never")
public void neverRun() {
assertTrue(false);
}

@Test
@Tag("Sometimes")
public void runSometimes() {
// exclude shouldn't run
if (System.getProperty("JUNIT5_EXCLUDE_TAGS", "").contains("Sometimes")) {
assertTrue(false, "Should have skippend this test");
} else if (System.getProperty("JUNIT5_INCLUDE_TAGS", "").contains("Sometimes")) {
assertTrue(true, "Positive ask to run this test");
} else {
assertTrue(true, "Not specifically filtered, but run anyway");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("//java:defs.bzl", "java_junit5_test", "java_library", test_suite = "create_jvm_test_suite")

def _define_junit5_test(name, **kwargs):
duplicate_test_name = kwargs.pop("duplicate_test_name", None)

test_name = "%s-%s" % (duplicate_test_name, name) if duplicate_test_name else name
java_junit5_test(
name = test_name,
**kwargs
)

return test_name

def _define_library(name, **kwargs):
java_library(
name = name,
**kwargs
)

def java_test_suite(
name,
runner = "junit5",
test_suffixes = ["Test.java"],
package = None,
**kwargs):
test_suite(
name,
test_suffixes = test_suffixes,
package = package,
define_test = _define_junit5_test,
define_library = _define_library,
runner = runner,
duplicate_test_name = name,
**kwargs
)

0 comments on commit dda039c

Please sign in to comment.