5

我在一个测试类中有很多弹簧测试方法。我只想进行选择性测试。所以我想在同一个班级创建一个测试套件。

@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 ....
}   

}

如果我运行这个 Class TestersChoice,所有测试都会运行!我只想运行 testReadAccount 而不是其余的。我想创建套件来运行选择性测试。(我想避免删除 @Test 以上测试方法来实现这一点)类似于 jUnit testcase 的东西。这就是我能够通过将 TestersChoice 类扩展到 TestCase 并插入此方法来做到的:

public static TestSuite suite(){
      TestSuite suite = new TestSuite();
       suite.addTest(new TestersChoice("testDeleteAccount"));
      return suite;
}

但是现在我没有扩展 TestCase 所以我无法将 TestersChoice 实例添加到套件中!

如何进行选择性测试?

4

3 回答 3

1

您可以使用 Spring IfProfileValue(如果您一直使用@RunWith(SpringJUnit4ClassRunner.class)),然后只有在您使用-D<propertyname>.

于 2011-04-05T12:52:37.703 回答
1

如果您想要对测试进行分组,那么问题不在于 spring-test,而在于无法对测试进行分组的 JUnit。考虑切换到 TestNG(spring OOTB 也支持)。

TestNG 建立在 JUnit 之上,但功能更强大:参见比较

分组很容易。

问候,
Stijn

于 2011-03-10T08:17:43.243 回答
0

@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都不会被执行

于 2013-07-02T10:36:19.140 回答