【问题标题】:How to get Method name in testng using @BeforeTest annotation如何使用@BeforeTest注解在testng中获取方法名称
【发布时间】:2018-06-06 06:34:26
【问题描述】:

我正在使用 Rest Assured 进行 REST API 测试,我正在使用 @BeforeTest 注释调用 jxl 数据并在 @Dataprovider 注释的帮助下将结果写入其中以传递测试数据。

但是在这里我总共有 4 个 api,我想根据来自不同类的 @Test 注释中的方法名称来阅读特定的其余 API 的特定工作表。

示例:

1.cardvalidation - 验证表 2.Purchase - 采购单 3.Cancel - 取消采购表

例如,如果方法名称是 cardvalidation,那么我将只为所有 4 个 API 获取验证表等,并且我正在使用 Before Method 注释,但它只为一行而不是所有测试数据行写入结果。

示例代码:

@BeforeMethod
    public void excelWriter(Method method) throws BiffException, IOException, RowsExceededException, WriteException {

        File file = new File(inputFile);
        FileInputStream testFile = new FileInputStream(file);
        Workbook workBook = Workbook.getWorkbook(testFile);

        String methodName = method.getName();

        if(methodName.equals("validateTesting")) {
            sheet = workBook.getSheet("Validate_TestData");
        }else if(methodName.equals("purchaseTesting")) {
            sheet = workBook.getSheet("Purchase_TestData");
        }

        int rows = sheet.getRows();
        int columns = sheet.getColumns();

        String dataFile[][] = new String[rows][columns];

        File outputResults = new File(outputFile);
        // Write data into file
        FileOutputStream fileOutputStream = new FileOutputStream(outputResults);
        // Create the workbook
        workbookCopy = Workbook.createWorkbook(fileOutputStream);

        if(methodName.equals("validateTesting")) {
            writablesh = workbookCopy.createSheet("Validate_Results", 0);
        }else if(methodName.equals("purchaseTesting")) {
            writablesh = workbookCopy.createSheet("Purchase_Results", 1);
        }


        // Copy all data from source file to Destination File

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                dataFile[i][j] = sheet.getCell(j, i).getContents();
                Label l = new Label(j, i, dataFile[i][j]);

                Label L2 = new Label(13, 0, "Results");
                writablesh.addCell(l);
                writablesh.addCell(L2);

            }
        }

    }

【问题讨论】:

    标签: java rest testng jxl


    【解决方案1】:

    尝试实现接口IInvokedMethodListener,那么你将不得不实现以下方法:

    /**
     * A listener that gets invoked before and after a method is invoked by TestNG.
     * This listener will only be invoked for configuration and test methods.
     */
    
    void beforeInvocation(IInvokedMethod method, ITestResult testResult);
    void afterInvocation(IInvokedMethod method, ITestResult testResult);
    

    所以这将满足您的需求。

    所以这里是实现这一点的示例代码:

    @Override
    public synchronized void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        Class<?> clazz = (Class<?>) method.getTestMethod().getRealClass();
        Method method = method.getTestMethod().getMethod());
    
         // so each method You call is basically invoked, and it has to be processed.
    
    }
    

    试试这个。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2011-02-26
      • 2015-10-11
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多