TestNG 测试继承和组
我们有 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
注释将导致相同的效果 - 测试不会针对fast
和slow
组运行。带有
fast
和slow
组的@Test
注释将导致相反的效果 -TestMockDAO
和无论仅运行快速测试还是仅运行慢速测试,TestDAO
都将运行。具有不同组的
@Test
注释,例如common
,并在两个TestMockDAO 中添加
和dependsOnGroups="common"
注释TestDAO
也将不起作用,除非common
包含在要运行的组中,这再次导致上面的情况 2(都是TestMockDAO
和TestDAO
运行)。
最后,我正在寻找一种能够为子类中继承的测试定义组的方法,但似乎 @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 eitherfast
norslow
groups.A
@Test
annotation with groupsfast
andslow
will lead to the opposite effect - bothTestMockDAO
andTestDAO
will be run regardless of whether only fast or only slow tests should be run.A
@Test
annotation with a different group, saycommon
, plus addeddependsOnGroups="common"
annotations in bothTestMockDAO
andTestDAO
will also not work unlesscommon
is included in the groups to run which leads again to case 2 above (bothTestMockDAO
andTestDAO
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我目前正在经历类似的情况。
运行测试用例的一种方法是使用类似以下内容:
参考: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:
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.
您是否尝试过在 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.我使用的是 TestNG 6.14.3 版本,我找到了使用
priority
注释的解决方案。示例:
我有一个基本测试类:
还有另一个扩展测试类:
}
当我运行
Test2
测试类时,我得到以下结果:I am using TestNG 6.14.3 version and I found a solution using
priority
annotation.Example:
I have a base test class:
And another extended test class:
}
When I run
Test2
test class, I obtain the following esult:我这样解决了:
基类中的方法位于“base”组中,但需要检查测试是否已初始化。
该测试在子级中的 BeforeClass 带注释的方法中初始化。
如果您注释父类而不是方法,则必须传递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.
The test is initialized in the child, in the BeforeClass annotated method.
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.