譬如我在某个test类里面定义2个test methods:@Test public void testA(){}
@Test public void testB(){}
我怎么保证每次运行的时候testA()都在testB()之前执行?如果JUnit根本没有这个特性,为什么它的设计者不设计者个特性?

解决方案 »

  1.   

    TestNG 利用 Test 注释的 dependsOnMethods 属性来应对测试的依赖性问题。有了这个便利的特性,就可以轻松指定依赖方法。例如,前面所说的登录将在某个方法之前 运行。此外,如果依赖方法失败,它将被跳过,而不是标记为失败。
      

  2.   


    如果一定要在JUnit4里面设定执行顺序,有什么办法?(请不要强调解释为啥要测试独立)
      

  3.   

    http://download.csdn.net/source/2689888
    If you really need hard dependency between your JUnit test, try JExample extensionJExample introduces producer-consumer relationships to unit-testing.
    A producer is a test method that yields its unit under test as return value.
    A consumer is a test method that depends on one or more producers and their return values.You can install it in Eclipse, for Junit4.4 or 4.5.import jexample.Depends;  @Test
      @Depends("#testEmpty") 
      public Stack<Integer> testPush(Stack<Integer> $) { 
          $.push(42); 
          assertFalse($.isEmpty()); 
          return $; 
      }