Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG @BeforeMethod method not called when it resides in superclass and a specific group is run

I am trying to use a group to run a subset of tests relevant to what I am working on called "current". The problem is that if I use a superclass to do some setup in a @BeforeMethod, the method runs when I run all tests, but does not when I run with just the group "current" specified.

So when I run all tests, the emptyTest fails because the @BeforeMethod is called, when just running group current, the method is not called. Note: If I add @Test(groups = {"current"}) to the subclass, then it does run - however, it runs all subclasses not labelled with "current" as well, which defeats the purpose of the "current" group.

If there is a better way to accomplish this behavior, I am open to all solutions.

Thanks, Ransom

Superclass:

public class TestNGSuperclass {
    @BeforeMethod
    public void failingToShowThatItIsNotRun() {
        Assert.fail();
    }
}

Subclass:

@Test(groups = {"current"})
public class TestNGCurrentGroup extends TestNGSuperclass {
    public void emptyTest() {}
}

TestNG Configuration:

<test name="current">
    <groups>
        <run>
            <include name="current"/>
        </run>
    </groups>
    <packages>
        <package name="uiowa.wf.test.*"/>
    </packages>
</test>
<test name="all-tests">
    <packages>
       <package name="uiowa.wf.test.*"/>
    </packages>
</test>
like image 889
Ransom Briggs Avatar asked Feb 16 '11 18:02

Ransom Briggs


2 Answers

Your @BeforeMethod needs to be part of the group you are running.

You can also use @BeforeMethod(alwaysRun = true) if you don't want to hardcode the value of your group and if you think you will always want to run this method, regardless of the group you are currently running.

like image 160
Cedric Beust Avatar answered Oct 06 '22 21:10

Cedric Beust


Have you tried @BeforeMethod(groups = {"current"})? I've come to the conclusion that TestNG groups and inheritance don't really work that well.

For example, the above works if you run everything in group current, but not if you run everything other than group current and the base class is used for both groups.

I'm currently refactoring all our test classes to eliminate subclassing and to make use of composition instead.

like image 29
Brian Agnew Avatar answered Oct 06 '22 21:10

Brian Agnew