JUnitParams icon indicating copy to clipboard operation
JUnitParams copied to clipboard

Running individual test case

Open Robelind opened this issue 9 years ago • 14 comments

When I try to run an individual test case with parameters in Eclipse, I get an initializationError from the runner. I'm always forced to run the whole class, i.e. all test cases. Any way to do this?

Robelind avatar Jul 21 '15 13:07 Robelind

It seams that the problem has two causes. First the JUnitParamsRunner class makes its own Descriptions for methods, but the super class of JUnit BlockJUnit4ClassRunner makes different Description objects in the method describeChild().

So the SubForestFilter filter of Eclipse does not find the correct method.

I made a subclass of JUnitParamsRunner which overrides the describeChild() method, appying the JUnitParamsRunner logic for description creation.

public class MyJUnitParamsRunner extends JUnitParamsRunner {
    private final ConcurrentHashMap<FrameworkMethod, Description> methodDescriptions = new ConcurrentHashMap<>();

    private final ThreadLocal<FrameworkMethod> describeChildAlreadyCalled = new ThreadLocal<>();

    public MyJUnitParamsRunner (Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Description describeChild(FrameworkMethod method) {
        if (describeChildAlreadyCalled.get() == method) {
            return super.describeChild(method);
        }
        describeChildAlreadyCalled.set(method);
        try {
            Description description = methodDescriptions.get(method);

            if (description == null) {
                // Call the JUnitParamsRunner logic instead of the org.junit.runners.BlockJUnit4ClassRunner.describeChild(FrameworkMethod) logic
                description = describeMethod(method);

                methodDescriptions.putIfAbsent(method, description);
            }

            return description;
        } finally {
            describeChildAlreadyCalled.set(null);
        }
    }

}

}

This will solve the problem to start one testMethod (with all inputs). But it does not solve the issue of executing one testMethod with only one input.

This issue seams to be the "default" method description logic of JUnitParams. JUnitParams creates Descriptions containing () for the methodName. E.g. [0] 15,false (personIsAdult) The full name would be "[0] 15,false (personIsAdult)(mypkg.JUnitParamsCsvTest)"

But JUnit uses () to filter the classname for the identifier. See class org.junit.runner.Description.METHOD_AND_CLASS_NAME_PATTERN

So to fix that issue is used the @TestCaseName annotation E.g. @TestCaseName("[{index}] {method}: {params}")

    @Test
    @FileParameters("testDataJUnitParamsCsvTest.csv")
    @TestCaseName("[{index}] {method}: {params}")
    public void personIsAdult(int age, boolean valid) {

Now I am able to execute exactly one testCase with one input.

It would be nice, if the default naming of methods could be changed to not include any (). And the JUnitParamsRunner overrides the describeChild() method.

AndreasTu avatar Sep 17 '15 16:09 AndreasTu

any ETA on this making it as part of the official release? a pretty valuable/necessary feature. thank you.

ncd7 avatar Jun 15 '16 17:06 ncd7

Hi @AndreasTu ,

Thanks for the workaround :+1: (using custom MyJUnitParamsRunner + adding @TestCaseNameon method). I don't get the initialization error anymore, only the single method is run, but yet, the JUnit View still displays all other tests methods in the class and the test process never returns ...

Any ideas?

Eclipse Mars (4.5.0) Junit 4.12

tinesoft avatar Feb 01 '17 15:02 tinesoft

Hi @tinesoft , I had the same problems with the JUnit View in Eclipse, and no good solution for it sorry.

Retested today on: Eclipse Neon SR.1a (4.6.1) JUnit 4.12

AndreasTu avatar Feb 02 '17 06:02 AndreasTu

I have the same issue with the Junit View in Eclipse.

My IDE: Eclipse Mars 4.5.1. JUnit 4.12.

Does already exist a solution for the issue??

Another important point is the exception handling... Unfortunately no detailed exception is printed at the JUnit View or the Console.

In my case it was only a small problem (the path of the testfile was wrong) but I searched more than 2 hours without really knowing where to look. I only saw the "initializationError" without any details. It is perhaps possible to throw the whole exception / or another detailed message instead of using just the initializationError? It would be great and very useful :-)

Kind regards and many thanks in advance, Christian

Chisey88 avatar May 05 '17 22:05 Chisey88

Yes, same problem, workaround doesn't help.

The filter seems to be looking for "method(class)" as opposed to "[index] method: params(class)" so it won't rerun my individual tests.

tbroberg avatar Sep 10 '17 22:09 tbroberg

i also have problems for running tests without params. will this solution work for me as well? or is this a different issue?

tibistibi avatar Sep 26 '17 10:09 tibistibi

This is a real pain point with this project. is anyone any nearer to finding a solution?

jeacott1 avatar Mar 09 '18 23:03 jeacott1

It seems that nobody is maintaining the project, although it is a real helper for JUnit tests.

AndreasTu avatar Mar 10 '18 09:03 AndreasTu

Now that JUnit 5 is getting proper parameterized tests, I think this project isn't going to get much attention (as useful as it has been).

I know Eclipse has some support for JUnit 5 but when I last tried there were still some issues (I forget what). Hopefully 4.8 will get things working 100%.

moreginger avatar Mar 10 '18 12:03 moreginger

Sorry for insufficent support on this issue. We, at Pragmatists, wish we could spend more time on this project. As @moreginger mentioned, nowadays JUnit 5 has decent support for parameterized tests. We recommed to use it when possible. It will be more likely better supported by all IDEs.

Nevertheless we are aware that many people, for some reasons, cannot migrate to JUnit 5 and JUnitParams is still usefull. That why we are doing our best to keep it maintained.

michal-lipski avatar Mar 10 '18 14:03 michal-lipski

yeah, I know about junit 5, but prefer this project. shame.

jeacott1 avatar Mar 12 '18 23:03 jeacott1

+1

Am 13.03.2018 um 03:06 schrieb jeacott1 [email protected]:

yeah, I know about junit 5, but prefer this project. shame.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

esvet avatar Mar 13 '18 09:03 esvet

fyi, tried junit5, but could not get it to play at all with integration-test phase of maven mixed with junit 4 tests, and its all a bit marginal right now, so am returning to junit4 and junitparams for the foreseeable.

jeacott1 avatar Mar 25 '18 23:03 jeacott1