使用junitperf运行junit4测试

2 junit junitperf

Junitperf可以和junit4一起使用吗?我有一个简单的Junit4测试类,有几个测试,我想在该类的单个测试中做一个TimedTest.我怎样才能做到这一点?

为了更清楚我的Junit4类是这样的:

public class TestCitta {

    @Test
    public void test1 {}

        @Test
    public void test2 {}
}
Run Code Online (Sandbox Code Playgroud)

与junit3我写下类似的东西:

public class TestCittaPerformance {

    public static final long toleranceInMillis = 100;

    public static Test suite() {

        long maxElapsedTimeInMillis = 1000 + toleranceInMillis;

        Test testCase = new TestCitta("test2");

        Test timedTest = new TimedTest(testCase, maxElapsedTimeInMillis);

        return timedTest;
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }
}
Run Code Online (Sandbox Code Playgroud)

与Junit4?

小智 8

我遇到了同样的问题,但试图让它在不同的构建环境中运行并不幸运.所以我使用了自JUnit 4以来可用的@Rule功能来注入性能测试调用和使用注释进行需求检查.原来它变成了一个小型库,在这个项目中取代了JUnitPerf,我以的名义发布了它.如果您对此方法感兴趣,可以在https://github.com/lucaspouzac/contiperf找到它.


小智 5

如果你已经有junit4,为什么不使用contiperf.它会做你正在寻找的东西和注释.

POM就是这样的.

 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.databene</groupId>
  <artifactId>contiperf</artifactId>
  <version>2.0.0</version>
  <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

测试类是这样的

public class PersonDAOTest {
@Rule
public ContiPerfRule i = new ContiPerfRule();
Run Code Online (Sandbox Code Playgroud)

而实际测试就是这样的

@Test
@PerfTest(invocations = 1, threads = 1)
@Required(max = 1200, average = 250)
public void test() {
Run Code Online (Sandbox Code Playgroud)