IntelliJ IDEA + TestNG: Run a method before each test in a group
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?
- The listing isn't in good order, this is intelliJ's fault. Run the test in command line or with maven the order will be correct.
@BeforeMethod
and@AfterMethod
seem broken with groups.- IntelliJ remember groups you used before, if you use a group that isn't remembered yet, the message "Group X is undefined" will be shown. Just pres alt + Enter on an undefined group to remember it.
Resources :
- TestNG bug tracker - BeforeMethod and AfterMethod groups support broken
- TestNG Mailing list - Order of execution for configuration methods involving groups
- TestNG Mailing list - Before and After for groups
- talios.com - New Inspections for the IntelliJ TestNG Plugin
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
@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
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>
精彩评论