简体   繁体   English

如何仅在达到特定测试之前运行junit测试

[英]How do you run junit tests only till a particular test is reached

I have a bunch of dependent test cases (Not dependent via dependsOn parameter) and can't run one test at a time. 我有一堆依赖的测试用例(不依赖于dependsOn参数),并且一次不能运行一个测试。 That's why I want to run tests till a particular test is reached. 这就是为什么我要运行测试直到达到特定测试的原因。 I want to stop executing further tests and want to call teardowns. 我想停止执行进一步的测试,并希望进行拆卸。

Is there a way I can do this? 有办法吗?

You can control the test case execution by org.junit.Assume.assumeTrue(condition), Which ignores the test cases if it found the condition to be false. 您可以通过org.junit.Assume.assumeTrue(condition)控制测试用例的执行,如果发现条件为假,它将忽略测试用例。 Sets the condition to be true in the beginning, a private static boolean variable can be user for example and the sets the condition to false in the end of the last test case that you want to be executed. 在开始时将条件设置为true,例如,可以使用用户一个私有的静态布尔变量,在要执行的最后一个测试用例的结尾将条件设置为false。 Checks the condition in @BeforeTest. 检查@BeforeTest中的条件。

Sample code 样例代码

private static boolean runTest = true;

@org.junit.Before
public void before() throws Exception {
    org.junit.Assume.assumeTrue(runTest);
}

@org.junit.Test
public void test1() throws Exception {
    // will be executed
}

@org.junit.Test
public void test2() throws Exception {
    // will be executed
}

@org.junit.Test
public void test3() throws Exception {
    // will be executed
    runTest = false;
    // no test after this will be executed
}

@org.junit.Test
public void test4() throws Exception {

    // will be ignored

}

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

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