Re: [testng-users] Before and After for groups.

130 views
Skip to first unread message
Message has been deleted

Cédric Beust ♔

unread,
Sep 16, 2008, 11:20:59 AM9/16/08
to testng...@googlegroups.com
Hi Aleksey,

It looks like this was run without including any groups.  Could you try your example with TestNG from the command line and a testng.xml that includes group "1" and let us know if you see the same result?

If you do, it's a bug in TestNG, if you don't, the problem is probably in the IDEA plug-in.


--
Cédric



On Tue, Sep 16, 2008 at 12:53 AM, lookout <alekse...@gmail.com> wrote:

Hello all.
Yesterday, I have created  next test class:

public class SimpleTest {
   private static Log log = LogFactory.getLog(SimpleTest.class);

   @BeforeTest(groups = {"1"})
   public void beforeTest() {
       log.info("BeforeTest");
   }

   @BeforeClass(groups = {"1"})
   public void beforeClass() {
       log.info("BeforeClass");
   }

   @BeforeMethod(groups = {"1"})
   public void beforeMethod() {
       log.info("BeforeMethod");
   }

   @Test(groups = {"1"})
   public void test1() {
       log.info("Test 1");
   }

   @Test(groups = {"2"})
   public void test2() {
       log.info("Test 2");
   }

   @AfterMethod(groups = {"1"})
   public void afterMethod() {
       log.info("afterMethod");
   }

   @AfterClass(groups = {"1"})
   public void afterClass() {
       log.info("afterClass");
   }

   @AfterTest(groups = {"1"})
   public void afterTest() {
       log.info("afterTest");
   }
}

As I can understand, I specified all Before and After methods to
execute  before tests from group "1". All this methods are belong to
group "1" by using parameter "groups".

And I supposed to have next result of test run without specifying
groups (from IntelliJ Idea, just test run):

BeforeTest
BeforeClass
BeforeMethod
Test 1
afterMethod
afterClass
afterTest
Test2

But I got another one:

BeforeTest
BeforeClass
BeforeMethod
Test 1
afterMethod
BeforeMethod
Test 2
afterMethod
afterClass
afterTest

As I can understand, all before and after methods specified for
group(s), must be executed only before or after tests from the same
group. In my example, nothing must be executed before and after test2,
because it's from the group "2", but all before and after methods are
from the group "1". Am I right? Or may be I understand TestNG not
right.
But! If I have run test with specifying running group, I have got good
result!
Run only group "1" log is next:
BeforeTest
BeforeClass
BeforeMethod
Test 1
afterMethod
afterClass
afterTest

And log of run only group "2" is next:
Test 2

I have already confused. Please, help me. Why TestNG sometimes don't
follow group specification? Is it possible to execute before and after
methods only for necessary groups?

Environment:
TestNG 5.8
IntelliJ IDEA 7.0.4 and 8M1
Maven 2.0.9 (Surfire 2.4.3) - Maven give the same result as IDEA.
Java 1.6.

Best regards,
Aleksey Didik.








Message has been deleted

lookout

unread,
Sep 17, 2008, 5:32:36 AM9/17/08
to testng-users
Hello Cedric,
thank you for so fast response.

Yes, It was run without including any groups.
I have made new test. I have prepare next class:

public class SimpleNGTest {

//Before methods
@BeforeMethod(groups = {"A"})
public void beforeMethodA() {
System.out.println("Before method for group A");
}

@BeforeMethod(groups = {"B"})
public void beforeMethodB() {
System.out.println("Before method for group B");
}

// Tests
@Test(groups = {"A"})
public void testA() {
System.out.println("Test from group A");
}

@Test(groups = {"B"})
public void testB() {
System.out.println("Test from group B");
}

//after methods
@AfterMethod(groups = {"A"})
public void afterMethodA() {
System.out.println("After method for group A");
}

@AfterMethod(groups = {"B"})
public void afterMethodB() {
System.out.println("After method for group B");
}
}

I have run this test from Idea's plugin without including any groups,
just right click on this class and select "Run SimpleNGTest" and have
got next result into console:

Before method for group B
Before method for group A
Test from group A
After method for group B
After method for group A
Before method for group B
Before method for group A
Test from group B
After method for group B
After method for group A

Ok, next step I have tried to run tests with Maven. I have used
Surfire plugin.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<disableXmlReport>true</disableXmlReport>
</configuration>
</plugin>


This means, that I don't specify groups for TestNG, and tests will be
run without including any groups. Am I right?
I have run test phase and I have got next in console:

Before method for group B
Before method for group A
Test from group A
After method for group B
After method for group A
Before method for group B
Before method for group A
Test from group B
After method for group B
After method for group A


It was the same result as for Idea plugin.
To be sure, I have made the same test for command line with using
testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >
<test name="OneClass">
<classes>
<class name="com.edg.earth.SimpleNGTest"/>
</classes>
</test>
</suite>


And I have got this result:

c:\JAVA>java -cp testng-5.8-jdk15.jar;./ -Dtestng.test.classpath="c:
\Java" org.testng.TestNG testng.xml
[Parser] Running:
C:\JAVA\testng.xml

Before method for group B
Before method for group A
Test from group A
After method for group B
After method for group A
Before method for group B
Before method for group A
Test from group B
After method for group B
After method for group A

===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================


It's the same result as I have before. And in my opinion, problem not
in TestNG and not in Idea, problem is in my understanding of TestNG
principles.
If I have started tests without <u>specifying groups</u>, I will get
result which I have got in previous experiments.

In my opinion, If I specify before method as belonged to group "A",
this method have to be executed only before tests from group "A" and
never else, independently from run with including groups or not. I
must ever have got next log:

Before method for group A
Test from group A
After method for group A
Before method for group B
Test from group B
After method for group B


But, as I can understand, when I don't specify executed groups, before
and after methods are executed independently from group specifying.
<br>

After I have modified my testng.xml and include groups A and B:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Main" verbose="1" >
<test name="Group A">
<groups>
<run>
<include name="A"/>
<include name="B"/>
</run>
</groups>
<classes>
<class name="com.edg.earth.SimpleNGTest"/>
</classes>
</test>
</suite>


But I have got the same result:

Before method for group B
Before method for group A
Test from group A
After method for group B
After method for group A
Before method for group B
Before method for group A
Test from group B
After method for group B
After method for group A

I have fix this problem the next way. I have wrote in testng.xml,
please, run two tests, first for group A, second for group B. You
could find it below:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Main" verbose="1" >
<test name="Group A">
<groups>
<run>

<include name="A"/>
</run>
</groups>
<classes>
<class name="com.edg.earth.SimpleNGTest"/>
</classes>
</test>
<test name="Group B">
<groups>
<run>
<include name="B"/>
</run>
</groups>
<classes>
<class name="com.edg.earth.SimpleNGTest"/>
</classes>
</test>
</suite>


In this case, I have got the next log:

c:\JAVA>java -cp testng-5.8-jdk15.jar;./ -Dtestng.test.classpath="c:
\Java" org.testng.TestNG testng.xml
[Parser] Running:
C:\JAVA\testng.xml

Before method for group A
Test from group A
After method for group A
Before method for group B
Test from group B
After method for group B

===============================================
Main
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Yes! It's what I need!
I can use It for Maven or Ant test task for my project, because I
could specify necessary testng.xml, but in case of Idea one click run,
I have no vision, how to get what I need.

Sorry for so big mail, but I want to explain my problem fully.

If TestNG will execute before and after methods only for tests from
their groups independently from run including groups or not, it will
be very useful for my project. It could allow to write in a test,
"this methods will be executed before tests from necessary group and
never else". It could allow me to run different configuration of tests
but be sure, that wrong DB configuration never will be created before
tests, which need another one. It will be very useful to specify two
groups of tests, every might have self environment initialized in self
@BeforeTest method, and when I will run tests from both groups
simultaneously, I can be sure that first environment will be
initialized, tests from first groups will be executed, after second
environment will be initialized and tests from second groups will be
executed.
I have created briefly diagram of my suggestion (Picase web link).
http://lh6.ggpht.com/aleksey.didik/SNDFgsI3qpI/AAAAAAAAACo/NJV846acv-I/s720/TestFramework.jpg"

Wait for your response,
Best regard, Aleksey.


On 16 сент, 20:20, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi Aleksey,
>
> It looks like this was run without including any groups.  Could you try your
> example with TestNG from the command line and a testng.xml that includes
> group "1" and let us know if you see the same result?
>
> If you do, it's a bug in TestNG, if you don't, the problem is probably in
> the IDEA plug-in.
>
> --
> ***Cédric
> *

Mark Derricutt

unread,
Sep 17, 2008, 5:43:12 AM9/17/08
to testng...@googlegroups.com
If I'm reading all this correctly, its a bug in the IDEA plugin, and possibly also the maven plugin.

When you just say "run this class" the IDEA plugin generates an XML file for the suite to run, the XML file (found in your ~/.IntelliJIdea70/system directory, or equivalent) is run by TestNG.

Is the XML being generated badly in this case?



> If you do, it's a bug in TestNG, if you don't, the problem is probably in
> the IDEA plug-in.


--
"It is easier to optimize correct code than to correct optimized code." -- Bill Harlan

lookout

unread,
Sep 17, 2008, 6:20:53 AM9/17/08
to testng-users
No, I think it's not a bug of Idea plugin.
TestNG command line, Idea and Maven make the same, when I don't
specify groups for run.

Below you can find temp-testng-customsuite.xml, which was created by
Idea for my test class.
It's the almost the same, as I have created for command line test.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Custom suite">
<test verbose="1" name="earth" annotations="JDK">
<classes>
<class name="com.edg.earth.SimpleNGTest"/>
</classes>
</test>
</suite>

I think, it is TestNG feature. It run before and after methods
independently from groups, if we run without groups including.
Reply all
Reply to author
Forward
0 new messages