TestNG No tests found. Nothing was run

守給你的承諾、 提交于 2021-02-08 04:57:06

问题


I am facing a strange problem with my TestNg code. When I run the testng class without dataprovider my tests run successfully and gets failed which is expected for me. But when I keep my dataprovider in the class for that test. Testng gives an error : [TestNG] No tests found. Nothing was run Below is my code: Please let me know the solution or suggest what I am doing wrong TestNG Version: 6.14.3

package api.tests;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.HashMap;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import api.config.Base;
import api.data.xlutility_read;
import api.services.GooglePlace;

public class GoogleAddPlaceTest extends Base {

    public static GooglePlace GP = null;
    public static xlutility_read ER = null;
    public HashMap<String, String> requestMap = null;

    public GoogleAddPlaceTest() throws IOException {
        super();

    }

    @BeforeMethod
    public void Setup() throws IOException {
        GP = new GooglePlace();
        Base.setResources();
        // ER =
    }

    @DataProvider()
    public Object[][] getExcelRows() throws InvalidFormatException, IOException {

        ER = new xlutility_read("D:\\ApiData.xlsx");

        Object[][] object = new Object[ER.getRowCount(1) - 2][1];
        int rNum = 3;
        while (!ER.isRowEmpty(1, rNum) && rNum <= ER.getRowCount(1)) {
            requestMap = new HashMap<>();
            for (int j = 0; j < 1; j++) {

                requestMap = ER.ExcelToHashMap(1, rNum);

            }
            object[rNum - 3][0] = requestMap;
            rNum++;
        }

        ER.closeFile();

        return object;

    }

    @Test(dataProvider = "getExcelRows")
    public void testAddPlaceApi(HashMap<Object, Object> requestMap) throws IOException {
        try {
            ER = new xlutility_read("D:\\ApiData.xlsx");
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int testRowNum = ER.getRowNum(1, "testAddPlaceApi", 0);
        if (testRowNum != -1) {
            if (ER.getCellData(1, testRowNum, 1).equalsIgnoreCase("y")) {
                GP.deletePlaceApi(GP.addPlaceApi(GP.jsonAsMap(requestMap)));
            } else {
                throw new SkipException("Test case run Flag is N : So Skipped.");
            }
        }

    }

    @AfterMethod
    public void tearUp() throws IOException {
        Base.freeResources();

    }
}

回答1:


From the TestNG documentation:

A @Test method specifies its Data Provider with the dataProvider attribute. This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.

So you have to bind your data provider method to test method by name:

@DataProvider(name = "data")
public Object[][] getExcelRows() throws InvalidFormatException, IOException {
   ...
}

and

@Test(dataProvider = "data")
public void testAddPlaceApi(HashMap<Object, Object> requestMap) throws IOException {
   ...
}



回答2:


Are you using excel sheets to store your test data? If yes, add one more column as Execute with value as Y.



来源:https://stackoverflow.com/questions/53590402/testng-no-tests-found-nothing-was-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!