Running Maven Defaults and Overriding with TestNG
2015-09-07 15:38
357 查看
TAG ARCHIVES: RUNNING MAIN METHOD FOR TEST CLASSES IN MAVEN
Running
Maven Defaults and Overriding with TestNG
Posted on September27, 2013
The standard for any team, maintaining a Maven project (or similar for any project), is that it should be downloadable to a users local environment, and the capability to run “mvn clean”, “mvn compile”, “mvn test” within an hour. This includes having to install
Java and Maven if necessary. No assumptions on the given environment.
Now, if you want to configure testNG to run with .xml files, you can use the suiteXmlFiles tag in the pom.xml file
as discussed in Notes
on Using Maven and TestNG, but then you will have to include a TestNG xml file. This is bad. The way you get new developers to download and run your code quickly is by having few steps and no configurations. Defaults should be provided to get them to run
your code as quickly as possible. It is okay if the code doesn’t do exactly what it was intended to do. Once they see the main purpose of your code, as a simple user, you can inspire them to learn how to configure to be an advanced user.
The way to get around this issue with TestNG is to give all your tests the TestNG annotation, turn them all to false, except the one you actually want to run by default. Those are set to true.
public class TestNGEntryPoint { public static void main(String[] args) { System.out.println("main start"); try { String classToRun; String methodToRun; if (args.length == 2) { classToRun = args[0]; methodToRun = args[1]; } else { classToRun = MyFirstTest.class.getName(); methodToRun = MyFirstTest.class.getMethod( "testFirstTest", new Class[0]).getName(); } new TestNGEntryPoint(classToRun, methodToRun); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("main finish"); } public TestNGEntryPoint(String className, String methodName) { // Create Suite List List suites = new ArrayList(); // Add Suite to Suite List XmlSuite suite = new XmlSuite(); suites.add(suite); suite.setName("MyTestSuite"); // Add Test to Suite XmlTest test = new XmlTest(suite); test.setName("MyTest"); // Add Class List to Test List classes = new ArrayList(); test.setXmlClasses(classes); // Add Class to Class List XmlClass clazz = new XmlClass(className); classes.add(clazz); // Run TestNG TestNG testNG = new TestNG(); testNG.setXmlSuites(suites); testNG.addListener(new TestNGAnnotationTransformer(methodName)); testNG.run(); } public static class TestNGAnnotationTransformer implements IAnnotationTransformer{ String methodToRun; public TestNGAnnotationTransformer(String methodName) { methodToRun = methodName; } public void transform(ITestAnnotation annotation, Class arg1, Constructor arg2, Method testMethod) { if (methodToRun.equals(testMethod.getName())) { annotation.setEnabled(true); } } } }
Add a main method in MyFirstTest that looks like the following:
package com.mycompany.myapp; public class MyFirstTest { public static void main (String[] args) { new TestNGEntryPoint(MyFirstTest.class.getName(), "testFirstTest"); } @Test(enabled=false) public void testFirstTest()
You can then run these tests from maven directly using:
mvn exec:java -Dexec.classpathScope="test" -Dexec.mainClass="com.mycompany.myapp.MyFirstTest"
Just as another sort of pet peeve, the way the TestNG object is build should be capable of building top down or bottom up. It is always good to have standard behaviors. For instance, since the line “new XmlTest(suite)” uses the suite to configure several defaults
for the XMLTest, this is great. However, you should also be able to do new “XmlSuite(suites)” or new XmlClass(classes). There should be the ability to build bottom up from class to test to suite or to build top down from suite to test to class.
Working with Maven and TestNG
My final recommendation is not to use the xml tag in the pom.xml file torun TestNG with an XML file. Rather, leave “mvn test” running a default set of tests, which will be all classes in your src/test/java directory which end with “Test” and within those tests, all methods annotated with “@Test” annotation.
Then, run additional tests with “mvn test -Dtest=MySecond” or Right Click->Run As>TestNG Test. You can also programmatically build up a test suite to run as shown previously.
Posted
in Uncategorized
| Tagged Configuring
TestNG for a one click build, Running
main method for test classes in maven | Leave
a reply
相关文章推荐
- javascript canvas 碰撞检测
- 前端开发 Grunt 之 Connect
- 【工具推荐】LICEcap –GIF 屏幕录制工具
- StringBuilder的实现与技巧
- mysql 远程链接
- DOS命令
- leetcode:Substring with Concatenation of All Words
- QT事件(3)
- 黑马程序员——Objective-C之特殊语法总结
- 生产消费模式
- jQuery编程规范与最佳实践(附带一些个人的笔记)
- require.js优化器
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Hibernate学习总结(一)
- 使用Haproxy搭建web群集
- html5 audio video output
- python标识符
- mobileconfig文件的签名和认证(signed、verified)
- 多线程环境下生成随机数
- maven核心,pom.xml详解