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
Disable Parameterised tests selectively #163
Comments
That's a very interesting idea @nishantvas. I looked at the issues and thought about the use case. I don't think I'd like a reflection-based solution because it is too brittle, specifically across Jupiter versions (I try to avoid specific Pioneer versions only working for specific Jupiter versions). I played a little with argument converters, but they're not good fit for this either. But why so complicated? What about a simple declarative API? @ParameterizedTest
@CsvSource({"1, a", "1, b", "2, a", "2, b"})
void parameterizedTest(int number, String string) {
Disable
.when(number, string)
.is(2, "a")
.is(2, "b");
// actual test code
if (number % 2 == 0)
throw new IllegalArgumentException();
}
private static class Disable {
private final Object[] arguments;
private Disable(Object[] arguments) {
this.arguments = arguments;
}
public static Disable when(Object... arguments) {
return new Disable(arguments);
}
public Disable is(Object... values) {
if (Arrays.equals(arguments, values))
throw new TestAbortedException("Test aborted for arguments " + Arrays.toString(values));
return this;
}
} Let me know what you think. |
You're right, this does result in the same effect. For someone looking to build a straight forward solution this works perfectly. Although I personally don't prefer programatic solutions for such things which need to be put in a test body. I incline more towards meta data like annotations because they're more readable and indicate their purpose better. Also, I have some extra fields in the disable annotation in my particular case which can't be processed from the test itself. Thanks for coming up with this immensely simple solution. I don't think there is a need for or possibility of creating any extension which can achieve the same effect without over engineering things. |
You're right about that, I usually prefer such a solution for those exact reasons, but since it seems to come at quite a cost here, I think the programmatic approach can be ok.
I'd be curious to learn more about that to better understand the general use case. 😊
I kinda like the idea, so I'm still thinking about implementing it. 😁 |
I'm using this with allure and am using the annotations to process tests which are disabled to link to their individual issue tags. Since each failure in the parameterized tests can be due to a different bug, using an annotation let's me mark it better lest I take up the task of updating the test code to save this data in some common place. Which is never a good idea.
Let me know how you think this should be implemented as an extension and I can work on building it. |
While I agree with your general preference to use an annotation-based approach, I still don't quite get whether it's actually necessary or not. If you want to link to an issue for disabled argument combinations, we could do something like: @ParameterizedTest
@CsvSource({"1, a", "1, b", "2, a", "2, b"})
void parameterizedTest(int number, String string) {
Disable
.when(number, string)
.is(2, "a")
.becauseOf("https://our.jira.company.com/issues/PROJ-123")
Disable
.when(number, string)
.is(2, "b")
.becauseOf("https://our.jira.company.com/issues/PROJ-125")
// actual test code
if (number % 2 == 0)
throw new IllegalArgumentException();
} Anyway, I convinced myself that this is a valid feature request, so if anybody wants to implement it, leave a comment here and go for it! |
I'd like to give this a try. |
I do have a related question, though. Say, you disable the tests and your build runs fine. How will you remember to enable the tests later? Wouldn't it be better to create a separate test for the specific test case and annotate it with |
You can always mark them as Alternatively, like many people do, you can mark the fixing issue on the What I'm trying to say is, you're going to have to go back to those |
@nicolaiparlog, so my idea with disabling parameterized tests was to support allure reporting. Namely, I could use allure meta annotations to point to individual failing tests and to enable me to assign an Issue property to the parameterized tests. Creating as an extension would allow people to meta annotate the extension and achieve something similar. |
Let me see if got this right, @nishantvas. Say we would create an annotation that works as we want, e.g.: @DisableParamterizedTest("foo", 5) // in JUnit Pioneer
void test(String s, int i) {
...
} Then you would like to combine it with allure reporting (which I know nothing about, so I may totally misunderstand you) to create your own annotation: // declaration
@AllureReportingAnnotation
@DisableParamterizedTest
public @interface MyOwnDisableParamterizedTest { }
// use
@MyOwnDisableParamterizedTest("foo", 5)
void test(String s, int i) {
...
} If that's what you're after, I have bad news: There's no way for my extension to get to |
@nicolaiparlog so It's almost how you're putting it, however, as I mentioned when I raised this
The Extensions could very easily have access to Since we don't want to go with the option of using reflection to access the
And them the AllureSetup was right on point, it uses the some Meta annotations similar to how you mentioned to add the value to Allure reports.
One thing to note would be that we want to disable individual parameterized tests, therefore, we will have to let the
|
I pushed an example PR to show what I meant. It is achieving the same effect as the snippet you mentioned,
However, by having this as an annotation, I can fetch this in my reports and it's more accessible to other extensions or listeners if required (like in my case with Allure) |
We just updated to Jupiter 5.5 (#278) to use the new |
Current situation
Currently any @disabled extensions that are there in the junit5 framework disable any test at the class level or at the invocation level before any of the parameters are resolved and the tests are launched
This leads to disable all the tests that were potentially going to run from any of the Parameterized test sources and it reports the test as only one skipped test.
Suggestion
To possible have an extension similar to @disabled (and it's counterparts) which is able to disable a single instance of the Parameterized test rather than all of them and only marks that individual test as skipped while others can run
Currently because of the following it is not straight forward to build something like this
junit-team/junit5#1139
junit-team/junit5#1668
junit-team/junit5#1884
And the only way is to use reflection to get access to the arguments for a Parameterized test's resolved argument
It would be nice to have native support for this as well, however as I understand, for programatic argument sources, specially ArgumentProvider and MethodSource, it isn't always sensible to let the arguments get initialised if we're skipping the tests
Currently I've built the following solution for this (it needs to be "productionalized" the answer is just a hint to actual impl)
https://stackoverflow.com/questions/59716009/is-it-possible-to-disable-a-parameterized-test-basis-arguments-in-junit5
However, it's only valid till Junit5 version 5.3.x, after 5.5.x the Extension classes are package-private and the same solution needs a little bit of tweaking.
The text was updated successfully, but these errors were encountered: