0
votes

I am using testNG for writing test cases, all testcase code is inside src/test/java folder, now i want to run testcases inside this folder from a class inside src/main/java after deployment of the application,will it be possible?

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

file under src/main/resources--> 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>

file under src/test/java --> ProductServiceImplTest.java

@Test
public class ProductServiceImplTest {
    @Test
        public void addProduct() {`enter code here`
            String value="GoodMorning";
            assertEquals("Hello", value);
        }
}
1

1 Answers

0
votes

Have you tried putting them in the same package? That's usually how you set up JUnit Tests. This is a JUnit5 example, but using the JUnit Platform Launcher, discovering tests by selecting the package works:

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

On the testNG side, yes should be able to put tests in src/test also. See this answer on example folder layouts with testNG.