本文介绍了如何确保 TestNG 在测试类上连续运行方法而不是交错运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个测试类,每个类都有几个测试方法.所有测试在后台使用相同的测试数据库.每个测试类初始化其数据库内容,然后在几个测试方法中测试内容.

I have several test classes, each with several test methods. All tests use the same test database in the background. Each test class initializes its database contents and then tests stuff in several test methods.

当我单独运行每个测试时,它们都通过了.但是当我同时运行多个测试时(使用 maven 或我的 IDE,IntelliJ),不同测试类的方法是交错运行的,例如.第二个类的数据库初始化在第一个类启动之后但在第一个类的所有测试方法运行之前运行,因此这些方法将失败(因为数据库已经包含第二个类的数据).

When I run each test individually, they all pass. But when I run several tests at the same time (either using maven or my IDE, IntelliJ), the methods of different test classes are run interleaved, eg. the database initialization of the second class runs after the first class has started but before all test methods of first class have been run so these methods will fail (because the database already contains second class's data).

最简单的解决方案是强制 TestNG runner 连续运行这些类(即,在运行另一个类的测试方法之前等待一个测试类的所有测试方法完成).可以这样做吗?

The simplest solution would be to force the TestNG runner to run the classes in succession (ie. wait for all the test methods of a test class to finish before running test methods of another class). Can this be done?

我可能可以通过在我的套件中将每个类指定为单独的测试来做到这一点,但我不想这样做,因为这意味着每当我添加一个测试类时我都必须向套件添加一些东西笨拙且容易出错.

I can probably do this by specifying each class as a separate test in my suite, but I don't want to do this as this means I'd have to add something to the suite whenever I add a test class which is clumsy and error-prone.

简单地要求 TestNG 不并行化任何东西(例如,将线程数设置为 1 或禁用并行运行)在这里没有帮助,因为方法仍然以错误的顺序运行(尽管不是同时运行).

Simply asking TestNG to not parallelize anything (eg. setting thread count to 1 or disabling parallel running) doesn't help here since methods still get run in the wrong order (though not simultaneously).

一种选择是为每个测试类使用不同的数据库,但我没有看到一种简单的方法来做到这一点(使用 JPA 和 Guice).

One option would be to use a different database for each test class, but I don't see a simple way to do this (using JPA and Guice).

我目前没有使用 DBUnit、Unitils 等;我不太了解这些工具,但我觉得它们不能解决我的问题.

I'm not currently using DBUnit, Unitils etc.; I don't know these tools very well but I got the impression the don't solve my problems.

我使用 JPA 在每个测试类中初始化数据库(即创建实体对象并保留它们).

I'm using JPA to initialize database in each test class (ie. create entity objects and presist them).

推荐答案

即使在顺序模式下,TestNG 也可以交叉使用同一套件中的测试方法.它确实保证了@BeforeClass -> @Test -> @AfterClass 的顺序,但它可以执行以下操作:

Even in sequential mode TestNG could interleave test methods from the same suite. It does guarantee the sequence @BeforeClass -> @Test -> @AfterClass but it can do something like:

before class1
    test class1.method1
before class2
    test class2.method1
    test class1.method2
after class1
    test class2.method2
after class2

解决方案是将每个类强制在不同的套件中(真正按顺序执行).从 2.16 版本开始,maven surefire 插件将每个类放在一个单独的套件中,因此问题得到解决.

The solution is to force each class in a different suite (which are executed truly sequentially). As of version 2.16, the maven surefire plugin puts each class in a separate suite so the problem is fixed.

另一方面,IDEA(甚至是最新的 13 EAP)生成一个 xml 文件,其中包含同一套件中的所有类.希望 IDEA 也能效仿并解决这个问题.在处理共享资源(例如数据库)时,交错测试是一大亮点.

On the other hand, IDEA (even the latest 13 EAP) generates an xml file with all classes in the same suite. Hopefully IDEA will follow suit and fix this too. Interleaved tests are a showstopper when working with shared resources such as databases.

这篇关于如何确保 TestNG 在测试类上连续运行方法而不是交错运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:04