java – 唯一标识testng中的每个测试调用

前端之家收集整理的这篇文章主要介绍了java – 唯一标识testng中的每个测试调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,所以我有一个这样的课
public class Calculator {

    @Test(dataProvider = "dp")
    public void add(int a,int b) {
        System.out.println("Invoked add: a,b" + a + "," + b);
    }

    @DataProvider(name = "dp")
    public Object[][] createData(ITestContext ctx) {
        return new Object[][] { new Object[] { 1,2 },new Object[] { 2,3 } };
    }

当测试运行时,它将运行add方法两次.我想根据其输入跟踪每个添加调用.所以说,add被调用1,2作为输入,那就是一个唯一的调用.如果失败,我想将此信息存储到具有调用ID的数据库.

使用testng如何实现?所有的监听器(methodinvocationlistener等)似乎没有提供唯一标识方法运行的上下文.是的,他们确实让你看到参数,但是我无法跟踪各个参数.那么,我以某种方式将我自己的唯一参数注入到结果对象中并从中跟踪呢?

UPDATE

我正在添加改进的代码,以帮助更好地理解上下文.
这是我的testng.xml

<suite name="Default Suite">
  <test name="test">
    <classes>
      <class name="com.test.testng.Calculator">
        <methods>
          <include name="add">
            <parameter name="data-id" value="1"/>
          </include> <!-- add -->
          <include name="add">
            <parameter name="data-id" value="2"/>
          </include> <!-- add -->
          <include name="subtract">
            <parameter name="data-id" value="3"/>
          </include> <!-- subtract -->
        </methods>
      </class> <!-- com.test.testng.Calculator -->
    </classes>
  </test> <!-- test -->
</suite> <!-- Default Suite -->

我有两个调用add和一个调用减法.
这是我的数据提供者

public class Calculator {

    @Test(dataProvider = "dp")
    public void add(int first,int second) {
        System.out.println("invoked add");
    }

    @Test(dataProvider = "dp")
    public void subtract(int first,int second) {
        System.out.println("invoked subtract");
    }

    @DataProvider(name = "dp")
    public Object[][] createData(Method m,ITestContext ctx) {    
        Object[][] data = new Object[][] { new Object[] { 1,3 },new Object[] { 3,4 } };
        for (XmlClass test : ctx.getCurrentXmlTest().getXmlClasses()) {
            for (XmlInclude method : test.getIncludedMethods()) {
                if (method.getName().equals(m.getName()))
                int key = Integer.parseInt(method.getAllParameters().get("data-id"));
                return new Object[][] { data[key - 1] };
            }
        }
        return null ;
    }

}

我预计,添加运行两次,一次以1,2为输入,另一次为2,3输入.类似地,用3,4减去输入.但是,我看到的是这样的 –

[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test test on 1  classes,included groups:[] excluded groups:[]
===== Test class
com.test.testng.Calculator
    @Test Calculator.add(int,int)[pri:0,instance:com.test.testng.Calculator@39a054a5]
    @Test Calculator.subtract(int,instance:com.test.testng.Calculator@39a054a5]
======
method.getAllParamas(){data-id=1}

[Invoker 665576141] Invoking com.test.testng.Calculator.add
invoked

[Invoker 665576141] Invoking com.test.testng.Calculator.subtract
subtract
===== Invoked methods
    Calculator.add(int,instance:com.test.testng.Calculator@39a054a5]1 2  966808741
    Calculator.subtract(int,instance:com.test.testng.Calculator@39a054a5]1 2  966808741
=====

我需要根据我将从testng xml发送的特殊参数为每个方法提供数据.我该如何实现?

解决方法

要实现这一点,您可以在testng.xml中定义不同的测试用例,如下所示:
<suite name="Default Suite">
  <test name="test">
    <classes>
      <class name="com.test.testng.Calculator">
        <methods>
          <include name="add">
            <parameter name="data-id" value="1"/>
          </include> <!-- add -->
          <include name="subtract">
            <parameter name="data-id" value="3"/>
          </include> <!-- subtract -->
        </methods>
      </class> <!-- com.test.testng.Calculator -->
    </classes>
  </test> <!-- test -->
  <test name="test2">
    <classes>
      <class name="com.test.testng.Calculator">
        <methods>
          <include name="add">
            <parameter name="data-id" value="2"/>
          </include> <!-- add -->
        </methods>
      </class> <!-- com.test.testng.Calculator -->
    </classes>
  </test> <!-- test -->
</suite> <!-- Default Suite -->

添加一个提供者方法的日志:

package com.test.testng;

import java.lang.reflect.Method;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;

public class Calculator {

    @Test(dataProvider = "dp")
    public void add(int first,4 } };
        for (XmlClass test : ctx.getCurrentXmlTest().getXmlClasses()) {
            for (XmlInclude method : test.getIncludedMethods()) {

                if (method.getName().equals(m.getName())) {
                    int key = Integer.parseInt(method.getAllParameters().get("data-id"));
                    System.out.println("Running method " + m.getName() + "  with data-id: " + key);
                    return new Object[][] { data[key - 1] };
                }


            }
        }
        return null ;
    }

}

运行这个xml作为testng套件(使用eclipse插件)显示

[TestNG] Running:
  /Users/fhernandez/Documents/workspaceTest/testNg-test/src/test/resources/testng.xml

Running method add  with data-id: 1
invoked add
Running method subtract  with data-id: 3
invoked subtract
Running method add  with data-id: 2
invoked add

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

如果我理解你的要求,你可以实现它.

实现这一点的另一种方法是在测试类之前放置一个方面或代理,并且检查方法调用,在此方面/代理中实现您的需求.

UPDATE

添加一个监听器到计算器

@Listeners(Listener.class)
public class Calculator

监听器如下所示

package com.test.testng;

import java.util.Arrays;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;

public class Listener implements IInvokedMethodListener {

    public void afterInvocation(IInvokedMethod method,ITestResult itr) {
        // TODO implements
    }

    public void beforeInvocation(IInvokedMethod method,ITestResult testResult) {

        // Parameters value
        System.out.println("Parameters invocation value for method " + method.getTestMethod().getMethodName());
        Arrays.asList(testResult.getParameters()).stream().forEach(System.out::println);

        // get data-id
        for (XmlClass test : testResult.getTestContext().getCurrentXmlTest().getXmlClasses()) {
            for (XmlInclude met : test.getIncludedMethods()) {

                if (met.getName().equals(method.getTestMethod().getMethodName())) {
                    int key = Integer.parseInt(met.getAllParameters().get("data-id"));
                    System.out.println("listener: Running method " + method.getTestMethod().getMethodName() + "  with data-id: " + key);
                }        
            }
        }
    }
}

beforeInvocation中的代码显示参数调用值和data-id,这是输出

[TestNG] Running:
  /Users/fhernandez/Documents/workspaceTest/testNg-test/src/test/resources/testng.xml

Running method add  with data-id: 1
Parameters invocation value for method add
1
2
listener: Running method add  with data-id: 1
invoked add
Running method subtract  with data-id: 3
Parameters invocation value for method subtract
3
4
listener: Running method subtract  with data-id: 3
invoked subtract
Running method add  with data-id: 2
Parameters invocation value for method add
2
3
listener: Running method add  with data-id: 2
invoked add

===============================================
Default Suite
Total tests run: 3,Skips: 0
===============================================

猜你在找的Java相关文章