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.

Posted in UncategorizedTagged Configuring TestNG for a one click build, Running main method for test classes in maven Leave a reply
0 0
原创粉丝点击