简体   繁体   English

在TestNG中运行Java单元测试时,无需使用testng.xml

[英]Eliminating the need for testng.xml when running Java unit tests in TestNG

I find the need to maintain and edit testng.xml despite the availability of annotations as a limitation of TestNG. 尽管注释可用作TestNG的限制,但我发现需要维护和编辑testng.xml。 Is it possible to either automate the generation of testng.xml, or completely eliminate the need for it while running unit tests? 是否可以自动生成testng.xml,或者在运行单元测试时完全不需要它?

From testng.org documentation: "You can invoke TestNG from your own programs very easily": 来自testng.org文档:“您可以非常轻松地从自己的程序中调用TestNG”:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();

This example creates a TestNG object and runs the test class Run2. 此示例创建一个TestNG对象并运行测试类Run2。 It also adds a TestListener. 它还添加了一个TestListener。

Ref: 参考:

You can read more about it Here: http://testng.org/ 你可以在这里阅读更多相关信息: http//testng.org/
Here is one Stack Discussion on the simillar topic 这是关于simillar主题的一个Stack讨论
A working example class on Github Github上的一个工作示例类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM