如何编写多个测试的 Spring 测试套件并运行选择性测试?

发布于 2024-10-18 21:21:19 字数 995 浏览 3 评论 0原文

我在一个测试类中有很多 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 技术交流群。

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

发布评论

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

评论(3

陪我终i 2024-10-25 21:21:19

如果您想要对测试进行分组,那么问题不在于 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

待天淡蓝洁白时 2024-10-25 21:21:19

您可以使用 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>.

哽咽笑 2024-10-25 21:21:19

对您不想执行的每个方法使用@Ignore。以您的示例:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/testApplicationContext.xml"}) @TransactionConfiguration(defaultRollback=true) public class TestersChoice  { 


@Test  @Transactional @Ignore public void testAddAccount(){           ///do something .... }  


@Test @Transactional @Ignore public void testDeleteAccount(){          ///do something .... }   

@Test @Transactional public void testReadAccount(){           ///do something .... }

所有标记为 @Ignore 的方法都不会被执行

Use the @Ignore on each method whom you do not want to execute. Taking your example:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/testApplicationContext.xml"}) @TransactionConfiguration(defaultRollback=true) public class TestersChoice  { 


@Test  @Transactional @Ignore public void testAddAccount(){           ///do something .... }  


@Test @Transactional @Ignore public void testDeleteAccount(){          ///do something .... }   

@Test @Transactional public void testReadAccount(){           ///do something .... }

All methods marked as @Ignore will not be executed

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