eclipse 如何以编程方式使用 TestNG 运行 Selenium Java 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5457983/
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
How to run Selenium Java tests with TestNG programmatically?
提问by Ripon Al Wasim
I am using Selenium RC with Java using TestNG as Test Framework. I'm using Eclipse as IDE. I want to invoke TestNG from my own program very easily. How can I do that?
我正在使用带有 Java 的 Selenium RC,使用 TestNG 作为测试框架。我使用 Eclipse 作为 IDE。我想很容易地从我自己的程序中调用 TestNG。我怎样才能做到这一点?
采纳答案by Cedric Beust
TheStijn gives a few good directions, although TestMethodWorker() is internal so you shouldn't use it.
TheStijn 给出了一些很好的指导,虽然 TestMethodWorker() 是内部的,所以你不应该使用它。
Based on the question, I'm not even sure the original poster is trying to launch TestNG in a separate process, so the API documentation might be what you're looking for:
基于这个问题,我什至不确定原始海报是否试图在单独的进程中启动 TestNG,因此 API 文档可能就是您要查找的内容:
http://testng.org/doc/documentation-main.html#running-testng-programmatically
http://testng.org/doc/documentation-main.html#running-testng-programmatically
回答by Ripon Al Wasim
My following code in java works nicely:
我在java中的以下代码运行良好:
@Test
public void testTestNGProgramatically(){
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {LoginAuthentication.class, GmailSigninSignout.class});
testng.addListener(tla);
testng.run();
}
You can get the details explanation by visiting the following URL:
您可以通过访问以下 URL 获取详细说明:
http://testng.org/doc/documentation-main.html#running-testng-programmatically
http://testng.org/doc/documentation-main.html#running-testng-programmatically
回答by Stijn Geukens
Have a look at org.testng.remote.RemoteTestNG, this will however require you to write an xml suite for your tests like:
看看 org.testng.remote.RemoteTestNG,但是这将要求您为您的测试编写一个 xml 套件,例如:
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="com...service.UserServiceImplTest"/>
</classes>
</test>
</suite>
Another entry point might be new org.testng.internal.TestMethodWorker(...).run() but you'll have to look at the code to determine the constructor args you need to set.
另一个入口点可能是 new org.testng.internal.TestMethodWorker(...).run() 但您必须查看代码以确定您需要设置的构造函数参数。
Perhaps other, more convenient entry points are available depending on your needs; I suggest to launch some test in debug mode, put a breakpoint in your test and go down the stack.
根据您的需要,也许可以使用其他更方便的入口点;我建议在调试模式下启动一些测试,在你的测试中放置一个断点并进入堆栈。