junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.51k stars 3.24k forks source link

Parameterized runner should name tests better than sequential numbers #44

Closed anshnd closed 12 years ago

anshnd commented 14 years ago

Right now the test names are named sequentially 0,1, etc in Parameterized:

@Override protected String getName() { return String.format("[%s]", fParameterSetNumber); }

@Override protected String testName(final Method method) { return String.format("%s[%s]", method.getName(), fParameterSetNumber); }

It would help if the data themselves were to be used in determining the name. Proposed alternatives for name (which has to be determinited statically before Test is instantiated):

I can provide (again) a patch. This issue was tracked as http://sourceforge.net/tracker/?func=detail&atid=365278&aid=1742040&group_id=15278 and https://sourceforge.net/tracker/?func=detail&atid=365278&aid=1630834&group_id=15278 I can submit a patch if needed

JThoennes commented 14 years ago

How about this issue? The suggestions are good, when will you implement it / incorporate the patch?

Thanks, Jörg

JThoennes commented 14 years ago

See also issue 24 which talks about the same topic, but was entered by dsaff.

jjunit2010 commented 14 years ago

Any more news on when this fix might be implemented?

reccles commented 14 years ago

There appears to be some demand for this simple feature outside as well: http://stackoverflow.com/questions/650894/change-test-name-of-parameterized-tests

Yishai commented 14 years ago

As an additional (simpler) suggestion, how about just changing
@Override protected String testName(final FrameworkMethod method) { return String.format("%s%s", method.getName(), Arrays.toString(fParameterList.get(fParameterSetNumber))); }

dbt commented 14 years ago

From my deployment experience (I forked Parameterized internally to my project and extended it with similar functionality), using toString or non-static testName() functions were suboptimal, and using a fixed portion of the parameters list (either the first parameter or all of them) was also not useful. My preferred solution was to support an annotated static method that took the same arguments as the constructor and returned a string, and fall back to the test number if that didn't work.

I'm happy to get the (fairly trivial) patch open sourced if that would help solve this problem.

Thanks, -dave

ralfebert commented 13 years ago

See http://github.com/ralfebert/junit/commit/d5fedb0

JThoennes commented 13 years ago

Could this patch merged into the next JUnit release, please?

And when will the next release happen?

davidkarlsen commented 13 years ago

+1

kaipe commented 13 years ago

I have released a framework that offers a solution to this problem: http://callbackparams.org The test-class runner that will probably solve most cases where this problem arises is ... http://callbackparams.org/project/api-javadoc/org/callbackparams/junit4/EnumRunner.html ... and for more complex cases the rest of the framework has a lot to offer.

The key feature in regard to this issue is that tests are named by the overridden method java.lang.Object#toString(), which for enum-constants happen to return the name of the constant. If the enum-constant name won't do it is of course still possible to override java.lang.Object#toString() explicitly for those constants that need more verbose test-names.

Anyhow, I think that a solution that does not rely on the toString()-method is simply not very elegant.

alexeyOnGitHub commented 13 years ago

the source code posted here worked perfectly http://stackoverflow.com/questions/650894/change-test-name-of-parameterized-tests

here are the direct links to the source code:

http://code.google.com/p/migen/source/browse/trunk/java/src/uk/ac/lkl/common/util/testing/LabelledParameterized.java?r=3789

http://code.google.com/p/migen/source/browse/trunk/java/src/uk/ac/lkl/common/util/restlet/test/builder/ServerBuilderTest.java?r=3789

mmakowski commented 13 years ago

+1

soehalim commented 12 years ago

+1

elygre commented 12 years ago

+1

binkley commented 12 years ago

+1

davidkarlsen commented 12 years ago

Cmon! Why can't you apply this Kent? You even have ready patches and the issue is ancient. Isn't good naming part of cleancode and TDD practices - or do we simply have to dump junit for testng?

sivaram-psg commented 12 years ago

+1

joelmheim commented 12 years ago

+1

stefanbirkner commented 12 years ago

I like Dave's idea of a label method. It may look like

@Label
public String labelForParameters(Object... parameters) {
  ...
}

Hopefully there's time today or tomorrow evening to implement this.

jonfreedman commented 12 years ago

+1

stefanbirkner commented 12 years ago

I'm just waiting for pull request #348

bsbodden commented 12 years ago

+1

RaviH commented 12 years ago

+1 (guessing that's the way to vote it up?)

Tibor17 commented 12 years ago

+1

Jeanguitoune commented 12 years ago

+1

yurodivuie commented 12 years ago

+1. We've started keeping our own version of Parameterized to fix this problem, but it would be great to have this supported from the source.

slongoni commented 12 years ago

+1

sharfah commented 12 years ago

+1

cwhite102 commented 12 years ago

+1

veger commented 12 years ago

+1

java-artisan commented 12 years ago

+1

lbuchy commented 12 years ago

+1

mrmanc commented 12 years ago

+1

ghost commented 12 years ago

+1

mariusx commented 12 years ago

+1

jpfielding commented 12 years ago

+2

z3jiang commented 12 years ago

+1

avernet commented 12 years ago

+1

guyburton commented 12 years ago

+1 we have used a fork of Parameterized to implement this feature. We have also made the TestClassRunnerForParameters protected, and it is created by a protected method to allow extension of Parameterized within another implementation of Suite.

stefanbirkner commented 12 years ago

The feature is implemented. See #393.

@dsaff Could you please close the issue.

dsaff commented 12 years ago

Done.

java-artisan commented 12 years ago

Many thanks indeed ! :-)