-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expected behavior of annotated methods on an @Test(groups="mygroup") annotated class #2366
Comments
|
Sure, I have created a SB starter app configured in a similar way to my application here: https://github.com/cardonator/demo When you right click and run the You can also observe the same behavior if you run Let me know if you need additional information. |
Here's a much more simpler example that can be used to reproduce the problem. import java.lang.reflect.Method;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test(groups = "acceptance")
public class SampleTestCase {
@BeforeClass
public void setupOrgMatching() {
System.err.println("Running @BeforeClass");
}
@BeforeMethod(alwaysRun = true)
public void beforeMethod(Method method) {
System.err.println("Running @BeforeMethod for " + method.getName());
}
@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult result) {
System.err.println("Running @AfterMethod for " + result.getMethod().getQualifiedName());
}
@AfterClass(alwaysRun = true)
public void cleanup() {
System.err.println("Running @AfterClass");
}
@Test
public void testGetHeartbeat() {
System.err.println("Running test method testGetHeartbeat()");
}
@Test(groups = "fragile")
public void testGetHeartbeatSkipped() {
System.err.println("Running test method testGetHeartbeatSkipped()");
}
} Suite xml file <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="2366_suite" verbose="2">
<test name="2366_test">
<groups>
<run>
<include name="acceptance"/>
</run>
</groups>
<classes>
<class name="com.rationaleemotions.github.issue2366.SampleTestCase"/>
</classes>
</test>
</suite> Output
Analysis The configuration method is ending up inheriting the groups defined at the class level defined Quoting the java docs
I think TestNG is working as designed here in terms of the published behavior. @cardonator - In your case, you might want to explicitly set Let me know if that explains the problem and if this can be closed out. |
Thanks @krmahadevan for attempting to research into this issue! I actually don't think your simpler test is exhibiting the same problem as my
As you can see from the output, the issue isn't that the annotated functions are inheriting the group and thus running as part of the test (they should be run as part of the group because they are necessary to successfully run the tests in the rest of the class), the issue is that the functions annotated with I did try your suggestion and observed the behavior I expected to see: |
@cardonator - I noticed that your pom says need JDK11. TestNG is not officially yet certified to be compliant with JDK11. I have JDK8. So I altered the pom file to use JDK8 and also to use TestNG So can you try again with JDK8 and post back your results ? Also if you are running with JDK11, please add
|
Hmm, okay. Thanks for letting me know. I assumed that TestNG was expected to work in 11 since the docs say it requires JDK 8+. Since 6.14.3 works in JDK 11, I guess I'll just stick with that until 11 support is improved in the 7.x branch. I tried increasing verbosity to collect more information but I didn't seem to get any additional output. |
@cardonator - Before concluding on the role of JDK11, can you please try once at your side with JDK8 and post back your results ? When I get some time over the weekend, I will try to simulate this issue with JDK11 to see what may be causing this. |
@cardonator - I have tried this on JDK11 (with TestNG krmahadevan@hades:~/githome/demo(master)$ mvn --version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.4, vendor: Oracle Corporation, runtime: /home/krmahadevan/jdk-11.0.4
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-47-generic", arch: "amd64", family: "unix" [INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ demo-integration ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Starting EndpointsTest...
Begin testGetHeartbeat...
End testGetHeartbeat
Finished EndpointsTest in 868 ms
FAILED: testGetHeartbeat
javax.ws.rs.ProcessingException: java.net.ConnectException: Connection refused (Connection refused)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:261)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:296)
at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:623)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:621)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:404)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:300)
at com.example.demo.integration.util.DemoClient.get(DemoClient.java:53)
at com.example.demo.integration.util.DemoClient.heartbeat(DemoClient.java:73)
at com.example.demo.integration.suite.EndpointsTest.testGetHeartbeat(EndpointsTest.java:38)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1248)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:367)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:259)
... 27 more
===============================================
Demo Integration Tests
Tests run: 1, Failures: 1, Skips: 0
===============================================
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.424 s <<< FAILURE! - in TestSuite
[ERROR] testGetHeartbeat(com.example.demo.integration.suite.EndpointsTest) Time elapsed: 0.763 s <<< FAILURE!
javax.ws.rs.ProcessingException: java.net.ConnectException: Connection refused (Connection refused)
at com.example.demo.integration.suite.EndpointsTest.testGetHeartbeat(EndpointsTest.java:38)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at com.example.demo.integration.suite.EndpointsTest.testGetHeartbeat(EndpointsTest.java:38)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] EndpointsTest.testGetHeartbeat:38 » Processing java.net.ConnectException: Conn...
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.393 s
[INFO] Finished at: 2020-09-19T20:14:17+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test (default-test) on project demo-integration: There are test failures.
[ERROR]
[ERROR] Please refer to /home/krmahadevan/githome/demo/demo-integration/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
krmahadevan@hades:~/githome/demo(master)$ |
Hi @krmahadevan, I'm not sure how to explain the difference in what is happening. I can reproduce the issue consistently across multiple environment and machines.
I can reproduce this even in IntelliJ (Community, 2020.2). Only when running the Suite XML. I cloned to a new machine and ran:
and this is the output I get:
Again, thanks so much for looking into this issue. I know sometimes issues like this can be the most frustrating. |
I see a difference between you:
|
I am running the OpenJ9 compiler on one of my machines but have also seen the same behavior with the hotspot compiler as well. |
I managed to reproduce the problem. See wiki page for this issue for details. TestNG won't pass value to your
One more difference is: It seems something wrong in 7.1.0. I haven't try other versions. |
@WengM so first, these function definitions work on 6.x so I think they are right but open to suggestions if they are wrong in 7.x. Second, isn't that the purpose of the annotations to indicate the behavior of those methods when testng parses them before and after each FYI, the behavior on 6 is that the test class is picked up in the group properly and any testng annotated functions are not run as tests. |
@cardonator And 7.0.0 has the same test result with 7.1.0. I will not update on the wiki page. So, finally, please double check 7.3.0 on your side. I used below JDK version for testing. Didn't run with mvn test command but java command to run TestNG.
|
Ahh, thanks for continuing to look into this. So I tried again on 7.3.0 after basically clearing out my maven cache on the demo project and that does indeed appear to fix the issue! When I was testing this before I was having issues with 7.3.0 testing due to conflicts with the IDE. It's possible that the times I was "testing" with 7.3.0 I was actually somehow still using compiled classes with an earlier version of TestNG included. I really appreciate the help researching and testing. Sorry it turned out to be already resolved! |
@cardonator - I am going ahead and closing off this issue since it turns out to be resolved. |
I have experienced a strange issue in TestNG 7+ that used to work in TestNG 6. Basically I have a suite defined that runs a group of
mygroup
and I have a few test classes annotated with@Test(groups="mygroup")
. These classes also have a defined@BeforeClass
and@BeforeMethod
and@AfterMethod
which arepublic void
.When I run my suite, all of the public functions inside the class are picked up and run as if they had an
@Test
annotation. That includes the public functions that have the above three listed annotations, which all fail with NPEs.So, my question is, what is the expected behavior in this scenario and what should my annotations look like? Here's a very rudimentary code sample of what I have for one of the tests:
TestNG Version
7.0.0, 7.1.0, 7.3.0
Expected behavior
The previous behavior was that the annotated methods would be skipped and any annotated method with a group that shouldn't be run was excluded. (It's worth noting that I am working on an upgrade from TestNG 6.8.8 to 7.x.x and so if the expected behavior has changed that's fine, I'm looking for clarification as this is not expressed anywhere in the docs I could find).
Actual behavior
Any public function, even TestNG annotated, in the Test class is run as a test when the Class itself has an included
groups
tag.Is the issue reproductible on runner?
Test case sample
The text was updated successfully, but these errors were encountered: