使用junitperf运行junit4测试
可以将Junitperf与junit4一起使用吗? 我有一个SimpletJUnit4测试类,其中包含几个测试,我想对该类的单个测试进行TimedTest。我该怎么做 更清楚地说,我的Junit4类类似于:使用junitperf运行junit4测试,junit,junitperf,contiperf,Junit,Junitperf,Contiperf,可以将Junitperf与junit4一起使用吗? 我有一个SimpletJUnit4测试类,其中包含几个测试,我想对该类的单个测试进行TimedTest。我该怎么做 更清楚地说,我的Junit4类类似于: public class TestCitta { @Test public void test1 {} @Test public void test2 {} } public class TestCittaPerformance { p
public class TestCitta {
@Test
public void test1 {}
@Test
public void test2 {}
}
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());
}
}
对于junit3,我应该编写如下内容:
public class TestCitta {
@Test
public void test1 {}
@Test
public void test2 {}
}
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());
}
}
对于Junit4?我也遇到了同样的问题,但尝试在不同的构建环境中运行它并不幸运。因此,我使用JUnit4之后提供的@Rule特性,使用注释注入性能测试调用和需求检查。结果它变成了一个小型库,在这个项目中取代了JUnitPerf,我以这个名字发布了它。如果您对这种方法感兴趣,可以在。上找到它,或者您可以使用注释:
@Test(timeout=1000)
如果您已经有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>
实际测试是这样的
public class PersonDAOTest {
@Rule
public ContiPerfRule i = new ContiPerfRule();
@Test
@PerfTest(invocations = 1, threads = 1)
@Required(max = 1200, average = 250)
public void test() {
请把答案标为正确答案。谢谢我使用那个图书馆已经有一段时间了。太棒了。恭喜