Reputation: 20934
I'm learning to use TestNG for IntelliJ IDEA 9.
As far as I understand, One way to put a test in a group called name
is to annotate it @Test(group = "name")
. To run a method before each test, annotate it with @BeforeMethod
.
In my test setup I want a method to run before each test only in a particular group. So there is a method beforeA
that runs before each test in group A
, a method beforeB
running before each B
test and so on.
Example code:
public class TestExample
{
@BeforeMethod(groups = "A")
public void beforeA()
{
System.out.println("before A");
}
@BeforeMethod(groups = "B")
public void beforeB()
{
System.out.println("before B");
}
@Test(groups = "A")
public void A1()
{
System.out.println("test A1");
}
@Test(groups = "A")
public void A2()
{
System.out.println("test A2");
}
@Test(groups = "B")
public void B1()
{
System.out.println("test B1");
}
@Test(groups = "B")
public void B2()
{
System.out.println("test B2");
}
}
I expect output like
before A
test A1
before A
test A2
before B
test B1
before B
test B2
but I get the following:
before A
before B
before A
before B
test A2
before A
before B
before A
before B
test B1
===============================================
test B2
===============================================
Custom suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
And IntelliJ IDEA has highlighted all my annotations with the message "Group A is undefined" or "Group B is undefined".
What am I doing wrong?
Upvotes: 3
Views: 10431
Reputation: 1979
As mentioned by TEH EMPRAH @BeforeMethod is not suppose to run before every method of which belongs in the same group as it.
In order to accomplish this you have to configure you testng.xml correctly. For your expected output it should look like so
<suite....>
<test name="A">
<groups>
<run>
<include name="A"/>
</run>
</groups>
<classes>
<class name="...TestExample"/>
</classes>
</test>
<test name="B">
<groups>
<run>
<include name="B"/>
</run>
</groups>
<classes>
<class name="...TestExample"/>
</classes>
</test>
</suite>
Upvotes: 1
Reputation: 2058
@BeforeMethod(groups =...)
is NOT supposed to run BEFORE every method IN A GROUP.
It will run before every method in a class. The difference is, it'll just belong to a particular group, nothing more. See DOCS
Upvotes: 3
Reputation: 31
I asked Intellij to fix it. Please check issue: http://youtrack.jetbrains.net/issue/IDEA-67653 We need to vote for it so JetBrains will fix it
Upvotes: 3
Reputation: 93167
@BeforeMethod
and @AfterMethod
seem broken with groups.Resources :
Upvotes: 19