Java 我可以在TestNG测试用例上指定一个类范围的组吗?
我在TestNG中有一个表示数据库测试的基类,我想指定从这个类扩展的所有类都属于一个组“db test”,但是我发现这似乎是不可能的。我尝试了@Test注释:Java 我可以在TestNG测试用例上指定一个类范围的组吗?,java,testng,Java,Testng,我在TestNG中有一个表示数据库测试的基类,我想指定从这个类扩展的所有类都属于一个组“db test”,但是我发现这似乎是不可能的。我尝试了@Test注释: @Test(groups = { "db-test" }) public class DBTestBase { } 但是,这不起作用,因为@Test注释将尝试将一组方法放入测试中,并且当测试运行时,eclipse中会弹出警告/错误 因此,我尝试禁用测试,因此至少分配了组: @Test(enabled = false, groups =
@Test(groups = { "db-test" })
public class DBTestBase {
}
但是,这不起作用,因为@Test注释将尝试将一组方法放入测试中,并且当测试运行时,eclipse中会弹出警告/错误
因此,我尝试禁用测试,因此至少分配了组:
@Test(enabled = false, groups = { "db-test" })
public class DBTestBase {
}
但是任何@BeforeTest(和其他类似的注释)也会被禁用。。。这当然不是我想要的
我想用某种方法将类注释为特定类型的组,但在TestNG中似乎不太可能。有人有其他想法吗?我不确定注释继承如何适用于TestNG,但这可能有一些用处
实际上,这可能需要看inheritGroups。TestNG将从带有@Test注释的类中运行所有公共方法。也许您可以将不希望TestNG运行的方法更改为非公共的您可以在方法级别指定@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);
}
}
这对你有用吗,或者我在你的问题中遗漏了什么。在我看来,这似乎是以下代码挑战(社区维基帖子): 如何能够执行“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");
}
}
@Test(groups={“aGlobalGroup”})
公共阶级基础{
/**
*
*/
@课前
公共最终无效设置(){
System.out.println(“基类:@BeforeClass”);
}
/**
*测试不是“aGlobalGroup”的一部分,但由于类注释,仍然包含在该组中。
*即使测试的TestNG类是子类,也将执行。
*/
@测试(组={“aLocalGroup”})
公开最终失效测试(){
System.out.println(“基类:快速测试”);
}
/**
*测试不是“aGlobalGroup”的一部分,但由于类注释,仍然包含在该组中。
*即使测试的TestNG类是子类,也将执行。
*/
@测试(组={“aLocalGroup”})
公开最终作废aSlowTest(){
System.out.println(“基类:慢测试”);
//抛出新的IllegalArgumentException(“oups”);
}
/**
*不应执行。
*然而,类的全局注释测试将把它包含在TestNG方法中。。。
*/
公开最终无效测试(){
System.out.println(“基类:非测试”);
}
/**
*TestNG类的子类。它的一些方法是TestNG方法,另一些不是。
*目标是检查在超类中指定的组是否包含此类的方法。
*并避免包含太多的方法,例如不打算作为TestNG方法的公共方法。
*@作者
*/
公共静态类扩展基
{
/**
*测试不是“aGlobalGroup”的一部分,但由于超类注释,仍然包含在该组中。
*即使测试的TestNG类是子类,也将执行。
*/
@试验
公共最终作废扩展测试(){
System.out.println(“扩展类:扩展测试”);
}
/**
*不应执行。
*然而,类的全局注释测试将把它包含在TestNG方法中。。。
*/
公共最终无效不可撤销测试(){
System.out.println(“扩展类:不是扩展测试”);
}
}
答案是通过一个自定义的org.testng.IMethodSelector:
它的includeMethod()可以排除我们想要的任何方法,就像一个未注释的公共方法一样
但是,要注册自定义Java MethodSelector,必须将其添加到任何TestRunner管理的XMLTest实例中,这意味着您需要自己的自定义TestRunner
但是,要构建定制的TestRunner,您需要通过-testrunfactory选项注册一个TestRunneFactory
但是,TestNG类从不考虑-testrunfactory…因此您还需要定义一个自定义的TestNG类:
- 为了覆盖configure(Map)方法
- 因此,您可以实际设置TestRunnerFactory
- TestRunnerFactory将为您构建一个定制的TestRunner
- TestRunner,它将为XMLTest实例设置一个自定义XMLMethodSelector
- XMLMethodSelector,它将生成自定义的IMethodSelector
- IMethodSelector,它将排除您选择的任何测试方法
- 一个java类(以及相当多的内部类)
- 将类复制粘贴到“source/test”目录中(因为包是“test”)
- 运行它(不需要参数)
迈克·斯通的更新: 我会接受这一点,因为这听起来和我最终做的非常接近,但我想我也会加上我所做的 基本上,我创造
@Groups({ "myGroup1", "myGroup2"})
public class MyTestCase {
@Test
@Groups("aMethodLevelGroup")
public void myTest() {
}
}