Search the web
Sign In
New User? Sign Up
junit · JUnit, the Java unit testing framework written by Kent Beck and Erich Gamma.
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
How to get test's name in JUnit 4   Topic List   < Prev Topic  |  Next Topic >
Reply | Forward  | 
Re: How to get test's name in JUnit 4


> > It seems like the RunListeneer would have a reference to the currently
> > running test, simply because of its "testStarted" and "testFinished"
> > functions
[...]
> > Is it possible to get access to this from within the test class
> > itself?
>
> No, by design. Sorry about that.
[...]
> Information on custom runners is currently somewhat lacking. It
> appears from your later message that you've somewhat successfully
> navigating the waters. Any interest in writing up a short blog entry
> or article on your experiences? Thanks,
>
> David Saff
>


To get a test's name use this runner with the
@RunWith(NameAwareTestClassRunner.class) at the top of your test class
and inside your @Before method do something like:

String testName = NameAwareTestClassRunner.getTestName();

Anyway, here's the runner.


// NameAwareTestClassRunner.java
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.Description;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class NameAwareTestClassRunner extends TestClassRunner {
public NameAwareTestClassRunner(Class<?> klass) throws
InitializationError {
super(klass);
}

private static String testName;

protected static String getName() {
return testName;
}

protected static void setName(final String name) {
testName = name;
}

protected static String getTestName() {
if (testName == null)
return null;

int last = testName.indexOf('(');
if (last < 0)
last = testName.length() + 1;
return testName.substring(0, last);
}

private static class NameListener extends RunListener {
/** Record start of tests, not suites */
public void testStarted(Description description) throws Exception {
System.err.println(" STARTED: " + description.getDisplayName());
setName(description.isTest() ? description.getDisplayName() : null);
}

public void testFinished(Description description) throws Exception {

System.err.println("FINISHED: " + description.getDisplayName());
if (getName() != null)
if (getName().equals(description.getDisplayName()))
setName(null);
else
throw new Exception("Test name mismatch");
}
}

public void run(final RunNotifier notifier) {
notifier.addListener(new NameListener());
super.run(notifier);
}
}








Fri Jan 12, 2007 2:52 pm

gabrielcooper
Offline Offline
Send Email Send Email

Forward
 | 
Expand Messages Author Sort by Date

I'm busy converting JUnit 3 tests to JUnit 4 tests but I've hit a small snag. The JUnit 3 classes subclassed TestCase, which had a constructor of ...
gabrielcooper
Offline Send Email
Jan 5, 2007
3:48 pm

... test.getClass().getName() ? Look at the call stack? Kevin www.junitfactory.com You send us code. We send you tests. For free....
Kevin Lawrence
kevinwilliam...
Offline Send Email
Jan 5, 2007
5:05 pm

... Hm, I think my terminology is wrong? Doesn't that give you the class's name, not the test's name? I need the... name of the method currently being executed...
gabrielcooper
Offline Send Email
Jan 5, 2007
8:14 pm

Gabriel -- I can't figure out how to get the test name, short of invoking the test yourself (via the "main" or something similar). Sorry. That said, I'm very...
Mike Bria
bria526
Offline Send Email
Jan 5, 2007
11:20 pm

... Well, it turns out they don't use it in the actual test case, but in the setUp/tearDown instead. Specifically to insert into/delete from a database with...
gabrielcooper
Offline Send Email
Jan 9, 2007
2:48 pm

... No, by design. Sorry about that. ... Information on custom runners is currently somewhat lacking. It appears from your later message that you've somewhat...
David Saff
dsaff
Offline Send Email
Jan 11, 2007
8:31 pm

... [...] ... [...] ... To get a test's name use this runner with the @RunWith(NameAwareTestClassRunner.class) at the top of your test class and inside your...
gabrielcooper
Offline Send Email
Jan 12, 2007
3:50 pm

Kevin, My sense of the question was a little different. It sounds like Gabriel is using the name field to store a file name. In JUnit 4 there is no name field....
Kent Beck
kentlbeck
Offline Send Email
Jan 5, 2007
11:28 pm

... Gabriel is ... } }); Without the name I'll have to rewrite the tests to a significant degree anyway, but for clarification, the programmers here have a ...
gabrielcooper
Offline Send Email
Jan 8, 2007
5:57 pm

Hello David, Gabriel, Thanks for your support, i could find any other simple way. I had to implement a custom runner....This was the only way out... As Gabriel...
Vasudevan Kalyanaraman
vkvasudevan
Offline Send Email
Jan 12, 2007
8:31 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help