简体   繁体   English

具有多个@Test方法的Junit测试类

[英]Junit test class with multiple @Test methods

I have a Junit test class with multiple @Test methods in it that I need to run in order. 我有一个Junit测试类,其中包含多个@Test方法,我需要按顺序运行。 If there is an exception thrown in a method I would like to stop entire test case and error out, but all of the rest of the test methods running. 如果在方法中抛出异常,我想停止整个测试用例并输出错误,但所有其他测试方法都在运行。

public class{

@Test{
 //Test1 method`enter code here`
}

@Test{
 //Test2 method
}

@Test{
 //Test3 method
}

}

If Test1 method fails then don't run other Tests 如果Test1方法失败,则不要运行其他测试

Note: All are independent tests 注意:所有都是独立测试

Unit tests should be designed to run independently of one another. 单元测试应设计为彼此独立运行。 The order of execution cannot be guaranteed. 执行顺序无法保证。 You should redesign your test class so that the order is not important. 您应该重新设计测试类,以便顺序不重要。

Without further information it's hard to advise you specifically. 没有进一步的信息,很难具体告诉你。 But it may help to have an @before method, which checks for some precondition before running each test. 但是有一个@before方法可能有所帮助,该方法在运行每个测试之前检查一些前提条件。 If you included an Assume.assumeTrue(...) method call, then your test could be skipped if the condition fails? 如果你包含了一个Assume.assumeTrue(...)方法调用,那么如果条件失败你的测试可以跳过吗?

As discribed here , JUnit 4.11 supports the ordered execution with the Annotation @FixMethodOrder , but the others are right, all tests should be independent of each other. 如此处所述 ,JUnit 4.11支持使用Annotation @FixMethodOrder进行有序执行,但其他测试是正确的,所有测试应该相互独立。

At the end of a test you can set a global success flag. 在测试结束时,您可以设置全局成功标志。 This flag will be tested at the begining of each test. 该标志将在每次测试开始时进行测试。 If the flag is not set by the end of one test(because it fails before finishing) all other tests will fail too. 如果在一次测试结束时未设置该标志(因为它在完成之前失败),则所有其他测试也将失败。 Example: 例:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConsecutiveFail{
  private boolean success = true;

  @Test
  public void test1{
    //fist two statements in all tests
    assertTrue("other test failed first", success);
    success = false;
    //do your test
    //...

    //last statement
    success = true;
  }

  @Test
  public void test2{
    //fist two statements in all tests
    assertTrue("other test failed first", success);
    success = false;
    //do your test
    //...

    //last statement
    success = true;
  }
}

I was able to achieve what you are looking with intellij idea IDE, I am using community edition. 我能用intellij idea IDE实现你所期待的,我正在使用社区版。

Go to Edit Configuration in the class where test methods available.( Run --> Edit Configurations) 转到可用测试方法的类中的编辑配置。(运行 - >编辑配置)

Select Test kind as " Class " as in the below Image. 选择Test kind as“ Class ”,如下图所示。

在此输入图像描述

When you run the Class Test it will execute all @Test Annotated methods inside the class as in below Picture. 当您运行Class Test时,它将在类中执行所有@Test Annotated方法,如下图所示。

在此输入图像描述

如果您需要保留后果并且未通过测试而不使整个测试失败,请将所有此类测试放在一个测试中并通过假设进行测试。

Here's example for TESTNG how to specify test running order: 以下是TESTNG如何指定测试运行顺序的示例:

@Test(priority = 1)
public void test1(){}

@Test(priority = 2)
public void test2(){}

@Test(priority = 3)
public void test3(){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM