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

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

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

问题

我有一个已经创建好的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();
    }
}

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

英文:

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:

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

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:


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

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

它应该像这样工作

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[]{});
}
英文:

It should work like this :

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[]{});
}

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:

确定