我想以编程方式调用testng。不蚀插件。
我已经关联了“ testng-6.8.21.jar”并通过eclipse运行,我在以下代码下运行:
import org.testng.TestNG; public class SampCls { public static void main(String[] args) { TestNG test=new TestNG(); } }
低于异常。我如何克服这个例外。
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException at SampCls.main(SampCls.java:12) Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more
更改:
Class cls = Class.forName("TestSuite.TestCases.AddContactHappyPath").getClass(); test.setTestClasses(new Class[] { cls });
通过:
test.setTestClasses(new Class[] { AddContactHappyPath.class });
所有代码是
import org.testng.TestNG; import com.xxx.test.others.AddContactHappyPath; public class SampCls { public static void main(String[] args) throws ClassNotFoundException { TestNG test = new TestNG(); test.setTestClasses(new Class[] { AddContactHappyPath.class }); test.run(); } }
TestNG代码是:
import org.testng.annotations.*; public class AddContactHappyPath { @Test() public void AddContactHappyPathTest() { System.out.println("hello world"); } }
控制台结果:
[TestNG] Running: Command line suite hello world =============================================== Command line suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================