java - 如何将变量从 BeforeTest 传递到 Test 注释

标签 java annotations testng

我正在尝试在 @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);

    }

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

最佳答案

为什么不使用实例变量?

class YourClassTest {

  Sheet wbSheet;

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

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

}

关于java - 如何将变量从 BeforeTest 传递到 Test 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292979/

相关文章:

Java react 器返回不同的异常

android - 没有使用maven将注释处理(生成的)源编译成apk

java - 使用 TestNG 让每个测试方法在其自己的测试类实例中运行?

java - java 1.8 上的 maven-enunciate-plugin 作为 apt 现在已从最新的 JDK(即 java 注释处理工具)中删除

java - Selenium 网络驱动程序 : MalformedByteSequenceException Invalid byte 2 of 2-byte UTF-8 sequence

java - 使用 Maven 在 Eclipse 中以编程方式运行 testNG,无需插件

java - 为什么我的 Cassandra 准备语句摄取数据如此慢?

java - 当数组充满了 get 方法中的数字时,如何对数组进行排序

java - java/scala中基于输入参数的序列号生成器

iphone - 如何在 map 首次加载时自动打开 map 上的注释标注?