没有 main() 方法的所有注解如何在 TestNg 中工作 [英] How does all annotations work in TestNg without main() method

查看:36
本文介绍了没有 main() 方法的所有注解如何在 TestNg 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我还有一个疑问.selenium Webdriver 和 TestNG 组合是否需要 main() 方法来执行脚本?或者我们可以在没有 main() 方法的情况下执行测试用例吗?如果我们可以在没有 main() 的情况下执行测试用例,那怎么可能?

package com.first.example;导入 org.testng.annotations.Test;公共类演示一个{@测试public void firstTestCase(){System.out.println("我在 demoOne 类的第一个测试用例中");}@测试public void secondTestCase(){System.out.println("我在 demoOne Class 的第二个测试用例中");}}

解决方案

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

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

正如在这个答案中正确指出的:https://stackoverflow.com/a/1918154/3619412><块引用>

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

此外,测试中没有必要使用 main() 方法,但是如果需要,您可以使用 main() 方法来运行 TestNg 测试.请参阅.

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?

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?

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");
    }
}

解决方案

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.

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 documentation to get the idea how it happens.

As rightly pointed out in this answer: 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.

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() 方法的所有注解如何在 TestNg 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆