2

TestNGを使用するために、JUnitを使用する次のコードを変更しようとしています。

パブリッククラスAutotestShellRunner{

static Class<?> autotestClass;
static org.junit.runner.Result junitResult; ( I replaced that with org.testng.ITestResult )

public static void main(final String[] args) {
    int rc;
    if (args.length != 2) {
        System.err.println("Usage: AutotestShellRunnerDrive  <TEST_SCENARIO_CLASS_NAME> <TEST_CASE>");
        System.exit(-1);
    }

    final String testsuite = args[0];
    final String testcase = args[1];

    try {
        autotestClass = Class.forName(testsuite);
    } catch (final ClassNotFoundException e) {t
        e.printStackTrace();
        throw new RuntimeException("class" + testsuite + " is not found ", e);
    }
    junitResult = (new JUnitCore()).run(Request.method(autotestClass, testcase));  //Now from what i saw i can use instead of JUnitCore I use TestNG

          The problem is that TestNG.run() is not recieving any arguments that I need to pass such as auttestClass and testcase
          Does anyone have any idea?

          Thanks,
          Nir        
4

2 に答える 2

2

TestNG のプログラム API を使用します。ドキュメントはこちらです

于 2013-02-03T14:55:16.757 に答える
1

以下は役に立ちましたが、クラス内の特定のテストのみを実行することはできません

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA. User: nir Date: 1/30/13 Time: 4:46 PM To change
 * this template use File | Settings | File Templates.
 */
public class AutoTestNGShellRunner {
    static Class<?> autotestClass;



    public static void main(final String[] args) {
        int rc;
        if (args.length != 2) {
            System.err
                    .println("Usage: AutoTestNGShellRunner  <TEST_SCENARIO_CLASS_NAME> <TEST_CASE>");
            System.exit(-1);
        }

        final String testsuite = args[0];
        final String testcase = args[1];

        try {
            autotestClass = Class.forName(testsuite);

        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("class" + testsuite + " is not found ",
                    e);
        }

        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { autotestClass });
        testng.setTestNames(Arrays.asList(testcase));
        testng.addListener(tla);

        testng.run();
        tla.getPassedTests();
        List<ITestResult> failedTestsResults = tla.getFailedTests();
        List<ITestResult> successTestsResults = tla.getPassedTests();

        final int failureCount = failedTestsResults.size();

        if (failureCount == 0) {
            System.out
                    .println(String.format("Test case %s passed", testcase));
            rc = 0;
        } else {
            System.out
                    .println(String.format("Test case %s failed", testcase));
            rc = -1;
        }

        System.exit(rc);
    }
}
于 2013-04-02T08:10:36.137 に答える