16

我有一个表示 TestNG 中的数据库测试的基类,我想指定从该类扩展的所有类都属于“db-test”组,但是我发现这似乎不可能。我已经尝试过 @Test 注释:

@Test(groups = { "db-test" })
public class DBTestBase {
}

但是,这不起作用,因为 @Test 注释会尝试将一堆方法放入测试中,并且在运行测试时会在 eclipse 中弹出警告/错误。

所以我尝试禁用测试,所以至少分配了组:

@Test(enabled = false, groups = { "db-test" })
public class DBTestBase {
}

但随后任何@BeforeTest(和其他类似的注释)也被禁用......这当然不是我想要的。

我想用某种方法将一个类注释为特定类型的组,但在 TestNG 中似乎不太可能。有没有人有任何其他想法?

4

5 回答 5

4

TestNG 将运行带有@Test 注解的类中的所有公共方法。也许您可以将不希望 TestNG 运行的方法更改为非公开的

于 2008-09-16T18:06:40.610 回答
2

答案是通过自定义org.testng.IMethodSelector

它的includeMethod()可以排除我们想要的任何方法,比如公共的未注释方法。

但是,要注册自定义Java MethodSelector,您必须将其添加到任何 TestRunner 管理的XMLTest实例中,这意味着您需要自己的自定义 TestRunner

但是,要构建自定义 TestRunner,您需要通过-testrunfactory选项注册一个TestRunnerFactory 。

但是TestNG类从不考虑 -testrunfactory ......所以您还需要定义一个自定义 TestNG 类:

  • 为了覆盖 configure(Map) 方法,
  • 所以你可以实际设置 TestRunnerFactory
  • TestRunnerFactory 将为您构建一个自定义的 TestRunner,
  • TestRunner 将为 XMLTest 实例设置一个自定义 XMLMethodSelector
  • XMLMethodSelector 将构建一个自定义的 IMethodSelector
  • IMethodSelector 将排除您选择的任何 TestNG 方法!

好吧……这是一场噩梦。但这也是一个代码挑战,所以一定有点挑战;)

所有代码都可以在DZone 片段中找到。

像往常一样进行代码挑战:

  • 一个 java 类(和相当多的内部类)
  • 将类复制粘贴到“源/测试”目录中(因为包是“测试”)
  • 运行它(不需要参数)

迈克·斯通的更新:

我会接受这一点,因为它听起来非常接近我最终所做的,但我想我也会添加我所做的。

基本上,我创建了一个 Groups 注释,其行为类似于 Test(和其他)注释的 groups 属性。

然后,我创建了一个 GroupsAnnotationTransformer,它使用 IAnnotationTransformer 查看正在定义的所有测试和测试类,然后修改测试以添加组,这与组排除和包含完美配合。

修改构建以使用新的注释转换器,这一切都完美无缺!

嗯......一个警告是它不会将组添加到非测试方法......因为在我这样做的时候,有另一个注释转换器可以让你转换任何东西,但它不包括在内在我出于某种原因使用的TestNG中......所以最好让你的前/后注释方法总是运行=真......这对我来说已经足够了。

最终结果是我可以做到:

@Groups({ "myGroup1", "myGroup2"})
public class MyTestCase {
    @Test
    @Groups("aMethodLevelGroup")
    public void myTest() {
    }
}

我让转换器与子类化和所有东西一起工作。

于 2008-11-08T23:12:31.067 回答
1

我不确定 TestNG 的注释继承是如何工作的,但这篇文章可能会有一些用处。

实际上,这可能会有所帮助,请查看inheritGroups。

于 2008-08-12T19:03:22.737 回答
1

在我看来,这是以下代码挑战(社区 wiki 帖子):

如何能够从组“aGlobalGroup”执行扩展类的所有测试方法,而无需:

  • 在扩展类本身上指定“aGlobalGroup”组?
  • 测试扩展类的非注释公共方法?

第一个答案很简单:
在基类级别添加一个类 TestNG(groups = { "aGlobalGroup" })

该组将适用于基类和扩展类的所有公共方法。

但是:即使是非 testng 公共方法(没有 TestNG 注释)也将包含在该组中。

挑战:避免包含那些非 TestNG 方法。

@Test(groups = { "aGlobalGroup" })
public class Base {

    /**
     * 
     */
    @BeforeClass
     public final void setUp() {
           System.out.println("Base class: @BeforeClass");
     }


    /**
     * Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br />
     * Will be executed even if the TestNG class tested is a sub-class.
     */
    @Test(groups = { "aLocalGroup" })
     public final void aFastTest() {
       System.out.println("Base class: Fast test");
     }

    /**
     * Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br />
     * Will be executed even if the TestNG class tested is a sub-class.
     */
    @Test(groups = { "aLocalGroup" })
     public final void aSlowTest() {
        System.out.println("Base class: Slow test");
        //throw new IllegalArgumentException("oups");
     }

     /**
      * Should not be executed. <br />
      * Yet the global annotation Test on the class would include it in the TestNG methods...
     */
    public final void notATest() {
       System.out.println("Base class: NOT a test");
     }

    /**
     * SubClass of a TestNG class. Some of its methods are TestNG methods, other are not. <br />
     * The goal is to check if a group specify in the super-class will include methods of this class. <br />
     * And to avoid including too much methods, such as public methods not intended to be TestNG methods.
     * @author <a href="http://stackoverflow.com/users/6309/vonc">VonC</a>
     */
    public static class Extended extends Base
    {
        /**
         * Test not part a 'aGlobalGroup', but still included in that group due to the super-class annotation. <br />
         * Will be executed even if the TestNG class tested is a sub-class.
         */
        @Test
        public final void anExtendedTest() {
           System.out.println("Extended class: An Extended test");
        }

        /**
         * Should not be executed. <br />
         * Yet the global annotation Test on the class would include it in the TestNG methods...
         */ 
        public final void notAnExtendedTest() {
            System.out.println("Extended class: NOT an Extended test");
        }
    }
于 2008-11-08T22:57:34.233 回答
0

您可以在允许最大灵活性的方法级别指定 @Test 注释。

public class DBTestBase {

    @BeforeTest(groups = "db-test")
    public void beforeTest() {
        System.out.println("Running before test");
    }

    public void method1() {
        Assert.fail(); // this does not run. It does not belong to 'db-test' group.
    }

    @Test(groups = "db-test")
    public void testMethod1() {
        Assert.assertTrue(true);
    }
}

这对你有用还是我从你的问题中遗漏了一些东西。

于 2008-09-30T17:23:10.673 回答