vscode-java-test icon indicating copy to clipboard operation
vscode-java-test copied to clipboard

Run tests on class does not take inherited methods into consideration

Open maxandersen opened this issue 6 years ago • 19 comments

When you have a test class with inherited test methods only the methods explicitly marked on the current class gets run. Would have expected it to run all tests.

Example: https://github.com/jbake-org/jbake/blob/master/jbake-core/src/test/java/org/jbake/app/template/FreemarkerTemplateEngineRenderingTest.java#L40

If you run this only 2 tests are run, none of the @Test from its super class (https://github.com/jbake-org/jbake/blob/master/jbake-core/src/test/java/org/jbake/app/template/AbstractTemplateEngineRenderingTest.java) are run

maxandersen avatar Nov 14 '18 05:11 maxandersen

I might also add that along with tests inherited from a parent class, it would be useful to also be able to run tests from default interface methods. Note: I'm pretty sure this requires running the tests using junit 5+ (if using junit).

E.g. Given:

public interface CanJumpTest<TestInstance extends CanJump> {
    TestInstance getTestInstance();

    int getMinExpectedJumpHeight();

    @Test
    default void canJumpToExpectedHeight()  {
        assertThat(getTestInstance().jump()).isGreaterThanOrEqualTo(getMinExpectedJumpHeight());
    }
}
public class BasketballPlayerTest implements CanJumpTest<BasketballPlayer> {
    @Override
    BasketballPlayer getTestInstance() {
        return new BasketballPlayer();
    }

    @Override
    int getMinExpectedJumpHeight() {
        return 70;
    }
}

Running BasketballPlayerTest should run the canJumpToExpectedHeight test.

joshmanderson avatar Jan 10 '19 06:01 joshmanderson

This issue will be addressed in 0.15.0

We refactored the logic of parsing the test results recently.

jdneo avatar Mar 08 '19 03:03 jdneo

Is this issue fixed? encounter the same issue in 0.19.0.

lanfuchao avatar Aug 14 '19 05:08 lanfuchao

is there an update to this? I'm currently working around it by just dragging the inherited methods into the classes, which is quite messy

Sunnyxpected avatar Jul 02 '20 07:07 Sunnyxpected

@Sunnyxpected We have limited support for this scenario, but if you trigger the tests at the inherited class, the methods from the super class should also get run and appear in the test report. Could you provide more information about the problem you have?

jdneo avatar Jul 03 '20 08:07 jdneo

i have lets say a LoginAbstract class, which contains the Test Method. then i have a class called LoginFooTest which inherits said abstract class, but overrides an abstract method called getUser. if i then run the tester on the class, my expected result (from past working with eclipse) is that the test runner runs the inherited methods, however he doesnt

My current workaround is to just copy in all the test methods from the parent

Sunnyxpected avatar Jul 03 '20 08:07 Sunnyxpected

@Sunnyxpected Seems some words are missing in the comments.

Do you mean you want to run the methods in the super type and hope to get the results for all the inherited ones?

jdneo avatar Jul 04 '20 02:07 jdneo

yes there is test methods in the super type that i want to run in the subtype which inherits those.

Sunnyxpected avatar Jul 14 '20 14:07 Sunnyxpected

As I see this issue do not updated for a long time.

I created a minimal repo to reproduce the bug. Hope it can help to solve this issue.

https://github.com/mic4126/vscode_java_test_abstract_class_bug

mic4126 avatar Jun 03 '21 04:06 mic4126

Any movement or update on this? This is a pretty common scenario for decently sized java projects.

cmitchell avatar Jan 26 '22 16:01 cmitchell

Seems to be duplicate or releated to #718.

This makes it almost impossible to switch from eclipse to vscode. We are not able to execute most of our testclasses because of this bug.

benzman81 avatar Feb 21 '22 11:02 benzman81

@cmitchell Are you using TestNG?

jdneo avatar Feb 22 '22 06:02 jdneo

We're using VsCode's Test Runner for Java plugin v0.35.2022070202 plugin, and are seeing the same issue. A test case that is located in a parent class and annotated with "@Test", does not show up in the test case sequence as the first test.

The same setup in Eclipse does work correctly.

@microsoft - are you planning to address this issue?

vadiml77 avatar Jul 11 '22 19:07 vadiml77

@vadiml77 do you mean the case from parent class appears in the test explorer but not at the first place?

jdneo avatar Jul 12 '22 01:07 jdneo

@jdneo I mean that the test case from the parent class does NOT appear in the test explorer at all. For example as setup below, the vscode explorer shows TEST01() as the first test, and does not show/execute TEST() at all. However, in the Eclipse, the explorer does show/execute TEST() as the very first test.

public class ChildClass extends ParentClass { @Test(enabled = true, priority = 1) public void TEST01() { ... } ... }

public class ParentClass { @Test(enabled = true) public void TEST() { ... } }

vadiml77 avatar Jul 12 '22 03:07 vadiml77

I guess you are using TestNG, which currently does not support well for inherited tests. JUnit should be fine though.

jdneo avatar Jul 12 '22 03:07 jdneo

@jdneo yes, we're using TestNG. Is there a roadmap for having inherited tests supported in VSCode to match the level of support Eclipse provides? This is a requirement for large sized software

vadiml77 avatar Jul 12 '22 05:07 vadiml77

@jdneo No, we are using both junit 4 and 5 at the moment, but hopefully soon only junit 5 (jupiter). Zero testNG

cmitchell avatar Jul 12 '22 10:07 cmitchell

I guess you are using TestNG, which currently does not support well for inherited tests. JUnit should be fine though.

@jdneo testng works very well for inherited classes and eclipse supports this, too. This is a major lack in vscode support for testng.

@vadiml77 I feel your pain, we have the same issue and habe to switch to maven command line test runs for this.

benzman81 avatar Jul 15 '22 20:07 benzman81