java - 如何使用@BeforeTest注释获取testng中的方法名称

标签 java rest testng jxl

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

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

示例:

1.cardvalidation - 验证表 2.采购-采购表 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);

            }
        }

    }

最佳答案

尝试实现接口(interface)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.

}

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

关于java - 如何使用@BeforeTest注释获取testng中的方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50713649/

相关文章:

java - 如何命名自 JPA 2.1 以来的 ManyToOne 引用的外键约束?

java - 如何防止 gson 将整数转换为 double

java - 当方法同步时,我们可以使用 StringBuilder 而不是 StringBuffer 吗?

javascript - 无法使用 node.js http.request 将记录插入 ElasticSearch - MapperParsingException[无法解析]

wcf - 使用 WCF 或 ASP.Net Web Api 实现 RESTful API 的版本控制

java - 在界面中使用@RequestMapping 是不是一个坏主意?

java - 在不同的 chrome 选项卡中运行测试方法

java - 在TestNG中动态添加参数到@beforeclass

java - 使用 JSF、PrimeFaces 和 JPA : Create Basic WebApp without using Generated CRUD Classes, 表单等

java - 生成 TestNG.xml 文件时出现问题