无法从src / main / java调用src / test / java内部的代码?

我正在使用testNG编写测试用例,所有测试用例代码都在src / test / java文件夹内,现在我想在部署应用程序后从src / main / java内的类在此文件夹内运行测试用例。有可能吗?

src / main / java-> TestNGRun下的

文件

public class TestNGRun {
    public static void runTestNg() {
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testNG = new TestNG();
        testNG.addListener(tla);
        List<String> suites = new ArrayList<String>();
        URL filePathUrl = TestNGRun.class.getclassLoader().getResource("/testng.xml");
        suites.add(filePathUrl.getPath());
        testNG.setTestSuites(suites);
        testNG.run();
    }
}
src / main / resources下的

file-> testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener
            class-name="com.cypress.prodService.testNG.CustomisedReports"></listener>
    </listeners>
    <test name="ProductServiceImplTest">
        <classes>
            <class
                name="com.cypress.prodService.testNG.ProductServiceImplTest"></class>
        </classes>
    </test>
</suite>
src / test / java下的

文件-> ProductServiceImplTest.java

@Test
public class ProductServiceImplTest {
    @Test
        public void addProduct() {`enter code here`
            String value="Goodmorning";
            assertEquals("Hello",value);
        }
}
zmmll 回答:无法从src / main / java调用src / test / java内部的代码?

您是否尝试过将它们放在同一包装中?通常,这就是设置JUnit测试的方式。这是一个JUnit5示例,但是使用JUnit Platform Launcher,通过选择软件包可以发现测试:

https://github.com/doenn/sentences/tree/master/src https://github.com/doenn/sentences/blob/master/src/test/java/sentences/SentencesTest.java https://github.com/doenn/sentences/blob/master/src/main/java/sentences/SentenceTests.java

在testNG方面,yes应该也可以将测试放在src / test中。有关使用testNG的示例文件夹布局,请参见this answer

本文链接:https://www.f2er.com/2732208.html

大家都在问