Skip to content

Commit 36f70ab

Browse files
committed
Allow having YamlTestExtension include method name in test names
When publishing to JUnit xml style files there is no hierarchy, so some tools will just show the class name and description, making it hard to tell what tests were run, or failed. If YamlTestExtension is used in an environment where that is the case this can be used to have the output include the method name. For example: - showcasingTests(Embedded) - showcasingTests(MultiServer (Embedded then !current_version)) ... Instead of - YamlIntegrationTests - showcasingTests(Runner) - Embedded - MultiServer (Embedded then !current_version) I thought about making this configurable via system property, but I think having it configured by a constructor parameter aligns more with PRs FoundationDB#3252 and FoundationDB#3251
1 parent 2d5235a commit 36f70ab

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

yaml-tests/src/main/java/com/apple/foundationdb/relational/yamltests/YamlTestExtension.java

+33-11
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ public class YamlTestExtension implements TestTemplateInvocationContextProvider,
6666
private List<ExternalServer> servers;
6767
@Nullable
6868
private final String clusterFile;
69+
private final boolean includeMethodInDescriptions;
6970

7071
public YamlTestExtension() {
71-
this.clusterFile = null; // it will get it from the environment
72+
this(null, false);
7273
}
7374

74-
public YamlTestExtension(@Nullable final String clusterFile) {
75+
/**
76+
* Create a new extension with some configuration.
77+
* @param clusterFile a custom cluster file to use, or {@code null} to inherit it from the environment, namely
78+
* {@code FDB_CLUSTER_FILE}.
79+
* @param includeMethodInDescriptions Set this to {@code true} if publishing test results to something that cannot
80+
* handle complex test hierarchies. In the record layer we maintain the full hierarchy in the output, so this is not
81+
* necessary, but if integrating some other tools this might be necessary.
82+
*/
83+
public YamlTestExtension(@Nullable final String clusterFile, final boolean includeMethodInDescriptions) {
7584
this.clusterFile = clusterFile;
85+
this.includeMethodInDescriptions = includeMethodInDescriptions;
7686
}
7787

7888
@Override
@@ -184,34 +194,36 @@ public boolean supportsTestTemplate(final ExtensionContext context) {
184194
@Override
185195
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(final ExtensionContext context) {
186196
final var testClass = context.getRequiredTestClass();
197+
final var testMethod = context.getRequiredTestMethod();
187198
if (testClass.getAnnotation(MaintainYamlTestConfig.class) != null) {
188199
final var annotation = testClass.getAnnotation(MaintainYamlTestConfig.class);
189-
return provideInvocationContextsForMaintenance(annotation);
200+
return provideInvocationContextsForMaintenance(annotation, testMethod.getName());
190201
}
191-
final var testMethod = context.getRequiredTestMethod();
192202
if (testMethod.getAnnotation(ExcludeYamlTestConfig.class) != null) {
193203
// excluded tests are still included as configs so that they show up in the test run as skipped, rather than
194204
// just not being there. This may waste some resources if all the tests being run exclude a config that has
195205
// expensive @BeforeAll.
196206
final var annotation = testMethod.getAnnotation(ExcludeYamlTestConfig.class);
197207
return testConfigs
198208
.stream()
199-
.map(config -> new Context(config, annotation.reason(), annotation.value()));
209+
.map(config -> new Context(config, annotation.reason(), annotation.value(),
210+
includeMethodInDescriptions, testMethod.getName()));
200211
} else if (testMethod.getAnnotation(MaintainYamlTestConfig.class) != null) {
201212
final var annotation =
202213
testMethod.getAnnotation(MaintainYamlTestConfig.class);
203-
return provideInvocationContextsForMaintenance(annotation);
214+
return provideInvocationContextsForMaintenance(annotation, testMethod.getName());
204215
}
205216
return testConfigs
206217
.stream()
207-
.map(config -> new Context(config, "", null));
218+
.map(config -> new Context(config, "", null, includeMethodInDescriptions, testMethod.getName()));
208219
}
209220

210-
private Stream<TestTemplateInvocationContext> provideInvocationContextsForMaintenance(@Nonnull final MaintainYamlTestConfig annotation) {
221+
private Stream<TestTemplateInvocationContext> provideInvocationContextsForMaintenance(
222+
@Nonnull final MaintainYamlTestConfig annotation, @Nonnull final String methodName) {
211223
return maintainConfigs
212224
.stream()
213225
.map(config -> new Context(config, "maintenance not needed",
214-
Objects.requireNonNull(annotation.value())));
226+
Objects.requireNonNull(annotation.value()), includeMethodInDescriptions, methodName));
215227
}
216228

217229
/**
@@ -225,17 +237,27 @@ private static class Context implements TestTemplateInvocationContext {
225237
private final String excludedReason;
226238
@Nullable
227239
private final YamlTestConfigFilters configFilters;
240+
private final boolean includeMethodInDescriptions;
241+
@Nonnull
242+
private final String methodName;
228243

229244
public Context(@Nonnull final YamlTestConfig config, @Nonnull final String excludedReason,
230-
@Nullable final YamlTestConfigFilters configFilters) {
245+
@Nullable final YamlTestConfigFilters configFilters,
246+
final boolean includeMethodInDescriptions, @Nonnull final String methodName) {
231247
this.config = config;
232248
this.excludedReason = excludedReason;
233249
this.configFilters = configFilters;
250+
this.includeMethodInDescriptions = includeMethodInDescriptions;
251+
this.methodName = methodName;
234252
}
235253

236254
@Override
237255
public String getDisplayName(int invocationIndex) {
238-
return config.toString();
256+
if (includeMethodInDescriptions) {
257+
return methodName + "(" + config + ")";
258+
} else {
259+
return config.toString();
260+
}
239261
}
240262

241263
@Override

0 commit comments

Comments
 (0)