简体   繁体   English

JUnit 4 按照在 class 中的编程顺序执行测试

[英]JUnit 4 Test Execution in the order how they are programmed in class

I am using JUnit and RestAssured create API tests.我正在使用 JUnit 和 RestAssured 创建 API 测试。

I was wondering if there is a way to execute test cases in the order how they are written in the class file.我想知道是否有一种方法可以按照在 class 文件中的写入顺序执行测试用例。 Currently when I execute them it seems random.目前,当我执行它们时,它似乎是随机的。

I tried @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and adding @Order(xy) into the tests but I think that this didn't help me.我尝试@TestMethodOrder(MethodOrderer.OrderAnnotation.class)并将@Order(xy)添加到测试中,但我认为这对我没有帮助。

Just to describe my problem:只是为了描述我的问题:

I have multiple tests in the following order in the class: Post tests, Get Tests, Delete tests.我在 class 中按以下顺序进行了多个测试:发布测试、获取测试、删除测试。 As you may already know I want Delete tests to be executed as very last.正如您可能已经知道的那样,我希望最后执行删除测试。

Is it possible to do it somehow?有可能以某种方式做到吗?

Thanks谢谢

Short answer: no.简短的回答:没有。 IMO it is not even possible because your order of methods is not visible in the generated byte code, but I'm not a byte code expert and may be wrong. IMO 甚至不可能,因为您的方法顺序在生成的字节码中不可见,但我不是字节码专家,可能是错误的。

The annotation @TestMethodOrder is part of JUnit Jupiter (JUnit 5) and therefore it does not work with JUnit 4 tests.注释@TestMethodOrder是 JUnit Jupiter (JUnit 5) 的一部分,因此它不适用于 JUnit 4 测试。 JUnit 4 has an annotation @FixMethodOrder that allows you to execute test methods in alphabetic order. JUnit 4 有一个注解@FixMethodOrder ,允许您按字母顺序执行测试方法。 By prefixing your test names with some String like a_ you can achieve what you want.通过在你的测试名称前加上一些像a_这样的字符串,你可以实现你想要的。 I know it's ugly.我知道这很丑。

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

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