1

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);
        }
}
Jann
  • 1,799
  • 3
  • 21
  • 38
Vipul Gupta
  • 391
  • 2
  • 11

2 Answers2

0

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.

user176692
  • 780
  • 1
  • 6
  • 21
  • What do you mean by "after deployment"? Test classes are not included in the generated jar by default. – mnesarco Jan 22 '20 at 16:30
  • Agreed, think he just meant how can I run tests if they are in src/test. Somewhat of a two-pronged question. But having the tests in src/test should not be a stumbling block, that's where they should go. – user176692 Jan 22 '20 at 16:42
  • @mnesarco i want to create an API that will call runTestNg() in src/main/java and this will trigger testcases in src/test/java, looking to do something like this. – Vipul Gupta Jan 23 '20 at 04:03
  • @user176692 is there any way i am able to call testcases written under src/test/java from a method written inside under any class in scr/main/java? – Vipul Gupta Jan 23 '20 at 04:30
  • @mnesarco it is working fine when i am doing maven build all testcases are running and report is generated but i am looking for something like on UI side i will give one button after click of that button all testcases will run and geneate the report.(after deployment*), so for that i need something like , i am able to call src/test/java from src/test/main – Vipul Gupta Jan 23 '20 at 05:49
  • Test cases does not exists on runtime. maven runs test cases separately after build. But if you want to run test cases at runtime (UI), you first need to include the Test classes and test dependencies into the classpath at runtime. Then use TestNG or whatever framework you use for tests to call the Test Suit. – mnesarco Jan 23 '20 at 16:02
0

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.

Austngmnz
  • 24
  • 2