TestNG:确定接下来的测试方法 [英] TestNG: Identifying which tests methods are next

查看:165
本文介绍了TestNG:确定接下来的测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在每个测试方法之后清除()我的javax.persistence.EntityManager。

My goal is to clear() my javax.persistence.EntityManager after each test method.

以下是测试类的示例:

public class Example
{
    @Test(dataProvider = "sampleDataProvider")
    public void testA(String parameter)
    {
        System.out.println(parameter);
    }

    @Test(dataProvider = "sampleDataProvider")
    public void testB(String parameter)
    {
        System.out.println(parameter);
    }
}

在dataProvidersampleDataProvider中使用entityManager查询数据库中的测试数据,然后以这种格式编译: new Object [2] [1] 。请记住,数据的查询和编译都是在实际运行测试方法(使用@DataProvider注释)之前完成的,并且我们实际上是在查询实体而不仅仅是字符串。

The entityManager is used in the dataProvider "sampleDataProvider" by querying the DB for test data which is then compiled in this format: new Object[2][1]. Keep in mind that the querying and compiling of data is all done before a test method (annotated with @DataProvider) is actually run and that we're actually querying for entities and not just Strings.

上面的测试类运行如下:

The above test class would run like so:

testA("some queried entity 1")
testA("some queried entity 2")
testB("some queried entity 1")
testB("some queried entity 2")

我的初始解决方案是使用 @AfterTest 注释来清除entityManager。但是它会在 testA 一些查询的实体2 c>和 testB 会导致对某个查询实体2成员的读/写操作出现问题。

My initial solution was to use the @AfterTest annotation to clear the entityManager. However it would detach "some queried entity 2" from the entityManager before the second-runs (or second test instances) of testA and testB which causes problems on read/write operations to the members of "some queried entity 2".

我的目标是在测试方法之后清除entityManager ,而不一定在每个测试方法实例之后清除。

My goal is to clear the entityManager after a test method, and not necessarily after every instance of a test method.

TestNG能否知道接下来要运行哪个测试?这样我就可以轻松清除entityManager,如果下一个测试是新的。

Does TestNG make it possible to know which test is run next? That way I could easily clear the entityManager if the next test is a new one.

还有其他建议吗?

推荐答案

我不知道有什么简单的方法来获得下一个测试方法。顺便说一句,你可以使用@AfterGroups注释获得相同的结果:

I don't know about any easy way to get next test method. BTW, you can achieve the same result using @AfterGroups annotation:

@Test(groups = {"groupA"})
public void testA(...) { ... }

@Test(groups = {"groupB"})
public void testB(...) { ... }

@AfterGroups(groups = {"groupA"})
public void afterGroupA() {
    // clear entity manager here
}

注意,TestNG无法保证测试开始的顺序,直到您使用@Test的dependsOnMethods或dependsOnGroups参数明确指定它testng.xml文件中标记的注释或保​​留顺序属性。

Note, that order of tests starting is not guaranteed by TestNG, until you specify it explicitly with dependsOnMethods or dependsOnGroups parameters of @Test annotation, or "preserve-order" attribute of tag in testng.xml file.

更新

作为替代方案,您可以使用自己的 TestListiner 。您可以实施 onTestStart()方法如果有必要,将进行清算。 AFAIU,调用的测试方法在部分填充的ITestResult中可用。顺便说一句,小心 - 这可能会导致隐式测试逻辑引起的问题(从测试代码中看不到清除)。

As an alternative you can use your own TestListiner. You can implement onTestStart() method, which will do clearing, if it is necessary. AFAIU, invoked test method is available in partially filled ITestResult. BTW, be careful - this may lead to problems caused by implicit test logics (clearing will not be visible from test code).

这篇关于TestNG:确定接下来的测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆