如何以编程方式使用TestNG运行Selenium Java测试?

我使用Selenium RC和Java一起使用TestNG作为测试框架。 我正在使用Eclipse作为IDE。 我想非常轻松地从我自己的程序中调用TestNG。 我怎样才能做到这一点?

虽然TestMethodWorker()是内部的,所以你不应该使用它来提供一些好的方向。

根据这个问题,我甚至不确定原始海报是否试图在单独的流程中启动TestNG,因此API文档可能正是您所寻找的:

http://testng.org/doc/documentation-main.html#running-testng-programmatically

我在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(); } 

您可以访问以下URL获取详细信息说明:

http://testng.org/doc/documentation-main.html#running-testng-programmatically

看看org.testng.remote.RemoteTestNG,但是这需要你为你的测试编写一个xml套件,如:

        

另一个入口点可能是新的org.testng.internal.TestMethodWorker(…)。run()但你必须查看代码以确定你需要设置的构造函数args。

根据您的需要,也许还有其他更方便的切入点; 我建议在调试模式下启动一些测试,在测试中放置一个断点并向下移动。