JUnit 4.5 - Problems with Filter in conjunction with parameterized tests

View: New views
2 Messages — Rating Filter:   Alert me  

JUnit 4.5 - Problems with Filter in conjunction with parameterized tests

by jayasinghe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi List!

I am currently working on a JUnit project that enables us to run tests in a well-defined order and with the ability to skip tests in a flexible manner (by annotations and property files). Additionally we do need to pass certain enviroment-parameters to the tests.
The problem is that I somehow did not manage to combine the filters with the parameterized runners. My approach is to extend Parameterized.class and add a custom filter. The point is that the filter does not seem to have any effect on the set of executed tests.

I tried my example with JUnit 4.3.4 that is packaged with Eclipse and it works as expected. With JUnit 4.5 checked out from SOurceForge it does not work.

Here is my source-code. It is a little extension to a Filter-example that Kent posted ealier:

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

public class TestExample {

        @RunWith(FilteredParameterizedRunner.class)
        public static class Tests {
                private int value1;
                private int value2;
               
                @Parameters
                public static Collection data() {
                return Arrays.asList(new Object[][] { { 2, 2 }, { 1, 1 }});
                }
               
                public Tests(int value1, int value2) {
                        this.value1 = value1;
                        this.value2 = value2;
                }
               
                @Test
                public void a() {
                        System.out.println("Run test A " + value1);
                }

                @Test
                public void b() {
                        System.out.println("Run test B " + value1);
                }

                @Test
                public void c() {
                        System.out.println("Run test C " + value1);
                }
        }
       
        public static class NamedTestsFilter extends Filter {
                final String[] tests = new String[] { "a", "b" };
               
                @Override
                public String describe() {
                        return "only tests starting with 'a'";
                }

                @Override
                public boolean shouldRun(Description description) {
                        if(description.isTest()) {
                                for (String each : tests) {
                                        if (description.getDisplayName().startsWith(each)) {
                                                System.out.println("Test with name " + description.getDisplayName() + " starts with " + each);
                                                return true;
                                        }
                                }
                                return false;
                        }
                        else if(description.isSuite()) {
                                System.out.println("suite");
                                return true;
                        }
                        else {
                                System.out.println("else");
                                return false;
                        }
                }
        }
       
        public static class FilteredParameterizedRunner extends Parameterized {

                public FilteredParameterizedRunner(Class<?> klass) throws Throwable {
                        super(klass);
                       
                        for(Runner rnr : this.getChildren()) {
                                super.filter(new NamedTestsFilter());
                        }
                }
               
        }
}

Is this a bug of 4.5 or is it intended behavior?

Thanks in advance !

Robin

RE: JUnit 4.5 - Problems with Filter in conjunction with parameterized tests

by kentb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jaya,
 
Thank you for the report. I can't think offhand of a change that would cause
the problem, er..., opportunity for improvement, you found. We intend to
keep code like yours working between 4.4 and 4.5.
 
What would be most helpful to us (because otherwise it's work we will do
ourselves) is if you can pinpoint the difference that's causing you problems
and write a unit test to demonstrate it. That is, write a test that passes
in 4.4, fails in 4.5, and that, if it worked in 4.5, would cause your code
to work. We'll address your problem more quickly if we have such a test.
 
Best regards,
 
Kent Beck
Three Rivers Institute

  _____  

From: junit@... [mailto:junit@...] On Behalf Of
jayasinghe
Sent: Wednesday, June 18, 2008 7:31 AM
To: junit@...
Subject: [junit] JUnit 4.5 - Problems with Filter in conjunction with
parameterized tests




Hi List!

I am currently working on a JUnit project that enables us to run tests in a
well-defined order and with the ability to skip tests in a flexible manner
(by annotations and property files). Additionally we do need to pass certain
enviroment-parameters to the tests.
The problem is that I somehow did not manage to combine the filters with the
parameterized runners. My approach is to extend Parameterized.class and add
a custom filter. The point is that the filter does not seem to have any
effect on the set of executed tests.

I tried my example with JUnit 4.3.4 that is packaged with Eclipse and it
works as expected. With JUnit 4.5 checked out from SOurceForge it does not
work.

Here is my source-code. It is a little extension to a Filter-example that
Kent posted ealier:

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

public class TestExample {

@RunWith(FilteredParameterizedRunner.class)
public static class Tests {
private int value1;
private int value2;

@Parameters
public static Collection data() {
return Arrays.asList(new Object[][] { { 2, 2 }, { 1, 1 }});
}

public Tests(int value1, int value2) {
this.value1 = value1;
this.value2 = value2;
}

@Test
public void a() {
System.out.println("Run test A " + value1);
}

@Test
public void b() {
System.out.println("Run test B " + value1);
}

@Test
public void c() {
System.out.println("Run test C " + value1);
}
}

public static class NamedTestsFilter extends Filter {
final String[] tests = new String[] { "a", "b" };

@Override
public String describe() {
return "only tests starting with 'a'";
}

@Override
public boolean shouldRun(Description description) {
if(description.isTest()) {
for (String each : tests) {
if (description.getDisplayName().startsWith(each)) {
System.out.println("Test with name " + description.getDisplayName() +
" starts with " + each);
return true;
}
}
return false;
}
else if(description.isSuite()) {
System.out.println("suite");
return true;
}
else {
System.out.println("else");
return false;
}
}
}

public static class FilteredParameterizedRunner extends Parameterized {

public FilteredParameterizedRunner(Class<?> klass) throws Throwable {
super(klass);

for(Runner rnr : this.getChildren()) {
super.filter(new NamedTestsFilter());
}
}

}
}

Is this a bug of 4.5 or is it intended behavior?

Thanks in advance !

Robin
--
View this message in context: http://www.nabble.
<http://www.nabble.com/JUnit-4.5---Problems-with-Filter-in-conjunction-with-
parameterized-tests-tp17983968p17983968.html>
com/JUnit-4.5---Problems-with-Filter-in-conjunction-with-parameterized-tests
-tp17983968p17983968.html
Sent from the JUnit - User mailing list archive at Nabble.com.



 


[Non-text portions of this message have been removed]