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

问题描述

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

@Test
public void executeSessionOne(){
    runJavaProgram();
}

这可能吗?

编辑:我有一个名为Main.java的Java文件,另一个为Tools.java。我运行Main.java程序,并使用Selenium来测试网页。 Tools.java只是我需要的一些功能

我也尝试过:


public class RealTest {
    
    @Test    
    public void run1() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run2() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run3() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run4() throws MalformedURLException{           
        new Main();
    }
}
    public Main() throws MalformedURLException {
        String arg [];
        arg = new String []{"300"};
        caps = new DesiredCapabilities();
        caps.setCapability("os","Windows");
        caps.setCapability("os_version","10");
        caps.setCapability("browser","Chrome");
        caps.setCapability("browser_version","80.0 beta");
        caps.setCapability("browserstack.local","false");
        caps.setCapability("browserstack.selenium_version","3.5.2");

        caps.setCapability("name","selenium test");

        //driver = new RemoteWebDriver(new URL(URL),caps);
        
        chromeOptions = new ChromeOptions();
        String chromeDriverPath = "resources/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver",chromeDriverPath);
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        
        try {
            before(arg);
            test();
            after();
        } catch (Exception e) {
            Waits.pause(e.getMessage());
            driver.quit();
        }
        
    }

但随后测试合并,因此无法正常工作。

解决方法

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

,

它应该像这样工作:

public class test {
public static void main(String[] args) {

    String a = "_SEQ_1";
    a = a.substring (5);
    System.out.println (a);

}

}

@Test
public void testMethod(){
    test testObj = new test();
    testObj.main(new String[]{});
}