在JUnit测试用例中指定执行顺序 [英] Specifying order of execution in JUnit test case

查看:163
本文介绍了在JUnit测试用例中指定执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试用例,我添加一个实体,更新它并删除它。因此,执行顺序在这里很重要。我希望它是:

I have a test case where I add an entity, update it and delete the same. Hence, the order of execution is important here. I want it to be :


  1. 创建

  2. 更新

  3. 删除

奇怪的是,对于一个测试用例(15个中),JUnit按以下顺序执行:

Strangely, for just one test case ( out of 15) , JUnit executes it in the following order :


  1. 删除

  2. 更新

  3. 创建。

如何告诉JUnit按特定顺序执行它们?在其他情况下,JUnit完全正常(串行执行)。为什么JUnit在这种情况下表现得很奇怪?

How do I tell JUnit to execute them in a specific order ? In other cases, JUnit works totally fine ( executing serially ) . And why does JUnit behave weirdly in this one case ?

以下相关代码段:

    private static Date date;
    private static int entity;
    static Parking p;
    public ParkingTests(String name) {
       super(name);
    }
    public void testAdd() throws Exception {
           //Add code here
    }
    public void testUpdate() throws Exception {
            //update code here
    }
    public void testDelete() throws Exception {
            //delete code here
    }
  }

它变得怪异。我作为套件的一部分运行了很多测试用例。如果我只运行停车箱,则维持订单。如果我和其他人一起运行它,有时会保持,有时不会!

It gets weirder. I run a lot of test cases as part of a suite. If I run just the Parking case, the order is maintained. If I run it along with others, it is sometimes maintained, sometimes not !

推荐答案

您的情况 尴尬,因为为了保持重复工作感觉不好隔离测试(见下文) - 但请注意,大部分复制可以拉入 setUp tearDown @Before @After )方法,因此您不需要额外的代码。如果测试运行速度太慢而不能经常停止运行,那么最好在清洁测试的名义上浪费一些CPU。

Your kind of situation is awkward, as it feels bad to keep duplicating work in order to isolate the tests (see below) - but note that most of the duplication can be pulled out into setUp and tearDown (@Before, @After) methods, so you don't need much extra code. Provided that the tests are not running so slowly that you stop running them often, it's better to waste a bit of CPU in the name of clean testing.

public void testAdd() throws Exception {
      // wipe database
      // add something
      // assert that it was added
}
public void testUpdate() throws Exception {
      // wipe database
      // add something
      // update it
      // assert that it was updated
}
public void testDelete() throws Exception {
      // wipe database
      // add something
      // delete it
      // assert that it was deleted
}

另一种方法是将所有内容都放在一个带有多个断言的测试中,但这很难理解和维护,并在测试失败时提供少量信息:

The alternative is to stick everything into one test with multiple asserts, but this is harder to understand and maintain, and gives a bit less information when a test fails:

public void testCRUD() throws Exception {
      // wipe database
      // add something
      // assert that it was added
      // update it
      // assert that it was updated
      // delete it 
      // assert that it was deleted
}

使用数据库或集合或任何类型的存储进行测试很棘手,因为一个测试可以总是通过将垃圾留在数据库/集合中来影响其他测试。即使你的测试没有明确地依赖彼此,它们仍然可能互相干扰,特别是如果其中一个失败。

Testing with databases or collections or storage of any kind is tricky because one test can always affect other tests by leaving junk behind in the database/collection. Even if your tests don't explicitly rely on one another, they may still interfere with one another, especially if one of them fails.

尽可能使用新的实例对于每个测试,或擦除数据,理想情况下尽可能简单 - 例如对于数据库而言,擦除整个表的可能性比非常特定的删除更容易成功,您可能会意外地出错。

Where possible, use a fresh instance for each test, or wipe the data, ideally in as simple a way as possible - e.g. for a database, wiping an entire table is more likely to succeed than a very specific deletion that you might accidentally get wrong.

更新:通常在测试开始时擦除数据会更好,因此一次测试运行失败不会影响下一次运行。

Update: It's usually better to wipe data at the start of the test, so one failed test run doesn't affect the next run.

这篇关于在JUnit测试用例中指定执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆