简体   繁体   中英

TestNG Dataprovider closing driver after each iteration

I am trying to run a dataprovider based test using ANT. My main class points to testNG file

XmlSuite suite=new XmlSuite();
    suite.setName("Test Results");
    suite.setPreserveOrder(true);
    List<XmlSuite> suits=new ArrayList<XmlSuite>();
    suits.add(suite);


    List<XmlPackage> xpackage=new ArrayList<XmlPackage>();
    xpackage.add(new XmlPackage(TestProperties.TESTNG_PACKAGE.toString()));

    
    XmlTest test=new XmlTest(suite);
    test.setPackages(xpackage);

    String groups=TestProperties.TESTNG_GROUP.toString();
    System.out.println("groups are:"+groups);
    String groupArray[]=groups.split(",");
    List<String> includedGroups=new ArrayList<String>();
    includedGroups.addAll(Arrays.asList(groupArray));
    test.setIncludedGroups(includedGroups); 
    
    TestNG tng=new TestNG();
    tng.setOutputDirectory("test-output");
    tng.setXmlSuites(suits);
    tng.run();
    System.exit(0);

Now, in my Testcase file, I have

@BeforeClass(alwaysRun = true)
public void pretest() throws IOException, GeneralSecurityException {
    Pretest     
}

@Test(groups= {"indicatorGroup","",""},description = "Validate indicator uploaded", dataProvider = "getIndicatorData")
public void indicatorUpload(String txt){
    
    test
}


@DataProvider
public Object[] getIndicatorData() throws IOException, GeneralSecurityException
{
    bla bla
    for(int i=0; i<contents.length; i++) {
        System.out.println(contents[i]);
        if(!contents[i].contains("README")) {
        blah blah
        System.out.println(path);
        names.add(path);
        }
    }
    String[] myArray = new String[names.size()];
    names.toArray(myArray);
    return myArray;
}


@AfterClass(alwaysRun = true)
public void afterMethod() throws IOException {
    System.out.println("Deleting all files after operation.....");
    

}

The problem is, they run without any issues, if i run from Testng. ie if i right click on the file, click on run, click on Run as Testng Test. But if I run from my build file, after the first iteration, the driver I bring up in before class, closes and the rest of the tests fail. This causes my tests to fail in jenkins. Can someone please help me out?

After many trials, I found out that the testng xml created By my trigger file had

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="IndicatorSuite" data-provider-thread-count="1">
  <test name="Test Basics 1">
  <groups>
  <run>
    <include name="indicatorUpload" />
  </run>
</groups>
    <classes>
      <class name="Uploads.IndicatorFileUpload">
       <class name="Uploads.IndicatorFileUpload1">
      <class name="Uploads.IndicatorFileUpload2">

        </methods> 
      </class>
    </classes>
  </test>
</suite>

Even though my test group was correct, I had given test package, because of which all my testcase files had been added as classes. All of these had a listener which would close the driver after testrun. I dont know much about testng, but I think it mixed up the listeners. In my Trigger file, when I added just

TestNG tng=new TestNG();
        tng.setOutputDirectory("test-output");
        //tng.setXmlSuites(suits);
        
        
        tng.setTestClasses(new Class[] { IndicatorFileUpload.class });
        tng.run();

it worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM