如何在JUnit4中按特定顺序运行测试方法?

本文探讨了在JUnit4中如何按特定顺序运行测试方法,因为JUnit默认按方法名的字母顺序执行。尽管这可能不是最佳实践,但可以通过命名约定或使用特殊注解来实现顺序控制。文章引用了JUnit的改进提案和解决方案,以及一些测试开发者对于测试顺序需求的讨论。
摘要由CSDN通过智能技术生成

本文翻译自:How to run test methods in specific order in JUnit4?

I want to execute test methods which are annotated by @Test in specific order. 我想执行以特定顺序由@Test注释的测试方法。

For example: 例如:

public class MyTest {
    @Test public void test1(){}
    @Test public void test2(){}
}

I want to ensure to run test1() before test2() each time I run MyTest , but I couldn't find annotation like @Test(order=xx) . 我想确保在每次运行MyTest时在test2() test1()之前运行test1() ,但我找不到像@Test(order=xx)这样的注释。

I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature , why? 我认为这对JUnit来说非常重要,如果JUnit的作者不想要订单功能 ,为什么?


#1楼

参考:https://stackoom.com/question/FUsc/如何在JUnit-中按特定顺序运行测试方法


#2楼

The (as yet unreleased) change https://github.com/junit-team/junit/pull/386 introduces a @SortMethodsWith . (尚未发布)更改https://github.com/junit-team/junit/pull/386引入了@SortMethodsWith https://github.com/junit-team/junit/pull/293 at least made the order predictable without that (in Java 7 it can be quite random). https://github.com/junit-team/junit/pull/293至少在没有它的情况下使订单可预测(在Java 7中它可以是非常随机的)。


#3楼

See my solution here: "Junit and java 7." 在这里查看我的解决方案:“Junit和java 7.”

In this article I describe how to run junit tests in order - "just as in your source code". 在本文中,我将介绍如何按顺序运行junit测试 - “就像在源代码中一样”。 Tests will be run, in order as your test methods appears in class file. 将按照测试方法出现在类文件中的顺序运行测试。

http://intellijava.blogspot.com/2012/05/junit-and-java-7.html http://intellijava.blogspot.com/2012/05/junit-and-java-7.html

But as Pascal Thivent said, this is not a good practise. 但正如Pascal Thivent所说,这不是一个好习惯。


#4楼

Look at a JUnit report. 查看JUnit报告。 JUnit is already organized by package. JUnit已经按包组织。 Each package has (or can have) TestSuite classes, each of which in turn run multiple TestCases. 每个包都有(或可以有)TestSuite类,每个类依次运行多个TestCase。 Each TestCase can have multiple test methods of the form public void test*() , each of which will actually become an instance of the TestCase class to which they belong. 每个TestCase都可以有多个public void test*()形式的测试方法,每个测试方法实际上都会成为它们所属的TestCase类的一个实例。 Each test method (TestCase instance) has a name and a pass/fail criteria. 每个测试方法(TestCase实例)都有一个名称和通过/失败标准。

What my management requires is the concept of individual TestStep items, each of which reports their own pass/fail criteria. 我的管理层需要的是各个TestStep项目的概念,每个项目都报告自己的通过/未通过标准。 Failure of any test step must not prevent the execution of subsequent test steps. 任何测试步骤的失败都不得妨碍后续测试步骤的执行。

In the past, test developers in my position organized TestCase classes into packages that correspond to the part(s) of the product under test, created a TestCase class for each test, and made each test method a separate "step" in the test, complete with its own pass/fail criteria in the JUnit output. 在过去,我职位的测试开发人员将TestCase类组织成与被测产品的部件相对应的包,为每个测试创建一个TestCase类,并使每个测试方法成为测试中的单独“步骤”,在JUnit输出中完成自己的通过/失败标准。 Each TestCase is a standalone "test", but the individual methods, or test "steps" within the TestCase, must occur in a specific order. 每个TestCase都是独立的“测试”,但TestCase中的各个方法或测试“步骤”必须按特定顺序进行。

The TestCase methods were the steps of the TestCase, and test designers got a separate pass/fail criterion per test step. TestCase方法是TestCase的步骤,测试设计者在每个测试步骤中获得单独的通过/失败标准。 Now the test steps are jumbled, and the tests (of course) fail. 现在测试步骤混乱,测试(当然)失败。

For example: 例如:

Class testStateChanges extends TestCase

public void testCreateObjectPlacesTheObjectInStateA()
public void testTransitionToStateBAndValidateStateB()
public void testTransitionToStateCAndValidateStateC()
public void testTryToDeleteObjectinStateCAndValidateObjectStillExists()
public void testTransitionToStateAAndValidateStateA()
public void testDeleteObjectInStateAAndObjectDoesNotExist()
public void cleanupIfAnythingWentWrong()

Each test method asserts and reports its own separate pass/fail criteria. 每种测试方法都断言并报告其自己的单独通过/未通过标准。 Collapsing this into "one big test method" for the sake of ordering loses the pass/fail criteria granularity of each "step" in the JUnit summary report. 为了订购,将其折叠为“一个大的测试方法”会丢失JUnit摘要报告中每个“步骤”的通过/失败标准粒度。 ...and that upsets my managers. ......这让我的经理感到不安。 They are currently demanding another alternative. 他们目前要求另一种选择。

Can anyone explain how a JUnit with scrambled test method ordering would support separate pass/fail criteria of each sequential test step, as exemplified above and required by my management? 任何人都可以解释一个带有加扰测试方法排序的JUnit如何支持每个顺序测试步骤的单独通过/失败标准,如上面举例说明并且我的管理人员要求?

Regardless of the documentation, I see this as a serious regression in the JUnit framework that is making life difficult for lots of test developers. 无论文档如何,我都认为这是JUnit框架中的一个严重的回归,它使许多测试开发人员的生活变得困难。


#5楼

I've read a few answers and agree its not best practice, but the easiest way to order your tests - and the way that JUnit runs tests by default is by alphabetic name ascending. 我已经阅读了一些答案并同意它不是最佳实践,但是订购测试的最简单方法 - 以及JUnit默认运行测试的方式是按字母名称升序。

So just name your tests in the alphabetic order that you want. 因此,只需按照您想要的字母顺序命名测试。 Also note the test name must begin with the word test. 另请注意,测试名称必须以单词test开头。 Just watch out for numbers 请注意数字

test12 will run before test2 test12将在test2之前运行

so: 所以:

testA_MyFirstTest testC_ThirdTest testB_ATestThatRunsSecond testA_MyFirstTest testC_ThirdTest testB_ATestThatRunsSecond


#6楼

Migration to TestNG seems the best way, but I see no clear solution here for jUnit. 迁移到TestNG似乎是最好的方法,但我看不到jUnit的明确解决方案。 Here is most readable solution / formatting I found for jUnit: 这是我为jUnit找到的最易读的解决方案/格式

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
    @Test
    void stage1_prepareAndTest(){};

    @Test
    void stage2_checkSomething(){};

    @Test
    void stage2_checkSomethingElse(){};

    @Test
    void stage3_thisDependsOnStage2(){};

    @Test
    void callTimeDoesntMatter(){}
}

This ensures stage2 methods are called after stage1 ones and before stage3 ones. 这确保stage1方法在stage1之后和stage3之前调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值