如何将变量从BeforeTest传递给Test annotation

Nar*_*esh 2 java testng annotations

我试图在@BeforeTest方法中读取excel中的数据,并尝试在testNG中的@Test方法中打印相同的数据.我已经在一张纸上阅读了数据

@BeforeTest
    @Parameters({"filePath", "fileName", "sheetName"})
    public void readExcel(String filePath, String fileName, String sheetName) throws IOException, EncryptedDocumentException, InvalidFormatException{
        // set the file path
        File eFile = new File(filePath+"\\"+fileName);
        //print the file path
        System.out.println("File is at: "+eFile);

        FileInputStream inputstream = new FileInputStream(eFile);

        // Read and publish extension
        String extensionName = fileName.substring(fileName.indexOf("."));
        System.out.println("File type is: "+extensionName);

        //Read the file
        Workbook wb = new XSSFWorkbook(inputstream);

        //Read the sheet name
        Sheet wbSheet = wb.getSheet(sheetName);

    }
Run Code Online (Sandbox Code Playgroud)

我想将wbSheet传递给@Test方法来打印并将数据写入excel.@Parameters正在寻找在testng.xml中定义的常量.如何传递这样的变量..?

ass*_*ias 5

为什么不使用实例变量?

class YourClassTest {

  Sheet wbSheet;

  @BeforeTest public void readExcel(...) {
    wbSheet = wb.getSheet(sheetName);
  }

  @Test public void test() {
    //you can use wbSheet here
  }

}
Run Code Online (Sandbox Code Playgroud)