-
Notifications
You must be signed in to change notification settings - Fork 162
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
Custom parameter format #101
Comments
Sorry, my initial post might have been a little bit unclear, as far as I understand it, your example is exactly the other way around, you write '2017-01-01' and have a
and the output would for example only be the year of |
Oh, now reading I thought in the wrong direction. You can provide a custom placeholder / formatter. Here are the corresponding examples:
Better and helpful this time? Feedback and ideas for simplifying the mechanism (especially with JUnit Jupiter) are very welcome. |
The example looks like it could be, what I mean, however I don't fully understand the test. When I run it (in IntelliJ), the parameters Another question, this doesn't seem threadsafe? In case I run two test classes in parallel, and both want to format the same type in a different way? As for feedback, when I tried to figure this out, I was kind of wishing for a mechanism like JGiven's |
Unfortunately, the test does not work anymore in JUnit4 as And yes, the test case itself is made up just for demonstration as I cannot assert the test naming at that point ... so this is kind of a manual test which I forgot to check recently. However, I kind of like the idea with the |
…extension, done by the user ... (#101)
…extension, done by the user ... (#101)
I have similar issues but think the given solution is way too complex and does not really work. My case is simply one of the values is a long, but I want it displayed in hex for a particular data provider. I end up sending it in as a hex string and converting to long in the test method. |
Maybe to help the discussion, I can add a typical type of test case that I'm frequently encountering in my project. Let's suppose we have some
In practice I would of course have more data points. When the tests run, my test cases are described as
So in particular, the information what was relevant for my test case, is lost. Sure, when I debug it, I can figure out "oh, it's case 5, and the AssertionError is x, thus the problem must be ...", but when I skim those tests in our CI environment, it would be much nicer, to read Alternatively I could break it down to primitive values, but in this case this would kind of look like
and then print
which is almost worse. So what I normally end up with, is writing a I was thinking, if this use case is too specific for my project, and the implementation too complex, I might just end up writing some generic one/two/three parameter test case
Then I could reuse |
Thanks for your comments, @dalewking and @codecholeric here are the brainstormed options I could think about:
Do you have any futher ideas? What would be your preference? |
These are examples how the usage could look like:
@ExtendWith(DataProviderExtension.class)
class AnnotationArgumentPlaceholderAcceptanceTest {
@DataProvider(format = "[%i: %aa[0..-1]]")
static Object[][] dataProvider() {
// @formatter:off
return new Object[][] {
{ new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class), new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class) },
};
// @formatter:on
}
@TestTemplate
@UseDataProvider
void test(@ArgumentFormat(UnwrapFormatter.class) WrappedClass clazz, @MetaAnnotationArgumentFormat WrappedClass clazz) {
// TODO
}
}
@ExtendWith(DataProviderExtension.class)
class AnnotationArgumentPlaceholderAcceptanceTest {
@DataProvider(testNameFormatter = CustomTestNameFormatter.class)
static Object[][] dataProvider() {
// @formatter:off
return new Object[][] {
{ new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class), },
};
// @formatter:on
}
// User specific option 1
@TestTemplate
@UseDataProvider
void test(@UserSpecificAnnotation WrappedClass clazz) {
// TODO
}
// User specific option 2
@TestTemplate
@UseDataProvider
void test(/* just always format the same way */ WrappedClass clazz) {
// TODO
}
// Other user specific options go here ...
}
@ExtendWith(DataProviderExtension.class)
class AnnotationArgumentPlaceholderAcceptanceTest {
@DataProvider
@TestNameFormatter(CustomTestNameFormatter.class)
static Object[][] dataProvider() {
// @formatter:off
return new Object[][] {
{ new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class), },
};
// @formatter:on
}
// User specific option 1
@TestTemplate
@UseDataProvider
void test(@UserSpecificAnnotation WrappedClass clazz) {
// TODO
}
// User specific option 2
@TestTemplate
@UseDataProvider
void test(/* just always format the same way */ WrappedClass clazz) {
// TODO
}
// Other user specific options go here ...
}
@ExtendWith(DataProviderExtension.class)
class AnnotationArgumentPlaceholderAcceptanceTest {
@DataProvider(toStringMethods = { WrappedClassCustomToStringFunction.class, NormalToStringFunction.class, })
static Object[][] dataProvider() {
// @formatter:off
return new Object[][] {
{ new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class), new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class) },
};
// @formatter:on
}
@TestTemplate
@UseDataProvider
void test(WrappedClass clazz, WrappedClass clazz) {
// TODO
}
// Other user specific options go here ...
} |
Personally I like the second and third approach the most, with no specific preference. Both approaches would solve my problems, as far as I can see. The decision probably depends on the desired degree of separation from an API point of view, approach 3 suggests more, that it is an independent feature, while approach 2 binds it stronger to the core functionality... |
…101) Signed-off-by: Andreas Schmid <[email protected]>
…annotation (#101) Signed-off-by: Andreas Schmid <[email protected]>
I implemented approach 2 ("specify a test name formatter with @codecholeric Is that sufficient for you or do you also need it in Note: I want to get rid of |
I needed it in junit 4 but today is my last day on that job so not so much
now.
…On Fri, Mar 30, 2018, 5:03 PM Andreas Schmid ***@***.***> wrote:
I implemented approach 2 ("specify a test name formatter with
@dataProvider") for junit-jupiter in commits above, simply because it
fits better with existing APIs and strategy.
@codecholeric <https://github.com/codecholeric> Is that sufficient for
you or do you also need it in junit4 module?
Note: I want to get rid of format = "..." option in @dataProvider in the
future but giving formatter precedence should be perfect for now.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#101 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAVHzlwP0QfaDTFK1bkuh9r9pHH1S0gAks5tjp2ugaJpZM4QPQm6>
.
|
Signed-off-by: Andreas Schmid <[email protected]>
Signed-off-by: Andreas Schmid <[email protected]>
…for JUnit4 module (#101) Signed-off-by: Andreas Schmid <[email protected]>
Hi @dalewking, maybe your colleagues can use it, so I also provided the solution for JUnit4. |
I need it for JUnit 4 for our current project, too, so thanks a lot 😃 |
Hehe, depends only on my time tonight :-) |
* issue101-custom-parameter-format: support to specify a test name formatter on @dataProvider annotation for JUnit4 module (#101) minor refactorings (#101) move DataProviderTestNameFormatter to core (#101) adjust other DisplayNameContext constructors and custom DataProvider annotation (#101) support to specify a test name formatter on @dataProvider annotation (#101) add JavaDoc for named parameter formatting option
Nice, thanks 😃 |
I don't know, if this is already possible, and I just don't know about it, but I find myself often writing wrapper objects, simply providing a reasonable
toString
for the test parameter (e.g. the input is aClass<?>
but I don't wantclass some.pkg.Foo
in the output, but would rather have the simple class name).Is there any elegant solution for this case? I know I can control the format of the method name, but the parameter always seems to be presented as
toString
?The text was updated successfully, but these errors were encountered: