如何编写多个测试的 Spring 测试套件并运行选择性测试?
我在一个测试类中有很多 spring 测试方法。我只想进行选择性测试。所以我想在同一类中创建一个测试套件。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestersChoice {
@Test
@Transactional
public void testAddAccount(){
///do something ....
}
@Test
@Transactional
public void testDeleteAccount(){
///do something ....
}
@Test
@Transactional
public void testReadAccount(){
///do something ....
}
如果
我运行这个 TestersChoice 类,所有测试都将运行!我只想运行 testReadAccount 而不是其余的。我想创建套件来运行选择性测试。 (我想避免删除上面的测试方法@Test来实现这一点) 类似于 jUnit testcase 中的东西。这就是我通过将 TestersChoice 类扩展到 TestCase 并插入此方法所能做到的:
public static TestSuite suite(){
TestSuite suite = new TestSuite();
suite.addTest(new TestersChoice("testDeleteAccount"));
return suite;
}
但是现在我没有扩展 TestCase,所以我无法将 TestersChoice 实例添加到套件中!
如何进行选择性测试?
I have many spring test methods in a test class. i want to run only selective tests. So i want to create a test suite in same class.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestersChoice {
@Test
@Transactional
public void testAddAccount(){
///do something ....
}
@Test
@Transactional
public void testDeleteAccount(){
///do something ....
}
@Test
@Transactional
public void testReadAccount(){
///do something ....
}
}
If I run this Class TestersChoice all tests will run! I just want to run testReadAccount not the rest. I want to create suite to run selective tests. (I want to avoid deleting @Test above test methods to achieve this)
Something like in jUnit testcase . This is what i was able to do by extending TestersChoice class to TestCase and inserting this method:
public static TestSuite suite(){
TestSuite suite = new TestSuite();
suite.addTest(new TestersChoice("testDeleteAccount"));
return suite;
}
But as now I am not extending TestCase so I am unable to add TestersChoice instance to suite!
How to run selective tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要对测试进行分组,那么问题不在于 spring-test,而在于无法对测试进行分组的 JUnit。考虑切换到 TestNG(也受 spring OOTB 支持)。
TestNG 基于 JUnit 构建,但功能更强大:请参阅比较。
分组很容易。
问候,
斯泰因
if what you want is to group your tests then the issue is not with spring-test but rather with JUnit where grouping tests is not possible. Consider switching to TestNG (also supported by spring OOTB).
TestNG is built on JUnit but a lot more powerfull: See comparison.
Grouping is easy.
regards,
Stijn
您可以使用 Spring 的
IfProfileValue
(如果您始终使用@RunWith(SpringJUnit4ClassRunner.class)
),然后仅当您使用指定特定系统属性时才会运行测试 - D<属性名称>
。You can use Spring's
IfProfileValue
(if you are always using@RunWith(SpringJUnit4ClassRunner.class)
) and then tests will only run if you specify particular system properties using-D<propertyname>
.对您不想执行的每个方法使用
@Ignore
。以您的示例:所有标记为
@Ignore
的方法都不会被执行Use the
@Ignore
on each method whom you do not want to execute. Taking your example:All methods marked as
@Ignore
will not be executed