1
votes

When executing testng xml, it executes only 1 test in the suite & does not execute rest of the test cases. If I add all tests in single class in that suite, it executes all tests but when they are in separate classes, it will execute only 1 test. Below are testng xml, BasicTest & LoginTest- TestNG xml is as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose = "2" name="Suite" configfailurepolicy="skip">
  <test name="all-tests">
    <classes>
            <class name="Test.DemoMavenEclipseProject.BasicTest"/>
            <class name="Test.DemoMavenEclipseProject.LoginTest"/>
            <class name="Test.DemoMavenEclipseProject.AllProductsTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

BasicTest -

package Test.DemoMavenEclipseProject;

    import org.testng.ITestResult;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeSuite;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.io.FileHandler;
    import com.relevantcodes.extentreports.ExtentReports;
    import com.relevantcodes.extentreports.ExtentTest;
    import com.relevantcodes.extentreports.LogStatus;
    import Actions.DriverManager;
    import Actions.DriverManagerFactory;
    import Actions.DriverType;
    import Utility.Constants;

public class BasicTest {
    DriverManager driverManager;
    WebDriver driver;
    ExtentReports report;
    ExtentTest logger;

    @BeforeSuite
    public void setUp() throws Exception {
       report=new ExtentReports("./Reports/TestReport.html");
       driverManager = DriverManagerFactory.getDriverManager(DriverType.CHROME);
       driver = driverManager.getWebDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get(Constants.URL);
       driver.manage().window().maximize();
    }   

    @AfterSuite
    public void getResult(ITestResult result) throws IOException {
       if (result.getStatus()==ITestResult.FAILURE) { 
       logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getName()); // to add name in extent report
       logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getThrowable()); 

       String screenshotPath = BasicTest.getScreenshot(driver, result.getName());
            logger.addScreenCapture(screenshotPath);// adding screen shot
       }
       else if (result.getStatus()==ITestResult.SKIP) {
          logger.log(LogStatus.SKIP, "Test Case SKIPPED IS " + result.getName());
       }
       else if (result.getStatus() == ITestResult.SUCCESS) {
               logger.log(LogStatus.PASS, "Test Case PASSED IS " + result.getName());
      }
    }
    public void tearDown() throws Exception {
            report.endTest(logger);
            //Flush the data to report
            driver.quit();
            report.flush();
    }

    public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
          String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
          TakesScreenshot ts = (TakesScreenshot) driver;
          File source = ts.getScreenshotAs(OutputType.FILE);

          // after execution, you could see a folder "FailedTestsScreenshots" under src folder
          String destination = "./Reports/Screenshots/" + screenshotName + dateName + ".png";
          File finalDestination = new File(destination);
          FileHandler.copy(source, finalDestination);
         // FileUtils.copyFile(source, finalDestination);
          return destination;
      }
    }

LoginTest -

package Test.DemoMavenEclipseProject;

import org.testng.annotations.Test;

import com.relevantcodes.extentreports.LogStatus;

import Actions.login_action;
import PageObjects.homePage;

public class LoginTest extends BasicTest {
  @Test
    public void Login() throws Exception {
        //Create object for Report with filepath
        //Start the test
        logger=report.startTest("LoginTest");
        //Log the status in report
        logger.log(LogStatus.INFO, "Login link is displayed");
        login_action.Execute(driver);
    if (homePage.link_Logout(driver).isDisplayed())
            logger.log(LogStatus.PASS, "Logged into the site successfully");
        else 
        logger.log(LogStatus.FAIL, "Login is unsuccessful");
        //End the test
        Thread.sleep(2000);
  }

}
2
If you want the tests to be separate, you need to declare @Test on each test. Pretty good write-up here. If this solves your issue, I convert it to an answer you can accept. - Brian
Brian, My LoginTest & AllProductsTest are separate test classes with @Test annotation. But TestNG executed only BasicTest class in that suite. & this is using TestNG (not JUnit) - prii kadam
TestNG uses annotations too. - Brian
Yes, I have used @Test in all separate tests but TestNG executed only Basic Test - prii kadam
Are all your tests listed in your execution window? - Brian

2 Answers

0
votes

@Brian ,this is the console output:

[RemoteTestNG] detected TestNG version 7.0.0
...
... TestNG 7.0.0-beta3 by Cédric Beust ([email protected])
...

Starting ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) on port 46601
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1576688370.025][WARNING]: This version of ChromeDriver has not been tested with Chrome version 79.
Dec 18, 2019 11:59:30 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
===== Invoked methods
  BasicTest.startTest(java.lang.reflect.Method)[pri:0, instance:Test.EcommerceProject.LoginTest@5d76b067]public void Test.EcommerceProject.LoginTest.Login() throws java.lang.Exception  1568059495
=====
FAILED CONFIGURATION: @BeforeMethod startTest(public void Test.EcommerceProject.LoginTest.Login() throws java.lang.Exception)
java.lang.NullPointerException
    at Test.EcommerceProject.BasicTest.startTest(BasicTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:340)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:294)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:669)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:496)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:170)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:790)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:143)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at org.testng.TestRunner.privateRun(TestRunner.java:763)
    at org.testng.TestRunner.run(TestRunner.java:594)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1146)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
    at org.testng.TestNG.runSuites(TestNG.java:997)
    at org.testng.TestNG.run(TestNG.java:965)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED CONFIGURATION: @AfterMethod getResult
SKIPPED: Login
java.lang.NullPointerException
    at Test.EcommerceProject.BasicTest.startTest(BasicTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:340)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:294)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:669)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:496)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:170)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:790)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:143)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at org.testng.TestRunner.privateRun(TestRunner.java:763)
    at org.testng.TestRunner.run(TestRunner.java:594)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1146)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
    at org.testng.TestNG.runSuites(TestNG.java:997)
    at org.testng.TestNG.run(TestNG.java:965)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    FirstTest
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================

===== Invoked methods
  BasicTest.setUp()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] 391618063
  BasicTest.startTest(java.lang.reflect.Method)[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f]public void Test.EcommerceProject.ProductsTest.All_Products() throws java.lang.Exception  391618063
    ProductsTest.All_Products()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] 391618063
  BasicTest.getResult(org.testng.ITestResult)[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f][TestResult name=All_Products status=SUCCESS method=ProductsTest.All_Products()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] output={null}]  391618063
=====
PASSED: All_Products

===============================================
    SecondTest
    Tests run: 1, Failures: 0, Skips: 0
===============================================

log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.

===============================================
Suite
Total tests run: 2, Passes: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

0
votes

in your xml file "configfailurepolicy="skip"" ,as per testng if you have configfailurepolicy==skip in your suite then TestNG should skip to execute the remaining tests in the suite if an @Before* method fails.

As per your console I have seen that Beforetest Method got failed ,so check it and let me know.