在 JUnit 测试类中运行基准测试方法 [英] Running benchmark methods in a JUnit test class

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

问题描述

我有一个测试类 MyTestClass,其中包含几个典型的测试方法,可以执行断言或检查异常,并使用 @Test 进行注释.当我在 Ant 任务中调用以下指令时,它们会运行:

I have a test class MyTestClass with several typical test methods that do assertions or check for exceptions and are annotated with @Test. They run when I call the following directive in an Ant task:

然而,在同一个类中,我想放置几个未包含在必须通过的测试中的基准性能方法.我想要一个完全独立的 Ant 任务来运行这些基准测试.

However, in the same class I would like to situate several benchmark performance methods that are not included in the tests that must be passed. I would like to have an altogether separate Ant task that runs these benchmarking tests.

是否有带注释的方式来介绍这种测试分类并分别调用每个类别?例如.@Test 注释方法将是第一类,然后虚构的 @Benchmark 方法将只是记录诊断报告的性能指标,而不是进行成败的断言.我认为测试类是放置这些基准测试方法的最佳方式,但我不知道如何将它们的调用封装在 Ant 中,以便它们单独运行.例如,是否可以为每个方法加上基准"前缀并告诉 Ant 只运行以该为前缀的方法,或者我可以编写自己的注释吗?

Is there an annotated way to introduce this classification of tests and invoke each category separately? E.g. the @Test annotated methods would be 1st class and then the fictitious @Benchmark methods would simply be logging performance metrics for diagnostics reporting, not do assertions that are make or break. I think the test class is the best way to situate these benchmarking methods, yet I don't know how to encapsulate their invocation in Ant so that they are run separately. E.g., is it possible to prefix each method with "benchmark" and tell Ant to run only methods that are prefixed with that or can I write my own annotation?

推荐答案

我想你可能想看看 JUnit 类别.还有其他答案详细说明了如何从 ant 执行特定类别.

I think you probably want to look at JUnit Categories. There are other answers that detail how to execute specific categories from ant.

如何运行所有 JUnit使用 Ant 在类别/套件中进行测试?
如何运行JUnit 4 中属于某个类别的所有测试

一些可能有用的外部链接:
使用 JUnit 分组测试类别
深入了解 JUnit 类别

Some external links that may be of use:
Grouping tests using JUnit categories
A Closer Look at JUnit Categories

这篇关于在 JUnit 测试类中运行基准测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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