当前位置: 首页 > 知识库问答 >
问题:

当我们使用TestNG数据提供程序而不是工厂时,ITest是否有效

颜鸿云
2023-03-14

我正试图使用ITest接口从数据提供商处获取测试的自定义名称(用于Jenkins reports)。我尝试了下面这样的示例程序,以了解TestNG在什么时候调用gettestname()方法。

粘贴输出和程序。

正如你在下面看到的那样,输出是令人困惑的,有些地方测试名称也是“空”的。所以我的问题是Itest是否被设计用于数据提供商?此外,当我们使用并行=true运行下面的数据提供程序时,它会变得更加混乱,因为成员“testName”是跨线程共享的,并且只有一个类实例在运行。

我可以看到ITest如何与@factory一起使用,因为当我们使用工厂时,每个testmethod运行都会调用一个新的TestClass,所以这不会是一个问题。

输出:

TestNGThreadingTest:31 - DataProvider running on Thread id = 1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = null
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
PASSED: test1("test1")
PASSED: test2("test2")
PASSED: test3("test3")
PASSED: test4("test4")
PASSED: test5("test5")

输出[当并行=真时]

TestNGThreadingTest:31 - DataProvider running on Thread id = 1
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = null
TestNGThreadingTest:43 - Before Method Thread id = 11 TestName = test3
TestNGThreadingTest:43 - Before Method Thread id = 13 TestName = test5
TestNGThreadingTest:43 - Before Method Thread id = 9 TestName = test1
TestNGThreadingTest:43 - Before Method Thread id = 12 TestName = test4
TestNGThreadingTest:43 - Before Method Thread id = 10 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 11 TestName = test3
TestNGThreadingTest:37 - Run Method Thread id = 12 TestName = test4
TestNGThreadingTest:37 - Run Method Thread id = 9 TestName = test1
TestNGThreadingTest:37 - Run Method Thread id = 10 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 13 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 12 TestName = test4
TestNGThreadingTest:48 - After Method Thread id = 10 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 9 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 11 TestName = test3
TestNGThreadingTest:48 - After Method Thread id = 13 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
PASSED: test2("test3")
PASSED: test2("test4")
PASSED: test2("test1")
PASSED: test2("test2")
PASSED: test2("test5")

代码:

public class TestNGThreadingTest implements ITest {

public String testName;

final static Logger logger = Logger.getLogger(TestNGThreadingTest.class);

@DataProvider(name="dp")
public Iterator<Object[]> getTests() {
    Collection<Object[]> tests = new ArrayList<Object[]>();

    tests.add(new Object[]{"test1"});
    tests.add(new Object[]{"test2"});
    tests.add(new Object[]{"test3"});
    tests.add(new Object[]{"test4"});
    tests.add(new Object[]{"test5"});

    logger.info("DataProvider running on Thread id = " + Thread.currentThread().getId());
    return tests.iterator();
}

@Test(dataProvider="dp")
public void run(String testName) {
    logger.info("Run Method Thread id = " + Thread.currentThread().getId() + " TestName = "+ testName );
}

@BeforeMethod
public void init(Object[] testArgs) {
    this.testName = (String) testArgs[0];
    logger.info("Before Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testArgs[0]);
}

@AfterMethod
public void tearDown(Object[] testArgs) {
    logger.info("After Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testArgs[0]);
}

@Override
public String getTestName() {
    logger.info("GetTestname Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testName);
    return testName;
}

}

共有1个答案

祁默
2023-03-14

这是一个一致性问题(可能是测试问题?)。

通过ThreadLocal重新放置String testName

或者您也可以使用链接到@DataProvider@Factory,但@Factory可能无法并行工作。

 类似资料:
  • 我在一个新项目上使用了Guice 4.0 Beta 2,之前在一个项目上使用了Guice 3.0。 在我当前的项目中,有一个对象,它是使用从HTTP请求派生的: 绑定看起来像这样: 到目前为止还不错。这和预期的一样。请注意,其他类依赖于将Application ationConfiguration注入其中。 这就是我遇到麻烦的地方:我有一些批次处理作业需要进行。批处理过程不是由HTTP请求启动的。

  • 我使用下面的代码读取CSV文件,并将对象传递给被测方法public void launchWCM1(IBLogonDataCSV data)。 当方法public void launchWCM1(IBLogonDataCSV data)通过或失败时,Testng报告将显示对象名称,这无助于了解字符串值或失败URL的名称,以便进一步调试。 示例:launchWCM1测试类:com。硒测试。测验IBL

  • 我想在类中与数据提供者并行运行测试方法。我需要一个数据提供者,它每次在新测试方法开始为给定的测试运行生成部分动态数据之前都会被调用。让我用伪代码解释一下: 我怎样才能做到这一点?

  • 问题内容: 但是我仍然不知道您何时在工厂使用服务。 据我所知,工厂通常用于创建可被多个控制器调用的“通用”功能:创建通用控制器功能 Angular文档似乎更喜欢工厂而不是服务。他们甚至在使用工厂时也提到“服务”,这更加令人困惑!http://docs.angularjs.org/guide/dev_guide.services.creating_services 那么什么时候可以使用服务呢? 有什

  • 请容忍我在这里。我知道还有其他答案,比如:AngularJS:服务vs提供商vs工厂 然而,我仍然不知道你什么时候会使用工厂服务。 据我所知,factory通常用于创建可由多个控制器调用的“公共”函数:创建公共控制器函数 Angular的医生似乎更喜欢工厂而不是服务。他们甚至在使用工厂时提到“服务”,这更令人困惑!http://docs.angularjs.org/guide/dev_guide.

  • 问题内容: 类允许客户获得实例的通常方法是提供一个公共构造器。另一种方法是提供一个公共的静态工厂方法,该方法只是一个返回类实例的静态方法。使用静态工厂方法的优缺点是什么? 问题答案: 《有效Java》一书中的这一章对此进行了很好的解释:考虑使用Static Factory而不是Constructors 。它以您可以理解的最佳方式说明了它们各自的优缺点。 只是引用本书的优点和缺点: 优点 : 静态工