如何预先定义junit测试用例的运行顺序?

发布于 2024-10-20 08:28:30 字数 82 浏览 2 评论 0原文

我在java中有一个测试类,其中有几个由@Test注释的方法,不知何故,当我运行整个测试时,我想Junit在方法B之前运行方法A。有可能或者有必要吗?

I have a test class in java and there are several methods annotated by @Test in it, somehow, i want to Junit run method A before method B when i run the whole tests. Is it possible or necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

这种对测试方法的依赖是糟糕的设计,应该避免。如果一个测试方法中有初始化代码需要为下一个测试方法完成,则应将其分解为 setUp 方法。

This sort of dependency on test methods is bad design and should be avoided. If there is initialization code in one test method that needs to be done for the next, it should be factored out into a setUp method.

踏月而来 2024-10-27 08:28:30

我遇到的问题是报告。如果您想/需要查看每种测试方法是否失败或通过,那么您就完蛋了。

我知道您不希望一个测试建立在以前的测试之上,但无论如何,在某些情况下您可能需要它来执行此操作(或者您会将测试的复杂性增加一个数量级)。

代码中的测试流程应该由测试开发人员还是框架开发人员决定?

向 10 位 Java 开发人员展示 JUnit 测试代码,我敢打赌大多数人都会假设测试(无论任何外部内容)将按照它们在测试类中出现的顺序运行。

这不应该是 JUnit 的默认行为吗? (让我可以选择告诉它顺序,而不是让 JUnit 自己弄清楚”。)

更新:2014-11-18
新版本的 JUnit 支持方法排序器

// 这会按字母顺序保存测试
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

我认为如果您“真的”想要执行自己的特定顺序,您也许可以创建自己的方法排序器。

The problem I have with this is reporting. If you WANT/NEED to see if each test method fails or passes then you're SCREWED.

I understand that you don't want one test to build upon previous tests, But regardless of that, there may be situations that you need it to do this (or you'll increase the complexity of the test by an order of magnitude).

Should the flow of tests in the code be up to the developer of the tests or the developer of the framework ?

Show JUnit test code to 10 java developers, and I'll be willing to bet most will assume that the tests (regardless of anything external) will be run in the order they appear in the test class.

Shouldn't THAT be the default behaviour of JUnit ? (Give me the option of telling it the order instead of JUnit figuring it out" on its own.)

Update: 2014-11-18
The newer version of JUnit supports method sorters

// This saves the tests in alphabetical order
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

I would think that you might be able to create your own method sorter if you "really" wanted do do your own specific order.

云胡 2024-10-27 08:28:30

测试应该有独立的顺序,但有时我们没有我们想要的。
如果您有一个包含数千个测试的大型遗留项目,并且它们取决于它们的执行顺序,那么当您尝试在 java 7 上迁移时,您将会遇到很多问题,因为它会打乱所有测试。

您可以在这里阅读有关此问题的更多信息:

junit test ordering and java 7

Tests should have independent order, but some times we have not what we want.
If you have a large legacy project with thousands of tests, and they depends on their execution order, you will have many problems, when, for example you will try to migrate on java 7, because it will shuffle all tests.

You can read more about this problem here:

junit test ordering and java 7

岁吢 2024-10-27 08:28:30

如果只有两种方法,那么您需要将其包装在一个真正不依赖于顺序的单元测试中。

@Test
public void testInOrder() throws Exception {
    testA(); 
    testB(); 
}

If it's only two methods then you'd need to wrap it in a single unit test that truly isn't order-dependent.

@Test
public void testInOrder() throws Exception {
    testA(); 
    testB(); 
}
深海少女心 2024-10-27 08:28:30

使用以下内容在测试之前和之后进行设置

@Before
public void setUp() throws MWException {

}

@After
public void tearDown() {

}

use the following to setup thing before and after tests

@Before
public void setUp() throws MWException {

}

@After
public void tearDown() {

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文