在没有 main() 方法的情况下,所有注释如何在 TestNg 中工作
How does all annotations work in TestNg without main() method
我对 Java 的 TestNG 有疑问。我对 TestNG 完全陌生。我的疑问是,在没有 main() 方法的情况下,如何在 java 中使用 TestNG 执行所有测试用例?如果您有任何想法,请建议我。以下代码是 java 中使用 TestNG 的示例测试用例示例。但是如果你注意到,你会发现一件事,代码中没有 main() 方法。那么,测试用例是如何执行的呢?
我还有一个疑问。 selenium Webdriver 和 TestNG 组合执行脚本是否需要 main() 方法?或者我们可以在没有 main() 方法的情况下执行测试用例吗?如果我们可以在没有 main() 的情况下执行测试用例,那怎么可能呢?
package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoOne Class");
}
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoOne Class");
}
}
这是许多测试人员的有效怀疑。因为 运行 Java 程序需要 main() 方法,而在 TestNg 中编写测试时,我们不使用 main() 方法,而是使用注释。
TestNG 中的注释是代码行,可以控制它们下面的方法将如何执行。所以,简而言之,你不需要编写 main() 方法,TestNg 会自己完成。请参阅注释末尾的代码 documentation 以了解它是如何发生的。
正如此答案中正确指出的那样:
Annotations are meta-meta-objects which can be used to describe other
meta-objects. Meta-objects are classes, fields and methods. Asking an
object for its meta-object (e.g. anObj.getClass() ) is called
introspection. The introspection can go further and we can ask a
meta-object what are its annotations (e.g. aClass.getAnnotations).
Introspection and annotations belong to what is called reflection and
meta-programming.
此外,您的测试中不一定要有 main() 方法,但如果需要,您可以使用 main() 方法 运行 TestNg 测试。参考 this.
到 运行 cmd 提示符的脚本我们使用下面的语句,
java org.testng.TestNG testng1.xml
TestNG.java中的main方法class如何接受命令行参数,
public static void main(String[] argv) {
TestNG testng = privateMain(argv, null);
System.exit(testng.getStatus());
}
你没看错。测试用例通过 testng, the testing framework which was inspired from junit without having the main()
method but extensively uses annotations.
执行
注释
根据 Annotations 中的文档,大多数 API 都需要大量样板代码。要编写 Web 服务,您需要提供成对的接口和实现。如果程序可以 装饰 并带有指示哪些方法可以远程访问的注释,则该样板可以由工具自动生成。注解不会直接影响程序语义,但会影响工具和库处理程序的方式,进而影响 运行ning 程序的语义。
TestNG
TestNG是一个简单的基于注解的测试框架,它使用一个标记注解类型来表明一个方法是一个测试方法,应该被测试工具运行。例如:
import org.testng.annotations.Test;
@Test
public void foo() {
System.out.println("With in foo test");
}
正在使用的测试工具如下:
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).getMethods()) {
if (m.isAnnotationPresent(Test.class)) {
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {
System.out.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}
我对 Java 的 TestNG 有疑问。我对 TestNG 完全陌生。我的疑问是,在没有 main() 方法的情况下,如何在 java 中使用 TestNG 执行所有测试用例?如果您有任何想法,请建议我。以下代码是 java 中使用 TestNG 的示例测试用例示例。但是如果你注意到,你会发现一件事,代码中没有 main() 方法。那么,测试用例是如何执行的呢?
我还有一个疑问。 selenium Webdriver 和 TestNG 组合执行脚本是否需要 main() 方法?或者我们可以在没有 main() 方法的情况下执行测试用例吗?如果我们可以在没有 main() 的情况下执行测试用例,那怎么可能呢?
package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoOne Class");
}
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoOne Class");
}
}
这是许多测试人员的有效怀疑。因为 运行 Java 程序需要 main() 方法,而在 TestNg 中编写测试时,我们不使用 main() 方法,而是使用注释。
TestNG 中的注释是代码行,可以控制它们下面的方法将如何执行。所以,简而言之,你不需要编写 main() 方法,TestNg 会自己完成。请参阅注释末尾的代码 documentation 以了解它是如何发生的。
正如此答案中正确指出的那样:
Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object for its meta-object (e.g. anObj.getClass() ) is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g. aClass.getAnnotations). Introspection and annotations belong to what is called reflection and meta-programming.
此外,您的测试中不一定要有 main() 方法,但如果需要,您可以使用 main() 方法 运行 TestNg 测试。参考 this.
到 运行 cmd 提示符的脚本我们使用下面的语句,
java org.testng.TestNG testng1.xml
TestNG.java中的main方法class如何接受命令行参数,
public static void main(String[] argv) {
TestNG testng = privateMain(argv, null);
System.exit(testng.getStatus());
}
你没看错。测试用例通过 testng, the testing framework which was inspired from junit without having the main()
method but extensively uses annotations.
注释
根据 Annotations 中的文档,大多数 API 都需要大量样板代码。要编写 Web 服务,您需要提供成对的接口和实现。如果程序可以 装饰 并带有指示哪些方法可以远程访问的注释,则该样板可以由工具自动生成。注解不会直接影响程序语义,但会影响工具和库处理程序的方式,进而影响 运行ning 程序的语义。
TestNG
TestNG是一个简单的基于注解的测试框架,它使用一个标记注解类型来表明一个方法是一个测试方法,应该被测试工具运行。例如:
import org.testng.annotations.Test;
@Test
public void foo() {
System.out.println("With in foo test");
}
正在使用的测试工具如下:
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).getMethods()) {
if (m.isAnnotationPresent(Test.class)) {
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {
System.out.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}