我想以编程方式调用testng。不是eclipse插件。
我已关联" testng-6.8.21.jar"并在eclipse中运行,我在代码下面运行:
<!DOCTYPE html>
<html>
<head>
<link type='text/css' rel='stylesheet' href='style.css'>
<title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo "<div class='coin'>H</div>";
}
else {
echo "<div class='coin'>T</div>";
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "<p>There {".$verb."} {".$flipCount."} {".$last."}!</p>";
?>
</body>
获得以下异常。我怎样才能克服这个例外。
import org.testng.TestNG;
public class SampCls
{
public static void main(String[] args)
{
TestNG test=new TestNG();
}
}
答案 0 :(得分:10)
如果您使用 Maven项目,则需要添加此依赖项:
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
班级 com / beust / jcommander / ParameterException 在里面
如果您使用没有Maven的项目,则需要在类路径中添加此jar文件:
jcommander-1.48.jar
上下载此jar文件
答案 1 :(得分:2)
更改:强>
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
===============================================
答案 2 :(得分:0)
正如@sgrillon正确指出的那样,你需要正确的Maven依赖,还需要使用shagin插件(https://maven.apache.org/plugins/maven-shade-plugin)打包一个包含所有Maven依赖项的Uber-jar,以便于执行。
这应该包含在pom.xml
:
...
<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
</dependencies>
...
<plugins>
...
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>runnable</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
构建Maven包后,您将获得常规my-app-1.0-SNAPSHOT.jar
文件和my-app-1.0-SNAPSHOT-runnable.jar
。
这是您应该运行的命令:
$ java -jar my-app-1.0-SNAPSHOT-runnable.jar
您可以使用以下命令进行验证:
$ jar tvf my-app-1.0-SNAPSHOT-runnable.jar
阴影jar包含JCommander类(以及所有其他Maven依赖项),而常规类则不包含。
答案 3 :(得分:0)
对我来说解决方案是安装TestNg。我引用了此链接here。当我使用最新版本的eclipse时,Eclipse MarketPlace选项对我不起作用,如果对您有用,那就太好了。其他帮助->安装新软件并使用http://dl.bintray.com/testng-team/testng-eclipse-release/,安装后请参考我提到的第一个链接以继续。祝一切顺利!