TestNG 测试继承和组

发布于 2024-12-09 03:04:59 字数 1345 浏览 0 评论 0原文

我们有 DAO 测试,应该针对真实的 DAO/数据库和模拟 dao 运行,以验证模拟 dao 的行为与真实 dao 相同。为此,我们有这样的结构:

public abstract class DAOTestBase
{
    public void testSimple()
    {
        // dummy assertion
        assertTrue(true, "Hello");
    }
}


@Test(groups = "fast")
public class TestMockDAO extends DAOTestBase
{
    // setUp/tearDown and helper methods for mock
}


@Test(groups = "slow")
public class TestDAO extends DAOTestBase
{
    // setUp/tearDown and helper methods for real DB
}

不幸的是,这不起作用 - TestNG 不认为 testSimple 方法是测试,因此不会运行它。因此,我尝试注释 testSimple 方法(或 DAOTestBase 类):

  • 没有任何组的 @Test 注释将导致相同的效果 - 测试不会针对 fastslow 组运行。

  • 带有fastslow组的@Test注释将导致相反的效果 - TestMockDAO和无论仅运行快速测试还是仅运行慢速测试,TestDAO 都将运行。

  • 具有不同组的 @Test 注释,例如 common,并在两个 TestMockDAO 中添加 dependsOnGroups="common" 注释TestDAO 也将不起作用,除非 common 包含在要运行的组中,这再次导致上面的情况 2(都是 TestMockDAOTestDAO 运行)。

最后,我正在寻找一种能够为子类中继承的测试定义组的方法,但似乎 @Test 注释仅适用于测试同一类中的方法,而不是没有 @Test 注释的继承方法。有没有其他方法可以实现此目的(无需重写子类中的所有方法)?

We have DAO tests that should run against both the real DAO/database, and against a mock dao to verify that the mock dao behaves the same as the real dao. To this end, we have a structure like this:

public abstract class DAOTestBase
{
    public void testSimple()
    {
        // dummy assertion
        assertTrue(true, "Hello");
    }
}


@Test(groups = "fast")
public class TestMockDAO extends DAOTestBase
{
    // setUp/tearDown and helper methods for mock
}


@Test(groups = "slow")
public class TestDAO extends DAOTestBase
{
    // setUp/tearDown and helper methods for real DB
}

Unfortunately this doesn't work - TestNG doesn't think that the testSimple method is a test and hence won't run it. So instead I tried to annotate the testSimple method (or the DAOTestBase class):

  • A @Test annotation without any groups will lead to the same effect - the test won't run for either fast nor slow groups.

  • A @Test annotation with groups fast and slow will lead to the opposite effect - both TestMockDAO and TestDAO will be run regardless of whether only fast or only slow tests should be run.

  • A @Test annotation with a different group, say common, plus added dependsOnGroups="common" annotations in both TestMockDAO and TestDAO will also not work unless common is included in the groups to run which leads again to case 2 above (both TestMockDAO and TestDAO are run).

In the end, what I'm looking for is a way to be able to define the group for the inherited tests in the sub class, but it seems as if the @Test annotation is only applied to test methods in that very same class, not also to inherited methods that don't have a @Test annotation. Is there any other way to achieve this (without overriding all methods in the sub classes) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随风而去 2024-12-16 03:04:59

我目前正在经历类似的情况。

运行测试用例的一种方法是使用类似以下内容:

@Test
public void someTest() {
  TestNG testng = new TestNG();
  testng.setTestClasses(new Class[] { SomeTests.class });
  testng.run();
}

参考:http: //testng.org/doc/documentation-main.html#running-testng-programmatically

不幸的是,我目前无法让它报告 SomeTests 中的测试用例。

I am currently working through a similar situation.

A way to make test cases run is to use something like:

@Test
public void someTest() {
  TestNG testng = new TestNG();
  testng.setTestClasses(new Class[] { SomeTests.class });
  testng.run();
}

Reference: http://testng.org/doc/documentation-main.html#running-testng-programmatically

Unforunately I am currently unable to get it to report the test cases within SomeTests.

三生一梦 2024-12-16 03:04:59

您是否尝试过在 DAOTestBase 之上简单地添加 @Test 注释?每个子类都会用自己的组覆盖它,这应该使基类中的方法成为测试方法。

Have you tried simply adding a @Test annotation on top of DAOTestBase? Each subclass will override it with its own group and this should make the method in the base a test method.

心意如水 2024-12-16 03:04:59

我使用的是 TestNG 6.14.3 版本,我找到了使用 priority 注释的解决方案。

示例:

  1. 我有一个基本测试类:

    公共类 TestBase {    
       @测试(优先级= 0)
       公共无效测试A(){        
           断言True(真,“测试A”);
       }
    }
    
  2. 还有另一个扩展测试类:

    public class Test2 扩展 TestBase {
       @测试(优先级= 1)
       公共无效测试B(){        
           断言True(真,“测试B”);
       }
    

    }

  3. 当我运行 Test2 测试类时,我得到以下结果:

    testA:true
    测试B:正确
    

I am using TestNG 6.14.3 version and I found a solution using priority annotation.

Example:

  1. I have a base test class:

    public class TestBase {    
       @Test(priority = 0)
       public void testA()  {        
           assertTrue(true, "testA");
       }
    }
    
  2. And another extended test class:

    public class Test2 extends TestBase {
       @Test(priority = 1)
       public void testB() {        
           assertTrue(true, "testB");
       }
    

    }

  3. When I run Test2 test class, I obtain the following esult:

    testA: true
    testB: true
    
沫雨熙 2024-12-16 03:04:59

我这样解决了:

基类中的方法位于“base”组中,但需要检查测试是否已初始化。

public abstract DaoTestBase {
    private boolean initialized = false;

    @Test(groups = "base")
    public void testSimple() {
        if (!initialized) { return; }

        // dummy assertion
        assertTrue(true, "Hello");
    }
}

该测试在子级中的 BeforeClass 带注释的方法中初始化。

@BeforeClass
protected void initialize() {
    super.initialized = true;
}

如果您注释父类而不是方法,则必须传递inheritGroups=false 和组,因为它也继承基类的组并且它将不起作用。

现在,您必须运行 TestNG 来检查基本、快速或基本、慢速组。两项测试都会被执行,但未初始化的测试将不会执行任何操作。

它很丑陋,我不会推荐它(重新定义 child 中的方法并调用相应的 super 方法看起来更好),但就我而言,我需要优先考虑我的测试方法,并且我想避免每个孩子中的重复班级。

I solved it this way:

The methods in the base class are in "base" group, but need to check if the test have been initialized.

public abstract DaoTestBase {
    private boolean initialized = false;

    @Test(groups = "base")
    public void testSimple() {
        if (!initialized) { return; }

        // dummy assertion
        assertTrue(true, "Hello");
    }
}

The test is initialized in the child, in the BeforeClass annotated method.

@BeforeClass
protected void initialize() {
    super.initialized = true;
}

If you annotate the parent class instead of the methods, you must pass inheritGroups=false and the group, since it inherits also the group of the base class and it will not work.

Now, you must run TestNG to check groups base,fast or base,slow. Both tests will be executed, but the one not initialized will do nothing.

It is ugly, and I would not recommend it (it looks better to redefine the methods in child and call the according super method), but in my case I need priority in my test methods, and I want to avoid that repetition in each child class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文