-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Support JUnit 4 RunListeners #1330
Comments
I'd like to explore the use cases in more depth. Here's a list of questions and concerns that came up in internal discussions
|
It's also worth noting that a very similar use case was submitted in #701 |
@w25r the problem is that currently there are no way to use custom reporting (for example - Allure Framework) with JUnit 4 and Gradle. And there are no way to get all the information about the test using TestListeners (they are kind of designed to only get list of the tests and their statuses). Allure can catch a much more such as steps, attachments, test parameters, custom groups, test fixtures etc. |
@baev I see that you've already contributed to an example using Allure with Gradle. I think that is a valid workaround (if not a better alternative) to customizing test behavior in a build script. Another possibility is performing these changes in a Gradle plugin that allows custom listeners to be added. That greatly reduces the risk, as only consumers of the plugin would be affected by the changes. If we still do want to move forward with a change to Gradle proper, the next step we need is a well thought out Design Spec that addresses the concerns outlined above. |
This requires to configure Runner for all the tests. Imagine to patch all custom runners (Parameterized, SpringRunner etc) to add the listener to tests. The problem is that we can not migrate test projects with thousands of tests from Maven to Gradle because of that. The other workaround is use AspectJ to catch execution of internal JUnit methods and add the listener in there. But this smells bad
What plugin do you have in mind? Do you mean to create a brand new plugin?
I can do all the work, this issue is kind of blocker for us. |
Yes, a brand new plugin would be perfect. If there doesn't seem to be a way to get what needs to be done in plugin form, then a design spec would be great. |
I already tried this but no luck. |
@baev Where you able to solve this issue? Where you looking to simply add an extra listener or override the OFTB Gradle one? I've come across a similar issue and I'd be interested to know how you have got on. |
I need to add one more JUnit listener. The only way I know is using the following aspect: @Aspect
public class AllureRunListenerAspect {
private static AllureRunListener allureRunListener = new AllureRunListener();
@Pointcut("execution(void org.junit.runners.ParentRunner.run(org.junit.runner.notification.RunNotifier))")
public void run() {
}
@Around("run()")
public void run(ProceedingJoinPoint pjp) {
Object[] args = pjp.getArgs();
RunNotifier notifier = (RunNotifier) args[0];
notifier.removeListener(allureRunListener);
notifier.addListener(allureRunListener);
notifier.fireTestRunStarted(Description.createSuiteDescription("Tests"));
try {
pjp.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
notifier.fireTestRunFinished(new Result());
}
} I tried to create a brand new Gradle Plugin that adds listener, but can't find the way to do it. |
Is there any chances to get this processed? |
+1 |
@kcooney @marcphilipp could you please help us with the issue? Currently there is no way to add a listener to JUnit4 in Gradle projects. Since we decided to not merge SPI support for listeners junit-team/junit4#1122 to JUnit 4 that is blocker for us |
@baev as stated before, we'd like to see a design spec that addresses the aforementioned issues before this can be addressed. How close did you get on the plugin side? If you push your repo, we could take a look at it and may be able to resolve your issues (or confirm that it can't be done from a plugin) |
The new JUnit Platform supports adding listeners via |
Nope, some our users can't migrate to JUnit 5 (banks, legacy projects etc)
I didn't find a way how to override logic in
I'm not familiar with your dev process. Can you link some articles about design specs etc? BTW I can do all the work but I need to be sure that this change gonna be merged some day. Also I don't know about your feature plans about JUnit 5 support in Gradle, how this will work together with JUnit4 etc. I pretty much sure that there are so many people that will use JUnit 4 for next few years and will not migrate to JUnit Platform, so It would be useful to have an ability to configure RunListeners anyway. |
Sorry for the late response. A few thoughts: First of all, the JUnit Secondly, I'm no longer comfortable with the idea of using an SPI interface to register listeners in JUnit. Doing so would allow any code in your classpath to add listeners, even if those listeners cause problems or are even hostile to your development environment. I could be convinced otherwise, but currently I'm thinking that if JUnit4 supported registering listeners via The Gradle config could be one way to opt-in, but then you would only get the behavior if you run the tests in Gradle. I'm confused about the concern about JUnit5. The users would not need to change their tests to use JUnit5. JUnit5 provides a newer API that IDEs and build tools can use to run JUnit4-style ("Vintage") and JUnit5-style ("Jupiter") tests (I believe JUnit3-style tests would run using the JUnit4 code, so those would be supported, too). I think Marc was suggesting that Gradle would eventually migrate to using the JUnit5 APIs to run JUnit tests, and you could take advantage of those APIs (which would also not require having your users upgrade their JUnit jars, since JUnit5 uses different maven packages). |
I agree, so all we need is produce extended XML report (steps, parameters, attachments, history, retries etc).
I got your point. The main idea of this conversation is that currently there no way to add listeners to JUnit using Gradle. And since Maven is kind of dead, our users start to migrate from Maven to Gradle. And they can't use Allure any more. That is the problem I am totally fine with using JUnit 5 platform inside Gradle to run the tests as long as users will not require to change the code of tests. The only thing is that it would be so nice to get the problem resolved as soon as possible. |
Sorry for the confusion, @baev. @raulgd (who opened this issue) indicated that he wanted to shutdown resources in the listener. If all Allure needs is a test run report, then |
As I mentioned before |
@baev I'm sure I'm missing something, but I still don't quite see what you need that |
Yep, we are using ThreadLocal to catch information about steps and attachments and reflection to get some metadata from test class/method (an example from annotations). |
I think it's an intriguing idea to try to leverage JUnit 5's If that doesn't work, design specs can be found here. We already discussed some of the use cases and concerns that should be addressed in the design. As for promising that it's going to be merged someday, well, that is impossible to guarantee. But if the Gradle team determines that it is a safe, well-thought, well-tested change, then we are likely to merge it. |
shall I create an separate PR with design docs or continue the work in #1725? |
A new one would probably be better, and we can reference this issue and the old PR. |
I believe the primary issue @baev refers to is the absence of It is quite common to use test annotations for reporting purposes (allure is a great example). Is there any chance we can get them into the TestDescriptor? |
Hey guys, any updates on this issue? |
I posted a potential solution in the Help/Discuss forum that could help JUnit 4 users who need to attach RunListener providers to their test suites. |
Thanks @sbabcoc, I'll try that out! |
@pelizza Let me know if you run into any issues. I've fixed all of the problem I encountered myself, but my scenarios are very limited. Also, I've tried to provide clear, complete documentation, but it's likely that more details would be helpful here and there. Feedback is welcomed! |
@oehme I think the information I posted will be helpful to many folks still using JUnit 4. The library I refer to enables timeout management, automatic retry of failed tests, and event notifications in Gradle project tests. Where is the best place to publicize this information? |
The forum or a blog post, so people will find it when they google for it. |
@sbabcoc I attempted to utilize this solution in my project and it is not working. I am not seeing any mention of the RunListener being picked up as the execution goes through it's processing. Any assistance would be welcome. |
@cah-daniel-adams01 The current release is |
I can report a working RunListener using JUnitFoundation as reported by @sbabcoc. E.g. following https://stackoverflow.com/questions/54450475/junit-listener-configuration-in-gradle However: To also get the calls to testRunFinished I needed to switch to 'useJUnitPlatform()' (using the vintage engine). |
Yes, |
any update on this request? |
If you configure your test task to Docs: https://junit.org/junit5/docs/current/user-guide/#launcher-api-launcher-session-listeners-custom |
Expected Behavior
I'd like to be able to include my own JUnit RunListener classes for starting up and shutting down resources that are used across test suites to massively reduce resource setup overhead on test suites, the same way that it's supported in Maven surefire.
Current Behavior
There's no way to setup a run listener using Gradle
Context
This affects me in that, for testing, we used multiple "outside" resources like database, message queue, mail, etc. services, and we embed those for more real world validation testing.
As those services tend to take time to startup and shutdown, we setup test suites and a run listener that initializes such services only once, then executes all tests, and shuts them down, otherwise, the services get startup and shutdown per test, which consumes a lot of time and resources.
Your Environment
This is executed both in developers local machines with MacOS, Windows and Linux, and when executed by the CI server (Jenkins) it's tends to execute a lot of build pipelines in parallel, so this is where we notice the resource usage the most.
The text was updated successfully, but these errors were encountered: