[英]junit detect if a test is the only one run
I have a test like this: 我有这样的测试:
@Test public void testInfinite() {
while (true) {runSomeOtherTest();waitForSomeSignal();}
We have a spring context which takes a while to initialize - and with the above test and hotswapping - it means I can debug changes in seconds rather than waiting for a restart. 我们有一个spring上下文,需要花一些时间进行初始化-并通过上述测试和热插拔-这意味着我可以在几秒钟内调试更改,而不必等待重新启动。
However, obviously checking this into the CI system is a bit of a problem, and of course something I've accidentally done several times. 但是,显然将其检入CI系统是一个问题,当然,我无意间做了几次。 I'm wondering if it's possible (without changing the testrunner, because we are already using a custom one, that's hard to modify) to determine if it's the only test running. 我想知道是否有可能(无需更改testrunner,因为我们已经在使用定制的,很难修改)来确定它是否是唯一运行的测试。 eg I want to be able to say 我想说
@Test public void testInfinite() {
if (!testIsTheOnlyTestWhichWillBeRun()) return; ...
So, bottom line - what I'm looking for is that if I right click on exactly that test and say run, in the IDE - it will do that - in all other cases - eg I right click and run the whole class - or I do it from gradle, or from CI - the test does nothing and just returns immediately. 因此,最重要的是-我要寻找的是,如果我在IDE中右键单击该测试并说运行,它将在其他情况下执行该操作-例如,我单击鼠标右键并运行整个类-或我是通过gradle或CI来完成的-测试不执行任何操作,只是立即返回。
You can evaluate a System property: 您可以评估系统属性:
@Test public void testInfinite() {
if (!"true".equals(System.getProperty("junit.runloop"))) {
return;
}
}
In your IDE/local run, you can then run the test with -Djunit.runloop=true. 在您的IDE /本地运行中,然后可以使用-Djunit.runloop = true运行测试。
To not polute the test cases themselves, you could write a JUnit rule that does this: 为了不污染测试用例本身,您可以编写一个执行以下操作的JUnit规则:
public class TestInfinite implements TestRule {
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
public void evaluate() throws Throwable {
do {
base.evaluate();
} while ("true".equals(System.getProperty("junit.runloop")));
}
};
}
}
So in your test, you can attach them: 因此,在测试中,您可以将它们附加:
public class YourTest {
@Rule public TestInfinite = new TestInfinite();
@Test public void testInfinite() {
// this will be run once if property is not set,
// in an endless loop if it is
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.