java 没有 main() 方法的所有注解如何在 TestNg 中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31717639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:02:21  来源:igfitidea点击:

How does all annotations work in TestNg without main() method

javaseleniumannotationstestng

提问by Anitha

I have a doubt in TestNG with Java. I am completly new to TestNG. My doubt is, How all the test cases are executing using TestNG in java without having main() method? Please suggest me if you have any ideas. Following code is the example of a sample test case using TestNG in java. But if you notice, you can find one thing that there is no main() method in the code. Then, how does the testcases are executing?

我对 Java 的 TestNG 有疑问。我对 TestNG 完全陌生。我的疑问是,所有测试用例如何在没有 main() 方法的情况下在 java 中使用 TestNG 执行?如果您有任何想法,请建议我。以下代码是在 java 中使用 TestNG 的示例测试用例的示例。但是如果你注意到了,你会发现代码中没有 main() 方法。那么,测试用例是如何执行的呢?

I have another doubt. Is main() method needed for selenium Webdriver and TestNG combination to execute a script? Or can we execute testcases without main() method? If we can execute testcases without main(), then how does it is possible?

我还有一个疑问。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");
    }
}

回答by Manu

This is a valid doubt many testers have. Because the main() method is needed to run the Java program and while writing tests in TestNg we don't use main() method, and we use Annotations instead.

这是许多测试人员都有的合理怀疑。因为运行 Java 程序需要 main() 方法,所以在 TestNg 中编写测试时我们不使用 main() 方法,而是使用注解。

Annotations in TestNG are lines of code that can control how the method below them will be executed. So, in short you don't need to write main() method, TestNg do that by itself. Refer the code at the end in Annotations documentationto get the idea how it happens.

TestNG 中的注解是可以控制它们下面的方法将如何执行的代码行。所以,简而言之,您不需要编写 main() 方法,TestNg 会自行完成。请参阅注释文档末尾的代码以了解它是如何发生的。

As rightly pointed out in this answer: https://stackoverflow.com/a/1918154/3619412

正如此答案中正确指出的那样:https: //stackoverflow.com/a/1918154/3619412

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.

注释是元元对象,可用于描述其他元对象。元对象是类、字段和方法。向对象询问其元对象(例如 anObj.getClass() )称为自省。自省可以更进一步,我们可以询问元对象它的注释是什么(例如aClass.getAnnotations)。自省和注释属于所谓的反射和元编程。

Also, it's not necessary to have main() method in your tests, but you can use main() method to run the TestNg tests if you want. Refer this.

此外,测试中没有必要使用 main() 方法,但如果需要,您可以使用 main() 方法来运行 TestNg 测试。参考这个

回答by Pavan T

to run script from cmd prompt we use below statement,

要从 cmd 提示符运行脚本,我们使用以下语句,

java org.testng.TestNG testng1.xml

java org.testng.TestNG testng1.xml

main method in TestNG.java class how accept the command line argument,

TestNG.java 类中的 main 方法如何接受命令行参数,

 public static void main(String[] argv) {
    TestNG testng = privateMain(argv, null);
    System.exit(testng.getStatus());
  }