首页 > 解决方案 > testng.xml 正在跳过测试?

问题描述

因此,如果我直接将它作为 TestNG 测试运行,或者如果我运行包含测试的 testng.xml 文件,我编写的一些测试会成功运行。但是目前,当我向我的 Java servlet 发出请求时,我正在尝试调用 TestNG 运行方法。我使用自定义报告器将结果写入 json 文件,并将这些结果发送回响应中。

但是,每当我向 servlet 发出请求时,我收到的结果都告诉我,我的所有测试都被跳过了。为什么我的测试在直接访问时会成功运行,而在被 TestNG.run() 调用时只是跳过?

这是我的 testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="MyTestSuite">
    <listeners>
        <listener class-name="util.Listener"></listener>
        <listener class-name="util.JsonReporter"></listener>
    </listeners>
    <test name="TestOne">
        <classes>
            <class name="testngs.MyTest"></class>
        </classes>
    </test>
</suite>

还有一个简短的虚拟测试:

package testngs;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import pages.MyPage;

public class MyTest {

    public static MyPage myPage;
    public static WebDriver driver;

    @BeforeSuite
    public void setUpDriverAndPage() {
        File file = new File("src/main/resources/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
        driver = new ChromeDriver();
        myPage = new ManageBatchPage(driver);
        driver.get("https://www.google.com");
    }

    @Test
    public void createBatch() throws InterruptedException {
        // filler code

        Assert.assertEquals("hello", "hello");

    }

    @AfterSuite
    public void cleanup() {
        driver.quit();
    }
}

还有我的 servlet 代码:

if (uri.equals("/TestProject/getTests.do")) {       
    TestNG runner = new TestNG();
    List<String> suiteFiles = new ArrayList<String>();
    suiteFiles.add(/*path to my xml*/);
    runner.setTestSuites(suiteFiles);
    runner.run();

    // read results from json file
    JsonArray testResults = ResultParser.parseJson();

    response.setContentType("application/json");
    response.getWriter().print(testResults);
}

标签: javaseleniumservletstestng

解决方案


让它工作,但不优雅。事实证明,我的测试文件在从服务器运行时不喜欢我的 webdriver 的路径,但是当我直接运行文件时工作正常。当我在服务器上运行它时,我必须制作一个属性文件或包含绝对路径的东西。


推荐阅读