gpt4 book ai didi

java - 如何使 JUnit 测试用例按顺序运行?

转载 作者:IT老高 更新时间:2023-10-28 20:55:50 27 4
gpt4 key购买 nike

我正在使用 JUnit4。

我在一个测试用例中有一套测试方法。

每个测试方法插入一些记录并验证一个测试结果,最后删除插入的记录。

Since the JUnit run in parallel, test methods fail because of some records present during the execution of previous test method. This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6).

我们需要的是测试方法必须在我们所有的机器中通过。

我已尝试清除 Setup() 方法中的记录,但它再次仅适用于我的机器。 JUnit 中是否有任何选项可以使测试方法以统一的顺序运行?

谢谢,

最佳答案

MethodSorters 是 Junit 4.6 发布后引入的一个新类。该类声明了三种执行顺序,可以在执行时在您的测试用例中使用。

  1. NAME_ASCENDING(MethodSorters.NAME_ASCENDING) - 对测试方法进行排序按方法名称,按字典顺序排列。

  2. JVM(null) - 按照返回的顺序保留测试方法JVM。请注意,来自 JVM 的顺序因运行而异。

  3. DEFAULT(MethodSorter.DEFAULT) - 对测试方法进行排序确定性但不可预测的顺序。

.

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

//Running test cases in order of method names in ascending order

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTestCasesExecution {

@Test
public void secondTest() {
System.out.println("Executing second test");
}

@Test
public void firstTest() {
System.out.println("Executing first test");
}

@Test
public void thirdTest() {
System.out.println("Executing third test");
}
}

输出:

Executing first test
Executing second test
Executing third test

引用:http://howtodoinjava.com/2012/11/24/ordered-testcases-execution-in-junit-4/

关于java - 如何使 JUnit 测试用例按顺序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com