如何根据内部逻辑多次运行testNG测试?

时间:2017-10-28 09:23:09

标签: java excel apache-poi testng

我已经找到了一些解决方案,讲述了 TestNg DataProvider InvocationCount ,但 DataProvider InvocationCount 来了甚至在我的 @Test 方法开始之前的图片中。我的要求是,我有一个 DataReader 类,它以键值对的形式从excel文件中读取数据(键总是在第一行,值可以有多行)。假设有两行值可用,那么我必须使用另一组数据运行相同的 @Test (如果我可以运行 @BeforeClass ,那就太棒了每次 @Test 迭代的 @AfterClass 方法。

像这样:

@BeforeClass
//Some Code Here that runs on each iteration of @Test

@Test
public void myTest() {

// make a decision here, based on number of rows of values, run the test multiple times
DataReader.LoadDataSheet("TestData.xlsx", "SheetName");

}

@AfterClass
//Some Code Here that runs on each iteration of @Test

2 个答案:

答案 0 :(得分:1)

您需要的是工厂支持的数据提供商。

与工厂方法绑定的第一个数据提供程序将提供测试方法将为每个实例使用的数据,以根据需要迭代多次。首先由外部数据提供者提供的数据将由数据提供者使用,该数据提供者将成为每个实例的一部分,这将根据需要多次迭代测试。

以下样本应该能够澄清这一点。

Printing Parameters before running test method [Maven]
Printing Parameters when running test method [Maven]
Printing Parameters after running test method [Maven]

Printing Parameters before running test method [Gradle]
Printing Parameters when running test method [Gradle]
Printing Parameters after running test method [Gradle]

Printing Parameters before running test method [Ant]
Printing Parameters when running test method [Ant]
Printing Parameters after running test method [Ant]

Printing Parameters before running test method [TestNG]
Printing Parameters when running test method [TestNG]
Printing Parameters after running test method [TestNG]

Printing Parameters before running test method [JUnit]
Printing Parameters when running test method [JUnit]
Printing Parameters after running test method [JUnit]

Printing Parameters before running test method [Java]
Printing Parameters when running test method [Java]
Printing Parameters after running test method [Java]

===============================================
Default Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

这是输出:

create.select().from(TABLE_A).join(TABLE_B).onKey(Keys.FK_TABLEA_TABLEB)
                    .join(TABLE_C).onKey(Keys.FK_TABLEB_TABLEC)
                    .where(TABLE_C.FIELDC.containsIgnoreCase("foo");

答案 1 :(得分:1)

我不是一个花哨的程序员,但这就是我想要的。

测试类:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Test2 extends BaseTset {

    @Test(dataProvider = "dataprovider")
    public void test(DataReader2 reader) {
        for (int i = 1; i <= 10; i++) {
            System.out.println("Test : " + reader.getValue("Key_" + i));
        }
    }

    @DataProvider(name = "dataprovider")
    public DataReader2[] dataProvider() {
        String dataFileName = "TestData.xlsx";
        DataReader2 reader = new DataReader2(dataFileName);
        int rowCount = reader.getRowCount();
        DataReader2[] reader2 = new DataReader2[rowCount];
        for (int i = 0; i < rowCount; i++) {
            int j = i + 1;
            reader2[i] = DataReader2.getReader(dataFileName, j);
        }
        return reader2;
    }
}

数据读取器2类:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DataReader2 {
    private Map<String, String> dataSet = new HashMap<String, String>();
    private XSSFWorkbook ExcelWBook;
    private XSSFSheet ExcelWSheet;
    private FormulaEvaluator Evaluator;
    private XSSFCell Cell;
    private XSSFRow Row;
    private int rowCount;
    private int columnCount;

    public DataReader2(String FileName, int rowNum) {
        loadDataFile(FileName);
        dataSet = getDataSet(rowNum);
    }

    public DataReader2(String FileName) {
        loadDataFile(FileName);
    }

    public static DataReader2 getReader(String FileName, int rowNum) {
        DataReader2 dataReader = new DataReader2(FileName, rowNum);
        return dataReader;
    }

    private void loadDataFile(String FileName) {
        try {
            String FilePath = "./data/" + FileName;
            FileInputStream ExcelFile = new FileInputStream(FilePath);
            ExcelWBook = new XSSFWorkbook(ExcelFile);
            Evaluator = ExcelWBook.getCreationHelper().createFormulaEvaluator();
            ExcelWSheet = ExcelWBook.getSheetAt(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getRowCount() {
        rowCount = ExcelWSheet.getLastRowNum();
        return rowCount;
    }

    private Map<String, String> getDataSet(int rowNum) {
        DataFormatter formatter = new DataFormatter();
        Row = ExcelWSheet.getRow(rowNum);
        columnCount = Row.getLastCellNum();
        for (int i = 0; i < columnCount; i++) {
            Cell = ExcelWSheet.getRow(0).getCell(i);
            String key = formatter.formatCellValue(Cell, Evaluator);
            Cell = Row.getCell(i);
            String value = formatter.formatCellValue(Cell, Evaluator);
            dataSet.put(key, value);
        }
        return dataSet;
    }

    public String getValue(String key) {
        try {
            key = key.trim();
            String value = dataSet.get(key).trim();
            if (!value.isEmpty() && !value.equals(null)) {
                return value;
            } else {
                throw (new Exception("No key with name : " + key + " available in datasheet."));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}

基础测试类:

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

public class BaseTset {

    @BeforeClass
    public void beforeClass() {
        System.out.println("Before class");
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before method");
    }

    @AfterMethod
    public void AfterMethod() {
        System.out.println("After method");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("After Class");
    }
}

输出:

[RemoteTestNG] detected TestNG version 6.12.0
Before class
Before method
Test : Value_1
Test : Value_2
Test : Value_3
Test : Value_4
Test : Value_5
Test : Value_6
Test : Value_7
Test : Value_8
Test : Value_9
Test : Value_10
After method
Before method
Test : Value_11
Test : Value_12
Test : Value_13
Test : Value_14
Test : Value_15
Test : Value_16
Test : Value_17
Test : Value_18
Test : Value_19
Test : Value_20
After method
Before method
Test : Value_21
Test : Value_22
Test : Value_23
Test : Value_24
Test : Value_25
Test : Value_26
Test : Value_27
Test : Value_28
Test : Value_29
Test : Value_30
After method
Before method
Test : Value_31
Test : Value_32
Test : Value_33
Test : Value_34
Test : Value_35
Test : Value_36
Test : Value_37
Test : Value_38
Test : Value_39
Test : Value_40
After method
After Class
PASSED: test(datareader.DataReader2@186beff)
PASSED: test(datareader.DataReader2@78afa0)
PASSED: test(datareader.DataReader2@1c2959f)
PASSED: test(datareader.DataReader2@19982de)

===============================================
    Default test
    Tests run: 4, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================