如何使用TestNG在测试中运行已创建的Java程序?

huangapple go评论97阅读模式
英文:

How can I use testNG to run an already created Java program in a test?

问题

我有一个已经创建好的Java程序。我需要知道在testNG中运行整个程序的最简单方法。类似这样:

  1. @Test
  2. public void executeSessionOne(){
  3. runJavaProgram();
  4. }

这可能吗?

编辑:我有一个名为Main.java的Java文件,还有一个名为Tools.java的文件。我运行Main.java程序,使用Selenium测试一个网页。Tools.java只包含一些我需要的函数。

我还尝试了这个:

  1. public class RealTest {
  2. @Test
  3. public void run1() throws MalformedURLException{
  4. new Main();
  5. }
  6. @Test
  7. public void run2() throws MalformedURLException{
  8. new Main();
  9. }
  10. @Test
  11. public void run3() throws MalformedURLException{
  12. new Main();
  13. }
  14. @Test
  15. public void run4() throws MalformedURLException{
  16. new Main();
  17. }
  18. }
  1. public Main() throws MalformedURLException {
  2. String arg[];
  3. arg = new String[]{"300"};
  4. caps = new DesiredCapabilities();
  5. caps.setCapability("os", "Windows");
  6. caps.setCapability("os_version", "10");
  7. caps.setCapability("browser", "Chrome");
  8. caps.setCapability("browser_version", "80.0 beta");
  9. caps.setCapability("browserstack.local", "false");
  10. caps.setCapability("browserstack.selenium_version", "3.5.2");
  11. caps.setCapability("name", "selenium test");
  12. //driver = new RemoteWebDriver(new URL(URL), caps);
  13. chromeOptions = new ChromeOptions();
  14. String chromeDriverPath = "resources/chromedriver.exe";
  15. System.setProperty("webdriver.chrome.driver", chromeDriverPath);
  16. driver = new ChromeDriver();
  17. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  18. try {
  19. before(arg);
  20. test();
  21. after();
  22. } catch (Exception e) {
  23. Waits.pause(e.getMessage());
  24. driver.quit();
  25. }
  26. }

但是测试会被合并,不能正常工作。

英文:

I have a Java program that is already created. I need to know the easiest way to run this entire program in testNG. Something like this:

  1. @Test
  2. public void executeSessionOne(){
  3. runJavaProgram();
  4. }

Is this possible?

EDIT: I have a Java file called Main.java and another called Tools.java. I run the Main.java program and uses Selenium to test a webpage. Tools.java just has some functions that I need.

I also tried this:

  1. public class RealTest {
  2. @Test
  3. public void run1() throws MalformedURLException{
  4. new Main();
  5. }
  6. @Test
  7. public void run2() throws MalformedURLException{
  8. new Main();
  9. }
  10. @Test
  11. public void run3() throws MalformedURLException{
  12. new Main();
  13. }
  14. @Test
  15. public void run4() throws MalformedURLException{
  16. new Main();
  17. }
  18. }
  1. public Main() throws MalformedURLException {
  2. String arg [];
  3. arg = new String []{"300"};
  4. caps = new DesiredCapabilities();
  5. caps.setCapability("os", "Windows");
  6. caps.setCapability("os_version", "10");
  7. caps.setCapability("browser", "Chrome");
  8. caps.setCapability("browser_version", "80.0 beta");
  9. caps.setCapability("browserstack.local", "false");
  10. caps.setCapability("browserstack.selenium_version", "3.5.2");
  11. caps.setCapability("name", "selenium test");
  12. //driver = new RemoteWebDriver(new URL(URL), caps);
  13. chromeOptions = new ChromeOptions();
  14. String chromeDriverPath = "resources/chromedriver.exe";
  15. System.setProperty("webdriver.chrome.driver", chromeDriverPath);
  16. driver = new ChromeDriver();
  17. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  18. try {
  19. before(arg);
  20. test();
  21. after();
  22. } catch (Exception e) {
  23. Waits.pause(e.getMessage());
  24. driver.quit();
  25. }
  26. }

but then the tests get combined and it doesn't work right.

答案1

得分: 1

如果程序在其他类中,则通过创建该类的对象来运行该方法。

英文:

If the program is in some other class, run the method by creating object of the class.

答案2

得分: 1

  1. 它应该像这样工作
  2. public class test {
  3. public static void main(String[] args) {
  4. String a = "_SEQ_1";
  5. a = a.substring(5);
  6. System.out.println(a);
  7. }
  8. }
  9. @Test
  10. public void testMethod() {
  11. test testObj = new test();
  12. testObj.main(new String[]{});
  13. }
英文:

It should work like this :

  1. public class test {
  2. public static void main(String[] args) {
  3. String a = "_SEQ_1";
  4. a = a.substring (5);
  5. System.out.println (a);
  6. }

}

  1. @Test
  2. public void testMethod(){
  3. test testObj = new test();
  4. testObj.main(new String[]{});
  5. }

huangapple
  • 本文由 发表于 2020年8月20日 02:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63492691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定