How to run Selenium Java tests with TestNG programmatically?

删除回忆录丶 提交于 2019-12-05 21:31:10

问题


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?


回答1:


TheStijn gives a few good directions, although TestMethodWorker() is internal so you shouldn't use it.

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:

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




回答2:


My following code in java works nicely:

@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:

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




回答3:


Have a look at org.testng.remote.RemoteTestNG, this will however require you to write an xml suite for your tests like:

<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.

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.



来源:https://stackoverflow.com/questions/5457983/how-to-run-selenium-java-tests-with-testng-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!