maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Chen <pdc...@gmail.com>
Subject 'Running a Single Test' feature does not work as expected (for testng provider)
Date Sun, 07 Apr 2013 12:32:42 GMT
Feature link
http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

Issue description: Method with @AfterTest annotation are never been called
if use the 'Running a Single Test' feature for testng.

I created a very simple project to reproduce this issue.
https://github.com/pdckxd/test-run-single-case.git

There are two classes in it.
BaseTest   # only has one method cleanUp with @AfterTest. it just prints
out a string 'clean up'
MyTest      # inherit from BaseTest (cases: testPlus, testSubtract,
testMultiplication)

1).Cmd:     mvn test
Result:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: TestNG652Configurator
clean up   <==================================== Right @AfterClass method
outputs
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.322 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 1.662s
[INFO] Finished at: Sun Apr 07 20:21:41 CST 2013
[INFO] Final Memory: 7M/244M
[INFO]
------------------------------------------------------------------------


2).Cmd:     mvn test -Dtest=MyTest#testPlus
Result:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running MyTest
Configuring TestNG with: TestNG652Configurator
<==================================== where is 'clean up'
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.292 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 1.641s
[INFO] Finished at: Sun Apr 07 20:25:22 CST 2013
[INFO] Final Memory: 7M/244M
[INFO]
------------------------------------------------------------------------

I find a workaround to resolve this problem. But I don't know how to file
this issue to JIRA issue tracking system so I just attach the patch here.

diff --git
a/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
b/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
index 3c06bca..c324744 100644
---
a/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
+++
b/surefire-providers/surefire-testng-utils/src/main/java/org/apache/maven/surefire/testng/utils/MethodSelector.java
@@ -19,12 +19,14 @@
  * under the License.
  */

+import java.lang.reflect.Field;
 import java.util.List;
 import org.apache.maven.shared.utils.io.SelectorUtils;

 import org.testng.IMethodSelector;
 import org.testng.IMethodSelectorContext;
 import org.testng.ITestNGMethod;
+import org.testng.internal.BaseTestMethod;

 /**
  * For internal use only
@@ -58,8 +60,29 @@ public boolean includeMethod( IMethodSelectorContext
context, ITestNGMethod test
         {
             return true;
         }
-
-        return SelectorUtils.match( METHOD_NAME,
testngMethod.getMethodName() );
+
+        boolean result = SelectorUtils.match( METHOD_NAME,
testngMethod.getMethodName() );
+
+        // fix that methods with @afterClass annotation are never be
invoked if running
+        // specific test cases using the feature of 'run a single test' of
maven-surefire-plugin
+        // mvn -Dtest=TestCircle#mytest test
+
+        if(!result)
+        {
+            Class<BaseTestMethod> clazz = BaseTestMethod.class;
+
+            Field field = null;
+     try {
+     field = clazz.getDeclaredField("m_enabled");
+     field.setAccessible(true);
+     field.set(testngMethod, false);
+     } catch (Exception e) {
+     e.printStackTrace();
+     }
+            field.setAccessible(false);
+        }
+
+        return result;
     }

     public static void setMethodName( String methodName )


Thanks
Best regards
Alex Chen

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message