Running Maven Defaults and Overriding with TestNG
来源:互联网 发布:淘宝客服是怎么做的 编辑:程序博客网 时间:2024/06/06 15:37
TAG ARCHIVES: RUNNING MAIN METHOD FOR TEST CLASSES IN MAVEN
Running Maven Defaults and Overriding with TestNG
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 to run 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.
- Running Maven Defaults and Overriding with TestNG
- MyBatis Generator Running with Maven
- up and running with cassandra
- up and running with cassandra
- up and running with cassandra
- Up and Running with C++
- tips for using maven and testng/junit
- Up and Running with Houdini ( lynda--H14)
- Overriding new and delete
- Overriding and Hiding Methods
- Overriding and Hiding Methods
- Overloading And Overriding
- Up and running with Kubernetes.io and Raspberry Pis
- testng翻译之四--Running TestNG
- TestNG-Excute selenium with TestNG.
- Method of Overriding and Overloading
- Compiling and Running a Java Program with a Native Method
- Understanding FastCGI and “Gotchas” with Running Native Code
- Android百分比布局使用小坑一个
- JS----拖拽图层,面向过程,面向对象
- Snail—iOS开发学习博客及网站
- 百度地图的封装
- git入门
- Running Maven Defaults and Overriding with TestNG
- C++实现Windows下的Daemon,监控多个进程
- 装修的注意事项
- TraceView工具的使用说明
- c++ 中 `++i` 与 `i++` 在运算表达式中的优先级
- codecombat安息之云山峰11-21关及沙漠38关代码分享
- 【转】c++中vector的用法
- echarts简单实例+ $.extend()方法解析
- 欢迎使用CSDN-markdown编辑器