Afamee
Afamee

Reputation: 5320

Testng dealing with test inheritance

I have (Test-A) fully annotated testng test in one project. Note: this test runs successfully. Then I have another testng test (Test-B) in a different project extending (Test-A). This new (Test-B) does not have any annotations since its extending a class that does. My expectation is that when you run this test (Test-B), it should run the test-cases in the super class in addition to the testcase defineed within it which is the object-oriented way. The problem is that testng does not even recognize it as test since there is no annotation within it. I guess testng annotation processing does not consider super-class's annotation??

Upvotes: 5

Views: 3960

Answers (2)

Zhuiguang Liu
Zhuiguang Liu

Reputation: 520

This may be due to the test running environment and setup in question (which may require more digging), but I was able to solve this for my case (same problem using such an inheritance pattern for TestNG 7) by adding a @Test annotation on the sub-class itself:

public abstract class A {

    @Test
    public void aTest() {

    }
}

@Test
public class B extends A {

}

Upvotes: 0

BoffinBrain
BoffinBrain

Reputation: 6525

(Note, using plain old JUnit 4 here, not TestNG)

Eclipse does appear to look up the class hierarchy for @Test annotations if the parent class is in the same project. The following example worked for me:

public class A {
    @Test public void a() {

    }
}

public class B extends A {

}

When running B as a JUnit Test, it executes a() and passes.

I then created two Eclipse projects, Test A and Test B. I made project B link to project A and repeated the above steps like you did, with class A in project A etc. Now, running class B as a unit test says 'No JUnit tests found.' Then, adding a @Test to class B solves the problem.

So, we can deduce that Eclipse doesn't leave the bounds of the current project when looking for test cases. I think your workaround of adding a single @Test to your class is a reasonable one, although I'm not sure why you would want to do this.

Upvotes: 2

Related Questions