Reputation: 491
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);
}
}
Upvotes: 1
Views: 1475
Reputation: 343
Change the scope of dependency to compile instead of Test. This works for me to run testng codes in src/main/java.
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>compile</scope>
</dependency>
Upvotes: 0
Reputation: 24
It might depend on how your project is set up, but I know for all of my projects, anything in the test folder IntelliJ will not let you call from the main folder, only the other way around. idk if this is a java thing or specifically from the IDE, but usually you wouldn't want to rely on anything from your test packages in your actual build.
To add, I'm pretty sure the test package of your projects are designed specifically to test your code outside of it being built, and arent included in the actual build of the program when it's running. So any calls to the classes within would not actually have anything to call to during the program running. If you are trying to create some form of test that is ran during the programs runtime, I'd create it in src/main/ then create a package for your verification tests there, where they will be included in the build. Just keep in mind that these tests will not run in any CICD style building check by default, so if all your tests are in there, you might get a bugged out program with failing tests past the checks without failing.
Upvotes: 0
Reputation: 830
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.
Upvotes: 0