[GRADLE-1189] TestNG @BeforeTest does not work as expected for groovy plugin and useTestNG() Created: 26/Oct/10  Updated: 04/Jan/13  Resolved: 24/Nov/10

Status: Resolved
Project: Gradle
Affects Version/s: 0.9
Fix Version/s: 0.9-rc-2

Type: Bug
Reporter: Sargis Harutyunyan Assignee: Hans Dockter
Resolution: Fixed Votes: 0

Attachments: XML File testng-results.xml    

 Description   

Hi I have following build.gradle:

apply plugin: 'idea'
apply plugin: 'code-quality'
apply plugin: 'groovy'

sourceCompatibility = 1.6

repositories {
mavenCentral()
}

dependencies {
groovy group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.7.5'
groovy group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1'

testCompile group: 'org.testng', name: 'testng', version: '5.14.1'
testRuntime group: 'ch.qos.logback', name: 'logback-core', version: '0.9.26'
testRuntime group: 'ch.qos.logback', name: 'logback-classic', version: '0.9.26'
}

test {
useTestNG()
}

and here is my tests:

class BeforeTestBugTest {

List<String> testList

@BeforeTest
public void setUp()

{ testList = new ArrayList<String>() }

@Test
public void testNumber01() throws Exception

{ testList.add("Some String") Assert.assertEquals(testList.size(), 1) }

@Test
public void testNumber02() throws Exception

{ testList.add("Another String") Assert.assertEquals(testList.size(), 1) }

}

here is test result:

<test-method status="FAIL" signature="testNumber02()" name="testNumber02" duration-ms="0" started-at="2010-10-26T21:58:05Z" finished-at="2010-10-26T21:58:05Z">
<exception class="java.lang.AssertionError">
<message>
<![CDATA[expected:<1> but was:<2>]]>
</message>
<full-stacktrace>
<![CDATA[java.lang.AssertionError: expected:<1> but was:<2>
at org.testng.reporters.JUnitReportReporter.generateReport(JUnitReportReporter.java:70)
at org.testng.TestNG.generateReports(TestNG.java:932)
at org.testng.TestNG.run(TestNG.java:912)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.stop(TestNGTestClassProcessor.java:95)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:32)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:23)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:75)
at $Proxy3.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:32)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:23)
at org.gradle.messaging.remote.internal.MethodInvocationUnmarshallingDispatch.dispatch(MethodInvocationUnmarshallingDispatch.java:48)
at org.gradle.messaging.remote.internal.MethodInvocationUnmarshallingDispatch.dispatch(MethodInvocationUnmarshallingDispatch.java:25)
at org.gradle.messaging.dispatch.DiscardOnFailureDispatch.dispatch(DiscardOnFailureDispatch.java:31)
at org.gradle.messaging.dispatch.AsyncDispatch.dispatchMessages(AsyncDispatch.java:131)
at org.gradle.messaging.dispatch.AsyncDispatch.access$000(AsyncDispatch.java:35)
at org.gradle.messaging.dispatch.AsyncDispatch$1.run(AsyncDispatch.java:71)
at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:63)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
]]>
</full-stacktrace>
</exception>
</test-method>

You can find ass attached file full test result.

Also something interesting in my test result:

<test-method status="PASS" signature="setUp()" name="setUp" is-config="true" duration-ms="14" started-at="2010-10-26T21:58:04Z" finished-at="2010-10-26T21:58:05Z">

as I understand my method annotated with @BeforeTest running as simple, test but not sure. Thanks in advaance.



 Comments   
Comment by Sargis Harutyunyan [ 26/Oct/10 ]

I checked for java project with ant with the same TestNG version it works fine

Comment by Adam Murdoch [ 26/Oct/10 ]

According to the TestNG documentation, methods marked @BeforeTest are run once before TestNG executes any test methods. Methods marked @BeforeTest are not run before each test method. You need to use @BeforeMethod for that. The relevant documentation can be found here: http://testng.org/doc/documentation-main.html#annotations

You might also have a look at http://jira.opensymphony.com/browse/TESTNG-223. It describes almost exactly the same situation you describe above. It is marked as 'not a problem'.

So, this seems to be a bug with the Ant tasks, rather than Gradle.

Comment by Sargis Harutyunyan [ 27/Oct/10 ]

H, you are absolutely right, I should read more carefully TestNG doc instead of rely on Ant results , one more reason to migrate to Gradle.

Comment by Sargis Harutyunyan [ 27/Oct/10 ]

Probably problem not in ANT also bacause for ANT I used:

<suite name="UnitTestsSuite" verbose="2" parallel="false">
<test name="FCVRServerUnitTests">
<packages>
<package name="some.package.*"/>
</packages>
</test>
</suite>

And with this config @BeforeTest was ok. Anyway thanks once more

Generated at Wed Jun 30 11:49:14 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.