指定在方法级别(而不是类级别)联合 4 个测试的顺序

发布于 2024-09-06 16:09:33 字数 102 浏览 3 评论 0原文

我知道这是不好的做法,但它需要完成,否则我需要切换到 testng。有没有一种类似于 JUnit 3 的 testSuite 的方法来指定要在类中运行的测试的顺序?

I know this is bad practice, but it needs to be done, or I'll need to switch to testng. Is there a way, similar to JUnit 3's testSuite, to specify the order of the tests to be run in a class?

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

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

发布评论

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

评论(6

吐个泡泡 2024-09-13 16:09:33

如果您确定您真的想要这样做:可能有更好的方法,但这就是我能想到的...

JUnit4 有一个注释:@RunWith 可以让您覆盖测试的默认 Runner。

在您的情况下,您需要创建一个特殊的 BlockJunit4ClassRunner 子类,并重写computeTestMethods()以按照您希望执行的顺序返回测试。例如,假设我想按相反的字母顺序执行测试:

public class OrderedRunner extends BlockJUnit4ClassRunner {

    public OrderedRunner(Class klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected List computeTestMethods() {
        List list = super.computeTestMethods();
        List copy = new ArrayList(list);
        Collections.sort(copy, new Comparator() {
            public int compare(FrameworkMethod o1, FrameworkMethod o2) {
                return o2.getName().compareTo(o1.getName());
            }
        });
        return copy;
    }
}
@RunWith(OrderedRunner.class)
public class OrderOfTest {
    @Test public void testA() { System.out.println("A"); }
    @Test public void testC() { System.out.println("C"); }
    @Test public void testB() { System.out.println("B"); }
}

运行此测试会产生:

C
B
A

对于您的特定情况,您需要一个比较器,它可以按照您希望执行的顺序按名称对测试进行排序。 (我建议使用类似 Google Guava 的类 Ordering.explicit("methodName1","methodName2").onResultOf(...); 来定义比较器,其中 onResultOf 提供了一个将 FrameworkMethod 转换为它的名字......虽然显然你可以自由地以任何你想要的方式实现它。

If you're sure you really want to do this: There may be a better way, but this is all I could come up with...

JUnit4 has an annotation: @RunWith which lets you override the default Runner for your tests.

In your case you would want to create a special subclass of BlockJunit4ClassRunner, and override computeTestMethods() to return tests in the order you want them executed. For example, let's say I want to execute my tests in reverse alphabetical order:

public class OrderedRunner extends BlockJUnit4ClassRunner {

    public OrderedRunner(Class klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected List computeTestMethods() {
        List list = super.computeTestMethods();
        List copy = new ArrayList(list);
        Collections.sort(copy, new Comparator() {
            public int compare(FrameworkMethod o1, FrameworkMethod o2) {
                return o2.getName().compareTo(o1.getName());
            }
        });
        return copy;
    }
}
@RunWith(OrderedRunner.class)
public class OrderOfTest {
    @Test public void testA() { System.out.println("A"); }
    @Test public void testC() { System.out.println("C"); }
    @Test public void testB() { System.out.println("B"); }
}

Running this test produces:

C
B
A

For your specific case, you would want a comparator that would sort the tests by name in the order you want them executed. (I would suggest defining the comparator using something like Google Guava's class Ordering.explicit("methodName1","methodName2").onResultOf(...); where onResultOf is provided a function that converts FrameworkMethod to its name... though obviously you are free to implement that any way you want.

无声无音无过去 2024-09-13 16:09:33

我可以看到这样做的几个原因,特别是在使用 JUnit 运行功能测试或测试持久对象时。例如,考虑一个持久存储到某种持久存储中的对象 Article。如果我想按照单元测试原则“所有测试都应该可重新排序并仅测试功能的特定部分”来测试 Article 对象上的插入、更新和删除功能,我将拥有三个测试:

  • testInsertArticle()
  • testUpdateArticle()
  • testDeleteArticle()

但是,为了能够测试更新功能,我首先需要插入文章。为了测试删除功能,我还需要插入一篇文章。因此,实际上,插入功能已经在 testUpdateArticle()testDeleteArticle() 中进行了测试。然后,很容易创建一个测试方法 testArticleFunctionality() 来完成这一切,但这样的方法最终会变得庞大(而且它们不会只测试 的部分功能)文章 对象)。

针对诸如 Restful API 之类的运行功能测试也是如此。如果不是测试的不确定顺序,JUnit 对于这些情况也非常有用。

也就是说,我扩展了 Michael D 的 OrderedRunner 以使用注释来确定测试顺序,只是认为我应该分享。它可以进一步扩展,例如通过准确指定每个测试所依赖的测试,但这就是我现在使用的。

这是它的使用方式。它避免了命名测试的需要,例如 AA_testInsert()AB_testUpdate()AC_testDelete()、...、ZC_testFilter( ) 等。

@RunWith(OrderedRunner.class)
public class SomethingTest {
    @Test
    @Order(order=2)
    public void testUpdateArticle() {
        // test update
    }

    @Test
    @Order(order=1)
    public void testInsertArticle() {
        // test insert
    }

    @Test
    @Order(order=3)
    public void testDeleteArticle() {
        // test delete
    }
}

无论这些测试如何放置在文件中,它们始终都会以 order=1 首先、order=2 第二和最后运行order=3,无论您是从 Eclipse 内部、使用 Ant 还是任何其他方式运行它们。

实施如下。首先,注释Order

@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    public int order();
}

然后,修改OrderedRunner

public class OrderedRunner extends BlockJUnit4ClassRunner {
    public OrderedRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> copy = new ArrayList<>(super.computeTestMethods());
        Collections.sort(list, new Comparator<FrameworkMethod>() {
            @Override
            public int compare(FrameworkMethod f1, FrameworkMethod f2) {
                Order o1 = f1.getAnnotation(Order.class);
                Order o2 = f2.getAnnotation(Order.class);
        
                if(o1==null && o2 == null) return 0;
                if (o1 == null) return 1;
                if (o2 == null) return -1;

                return o1.order() - o2.order();
            }
        });
        return list;
    }
}

I can see several reasons for doing this, especially when using JUnit to run functional tests or test persistent objects. For example, consider an object Article which is persisted to some kind of persistent storage. If I would like to test the insert, update and delete functionality on the Article object following the unit test principle "all tests should be reorderable and test only a specific part of the functionality", I would have three tests:

  • testInsertArticle()
  • testUpdateArticle()
  • testDeleteArticle()

However, to be able to test the update functionality, I would first need to insert the article. To test the delete functionality, I would also need to insert an article. So, in practice, the insert functionality is already tested both in testUpdateArticle() and testDeleteArticle(). It is then tempting to just create a test method testArticleFunctionality() which does it all, but methods like that will eventually get huge (and they won't just test part of the functionality of the Article object).

The same goes for running functional tests against for example a restful API. JUnit is great also for these cases if it wasn't for the undeterministic ordering of tests.

That said, I extended Michael D's OrderedRunner to use annotations to determine order of tests, just thought I should share. It can be extended further, for example by specifying exactly which tests each test depends on, but this is what I'm using for now.

This is how it is used. It avoids the need for naming tests like AA_testInsert(), AB_testUpdate(), AC_testDelete(), ..., ZC_testFilter(), etc.

@RunWith(OrderedRunner.class)
public class SomethingTest {
    @Test
    @Order(order=2)
    public void testUpdateArticle() {
        // test update
    }

    @Test
    @Order(order=1)
    public void testInsertArticle() {
        // test insert
    }

    @Test
    @Order(order=3)
    public void testDeleteArticle() {
        // test delete
    }
}

No matter how these tests are placed in the file, they will always be run as order=1 first, order=2 second and last order=3, no matter if you run them from inside Eclipse, using Ant, or any other way.

Implementation follows. First, the annotation Order.

@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    public int order();
}

Then, the modified OrderedRunner.

public class OrderedRunner extends BlockJUnit4ClassRunner {
    public OrderedRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> copy = new ArrayList<>(super.computeTestMethods());
        Collections.sort(list, new Comparator<FrameworkMethod>() {
            @Override
            public int compare(FrameworkMethod f1, FrameworkMethod f2) {
                Order o1 = f1.getAnnotation(Order.class);
                Order o2 = f2.getAnnotation(Order.class);
        
                if(o1==null && o2 == null) return 0;
                if (o1 == null) return 1;
                if (o2 == null) return -1;

                return o1.order() - o2.order();
            }
        });
        return list;
    }
}
滥情哥ㄟ 2024-09-13 16:09:33

从 JUnit 版本 4.11 开始,可以通过使用 @FixMethodOrder 注释您的类并指定任何可用的 MethodSorters 来影响测试执行的顺序。请参阅链接了解更多详情。

From JUnit version 4.11 onwards, it is possible to influence the order of test execution by annotating your class with @FixMethodOrder and specifying any of the available MethodSorters. See this link for more details.

ゝ偶尔ゞ 2024-09-13 16:09:33

使用 junit 4.11 新注释 @FixMethodOrder 允许设置特定顺序:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

Using junit 4.11 the new annotation @FixMethodOrder allows to set a specific order:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
遥远的绿洲 2024-09-13 16:09:33

如果您想按照“就像源代码中出现的那样”的顺序运行 junit 测试,
并且不想修改您的测试代码,
请参阅我关于此的注释:

如何按顺序运行 junit 测试它们出现在你的源代码中

但这确实不是一个好主意,测试必须是独立的。

If you want to run junit tests in order "just as they present in your source code",
and don't want to modify your tests code,
see my note about this here:

How to run junit tests in order as they present in your source code

But it is really not a good idea, tests must be independent.

泪眸﹌ 2024-09-13 16:09:33

Joscarsson 和 Michael D 在我的 github 存储库中编写代码。我希望他们不介意。我还为参数化类提供了有序版本。它已经用作 Maven 依赖项

<repositories>
    <repository>
        <id>git-xxx</id>
        <url>https://github.com/crsici/OrderedRunnerJunit4.11/raw/master/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.sici.org.junit</groupId>
    <artifactId>ordered-runner</artifactId>
    <version>0.0.1-RELEASE</version>
</dependency>

Joscarsson and Michael D code in my github repo. I hope they don't mind. I also provide ordered version for Parameterized class. It's already to use as maven dependency

<repositories>
    <repository>
        <id>git-xxx</id>
        <url>https://github.com/crsici/OrderedRunnerJunit4.11/raw/master/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.sici.org.junit</groupId>
    <artifactId>ordered-runner</artifactId>
    <version>0.0.1-RELEASE</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文