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

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

问题描述

我对使用Java的TestNG有疑问.我完全不熟悉TestNG.我的疑问是,在没有main()方法的情况下,如何在Java中使用TestNG执行所有测试用例?如果您有任何想法请给我建议.以下代码是在Java中使用TestNG的示例测试用例的示例.但是,如果您注意到了,您会发现一件事,即代码中没有main()方法.然后,测试用例如何执行?

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?

我还有一个疑问.硒Webdriver和TestNG组合执行脚本是否需要main()方法?还是可以在没有main()方法的情况下执行测试用例?如果我们可以在没有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?

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()方法,而是使用注解.

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.

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

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.

此答案正确指出: https://stackoverflow.com/a/1918154/3619412

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

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

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测试.参见.

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天全站免登陆