Running benchmark methods in a JUnit test class

2020-05-09 08:54发布

问题:

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:

<test name="MyTestClass"/>

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.

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?

回答1:

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

How to run all JUnit tests in a category/suite with Ant?
How to run all tests belonging to a certain Category in JUnit 4

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



标签: java ant junit