java - 在没有 main() 方法的情况下,所有注释如何在 TestNg 中工作

标签 java selenium annotations testng

我对 TestNG with Java 有疑问。我对 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() 方法,而是使用 Annotations。

TestNG 中的注释是代码行,可以控制它们下面的方法将如何执行。所以,简而言之,你不需要编写 main() 方法,TestNg 会自己完成。引用注释最后的代码 documentation了解它是如何发生的。

正如此答案中正确指出的那样: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.

此外,您的测试中不一定要有 main() 方法,但如果需要,您可以使用 main() 方法来运行 TestNg 测试。引用this .

关于java - 在没有 main() 方法的情况下,所有注释如何在 TestNg 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717639/

相关文章:

java - 我如何知道我的代码在执行时间方面是否高效?

java - 查找被注释处理覆盖的方法

google-chrome - 为 Protractor 安装特定的 chromedriver

ruby - 无法使用 selenium hub docker 和 ruby​​ watir 更改 chrome 的默认下载目录

python - 哪一种是在 Django 模型中使用 Python 类型提示的正确方法?

java - 使用 Spring @Value 时如何进行简单的属性验证

java - Spring /hibernate/Oracle : ORA-02289 Sequence Does Not Exist?

java - 如何在struts2.xml文件中声明Generics Action?

java - 将 libxml 添加到 Red Hat 中的 Eclipse 项目

java - 可克隆和 WebDriver ( Selenium )