Reputation: 6656
I'm testing extensively with JUnit and sometimes - while debugging my code - I want (temporary) only run a single @Test
of my @RunWith(Arquillian.class)
test class. Currently I'm adding a @Ignore
to all other tests and wondering if something like @IgnoreOther
does exist.
Are there better solutions to ignore all other tests?
Upvotes: 12
Views: 7463
Reputation: 6656
The answer from srkavin (and mijer) is correct, but the code is deprecated from JUnit 4.9. The interface and the method signature have changed. I want to provide this for others interested in this issue.
public class IgnoreOtherRule implements TestRule
{
private String applyMethod;
public IgnoreOtherRule(String applyMethod){
this.applyMethod = applyMethod;
}
@Override
public Statement apply(final Statement statement, final Description description)
{
return new Statement()
{
@Override
public void evaluate() throws Throwable {
if (applyMethod.equals(description.getMethodName())) {
statement.evaluate();
}
}
};
}
}
Upvotes: 3
Reputation: 1180
Test rules (JUnit 4.7+) will help. For example, you can write a rule that ignores all @Test methods except one with a specific name.
Upvotes: 4
Reputation: 4712
Just my two cents. You can try to use Junit Rules as @srkavin suggested.
Here is an example.
package org.foo.bar;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class SingleTestRule implements MethodRule {
private String applyMethod;
public SingleTestRule(String applyMethod) {
this.applyMethod = applyMethod;
}
@Override
public Statement apply(final Statement statement, final FrameworkMethod method, final Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (applyMethod.equals(method.getName())) {
statement.evaluate();
}
}
};
}
}
package org.foo.bar;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
public class IgnoreAllTest {
@Rule
public SingleTestRule test = new SingleTestRule("test1");
@Test
public void test1() throws Exception {
System.out.println("test1");
}
@Test
public void test2() throws Exception {
Assert.fail("test2");
}
@Test
public void test3() throws Exception {
Assert.fail("test3");
}
}
Upvotes: 9
Reputation: 115378
The simplest way is to replace all @Test
to //###$$$@Test
. Then when your debugging is finished replace //###$$$@Test
to @Test
.
Moreover typically IDEs allow running one test only. For example in Eclipse you can do it from Outline view.
Upvotes: 10