-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Connecting Bazel and JUnit5 by BazelJUnit5ConsoleLauncher #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connecting Bazel and JUnit5 by BazelJUnit5ConsoleLauncher #133
Conversation
|
@hcoona Could you help me review this PR? |
|
You're a hero @asinbow - Would love to see this get merged! |
What I see: What I'd like to see: (based on https://blog.jetbrains.com/idea/2016/08/using-junit-5-in-intellij-idea/) |
|
Interesting workaround! Is there a way in Bazel (excuse my ignorance) to reuse the |
Maybe we can move this into a standalone repo, and publish a jar to a maven repo? |
It seems Do you know how set the file path of XML report for JUnit5? I could not find it here: https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher-options |
Is |
|
@marcphilipp 👍 @razfriman I think I have already fixed this issue. BTW, the "Rerun Failed Tests" doesn't work at all, if we can fix this, it would boost our productivity hugely. |
| private static void fixXmlOutputFile(String xmlOutputFile) { | ||
| if (xmlOutputFile != null && !xmlOutputFile.isEmpty()) { | ||
| Path shouldPath = Paths.get(xmlOutputFile); | ||
| Path realPath = shouldPath.getParent().resolve(DEFAULT_JUNIT_JUPITER_XML_OUTPUT_FILE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you'd need a command to merge multiple files when there are multiple test engines... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you are right, I found this issue after pushing the commit as well 🤣. Will have a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add an option to the ConsoleLauncher to write a single xml report file to support this use case. Could you please open an issue for that on the junit5 repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some tests, and find that no matter how many tests in one running, it will only generate a single XML file named like TEST-*.xml, junit5 still does well, so I think we can simply move it to test.xml.
BTW, junit5 marks the XML report file as legacy: https://github.com/junit-team/junit5/blob/main/junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListener.java
I am not familiar with junit5, and feedback is welcome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found multiple XML files generated when I mixed both vintage tests and JUnit 5 Jupiter tests.
Would be great to have the option to generate a single file as @marcphilipp suggwsted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, interesting. Do you guys have a demo project for multiple XML files?
de01676 to
06c3780
Compare
|
@asinbow I'm really looking forward to see this merged. In the meantime I did a few tests and I still found some issues :( Checking JUnit 5 Guide I see that
In one of my Java modules I have 2 packages, so I need to pass this parameter more than once to work properly, so something like: would be needed. Also, due to constraints on some of my test cases, I have modules that run both Finally, but not last, while running in IntelliJ 2020.1 with Bazel plugin 2020.07.13.0.2 (latest available) I see a previous comment where you talk about it, but doesn't seem to be working for me. Also not sure if it's an issue with the Bazel plugin or the JUnit5 rule Thanks for the amazing work |
|
@ctasada Thanks for your feedback with these use cases.
There is a bit weird logic to erase
I am afraid in this case we have to merge these test reports together into a single file. |
|
I looked into merging multiple JUnit reports. If you're willing to pull in ant as a dependency ( private static File combineJunitReports(File reportDirectory, File[] junitReports) {
File output = new File(reportDirectory, "junit-combined.xml");
Project project = new Project();
project.setName(JUnit5Launcher.class.getName());
project.setBaseDir(reportDirectory);
XMLResultAggregator aggregator = new XMLResultAggregator();
for (File report : junitReports) {
FileSet fileSet = new FileSet();
fileSet.setProject(project);
fileSet.setFile(report);
aggregator.addFileSet(fileSet);
}
aggregator.setProject(project);
aggregator.setTodir(reportDirectory);
aggregator.setTofile(output.getName());
aggregator.execute();
return output;
}And modified the existing code in this pr like so: File outputFile;
if (files.length > 1) {
outputFile = combineJunitReports(dir.toFile(), files);
} else {
outputFile = files[0];
}
try {
Files.move(outputFile.toPath(), requiredPath);
} catch (IOException e) {
e.printStackTrace();
} |
|
@asinbow Great work on this and thanks for sharing! I gave this a try on our project and it mostly works. Some current shortcomings are:
I took a stab at fixing these:
I also tried to improve how You can find my changes here - asinbow#1 |
Parantheses -> Parentheses
Improve bazel JUnit5 integration with IntelliJ
| def java_junit5_test(name, srcs, test_package, deps = [], runtime_deps = [], **kwargs): | ||
| def java_junit5_test(name, srcs, test_package, main_class = None, deps = [], runtime_deps = [], **kwargs): | ||
| FILTER_KWARGS = [ | ||
| "main_class", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line can be removed if there's a main_class argument
| } | ||
|
|
||
| try { | ||
| Class.forName(testBridgeTestOnly); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--test_filter=package.class.method is also valid. In fact, my IDE, IntelliJ IDEA 2020.2, is automatically generating this form for Kotlin test code.
Probably need more logic to handle this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, when started from within IntelliJ IDEA, this doesn't seem to work for nested classes: bazelbuild/intellij#245
|
|
||
| try { | ||
| Document mergedXmlOutput = mergeTestResultXmls(files); | ||
| writeXmlOutputToFile(mergedXmlOutput, requiredPath.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is requiredPath.toString() equals to xmlOutputFile?
|
I found some issues with parsing the test filter, especially with Apologies for not having time to make a PR to this branch |
|
Hello! This looks amazing. Is it likely to get merged in? Thank you all for the work! |
|
@asinbow I think it would be good to provide this interim solution to Bazel users in a standalone repo and potentially publish the required code to Maven Central so there's less copy-and-paste involved. However, the JUnit team does not use Bazel and is therefore ill-suited to maintain that. I can imagine hosting it under our org but only if you or someone else that's interested make a commitment to maintain it. WDYT? |
|
Hey y'all I have been trying to adapt this into my project (I translated the code into Kotlin) and have noticed that Intellij uses the following format specifying a focused test for a single method: Just an FYI that there may be a missing a case to handle here. |
|
How does this PR relate to the new java_junit5_test rule? |
|
|
||
| if (testBridgeTestOnly.contains("#")) { | ||
| // There could be multiple classes in TESTBRIDGE_TEST_ONLY which are separated by $| | ||
| String[] splits = testBridgeTestOnly.split("\\$\\|"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#| is also a separator for classes without explicit methods, s.a. in: my.pkg.TestClass1#|my.pkg.TestClass2#
Anyone? I feel like I'm talking to myself here. 😕 |
We're looking at using java_junit5_test; so far I have a test project that works as expected, and I intend to roll it out to a larger monorepo in the near future. First impressions: it appears to be a workable solution, and it definitely integrates well with the Bazel IntelliJ plugin, providing individual test filtering. |
|
I've submitted #183 today. Would anyone like to review? |
|
Yes |
|
That is ok |









Overview
We are developers from Flexport. This PR is to open source how we solve this issue(bazelbuild/bazel#6681), and make Bazel support JUnit5.
A blog post talks about this: https://flexport.engineering/connecting-bazel-and-junit5-by-transforming-arguments-46440c6ea068
I hereby agree to the terms of the JUnit Contributor License Agreement.