Selenium webdriver 如何在每个测试用例之间有一定的延迟来执行JUnitTestSuite?
我有两个JUnit4测试类,分别是MyTest1和MyTest2,每个类都有两个测试方法。 实际上,这些是SeleniumJUnit测试用例 在MyTest1.someMethodInsertingDate()中,我将向数据库中插入一些数据,处理这些数据需要一些时间 在MyTest2.validateProcessedData()中,我需要验证插入到第一个测试方法中的已处理数据是否有效 我知道测试方法/案例之间的耦合不是一个好的实践。但我正在编写SeleniumTests来自动化用户在UI上的操作,所以我必须这样做 我正在使用MyTestSuite和@RunWith&@SuiteClasses执行这两个测试用例Selenium webdriver 如何在每个测试用例之间有一定的延迟来执行JUnitTestSuite?,selenium-webdriver,junit4,Selenium Webdriver,Junit4,我有两个JUnit4测试类,分别是MyTest1和MyTest2,每个类都有两个测试方法。 实际上,这些是SeleniumJUnit测试用例 在MyTest1.someMethodInsertingDate()中,我将向数据库中插入一些数据,处理这些数据需要一些时间 在MyTest2.validateProcessedData()中,我需要验证插入到第一个测试方法中的已处理数据是否有效 我知道测试方法/案例之间的耦合不是一个好的实践。但我正在编写SeleniumTests来自动化用户在UI上的操
现在,我如何告诉JUnit延迟执行MyTest2测试用例,比如说1分钟。您可以通过在每个测试方法的末尾添加一些内容来休眠脚本。
在PHP中:
睡眠(60) 我认为您没有得到这个问题的答案(至少不是您想要的答案),因为在测试套件中依赖这些机制通常被认为是不好的做法。如果您有这些类型的相互依赖,那么您的测试将变得非常难以维护,并且可能(很可能)会导致您在以后调试它们时遇到困难
您可能会将第一个测试抽象出来,第二个测试可能会扩展它。然后,第二个测试将在其设置中使用公共功能(可能使用不同的数据)。这应该允许您同时运行测试,因为每个测试都将是一个原子实体。在数据库代码之后的第一个测试用例中,您可以在Java中引发一些等待,如下所示: long-end3=System.currentTimeMillis()+6000
while(System.currentTimeMillis(),因为我们希望大家都同意这通常是不好的做法,但也有例外。
显而易见,我们谈论的不是单元测试,而是集成测试(JUnit可能不是适合这项工作的工具,但我还没有找到更好的工具)
我也在做Selenium测试。我对第三方测试服务器进行集成测试,如果我在没有sleep()的情况下运行测试,这种行为是随机的
这是一种可能的解决方案,请注意,这仅通过运行一次测试:
public class SleepySuite extends Suite {
private final Logger log = LoggerFactory.getLogger(SleepySuite.class);
private final Integer defaultSleepSec = 0;
private final Integer sleepSec;
public SleepySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
sleepSec = initSleep(klass);
}
private Integer initSleep(Class<?> klass) {
SleepSec ts = klass.getAnnotation(SleepSec.class);
Integer sleep = defaultSleepSec;
if (ts != null) {
sleep = ts.value();
log.debug("Configured with sleep time: {}s", sleep);
}
return sleep;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface SleepSec {
public int value();
}
/**
* @see org.junit.runners.Suite#runChild(org.junit.runner.Runner, org.junit.runner.notification.RunNotifier)
*/
@Override
protected void runChild(Runner runner, RunNotifier notifier) {
super.runChild(runner, notifier);
//Simply wrapped Thread.sleep(long)
TestUtils.sleep(sleepSec);
}
}
您可以使用简单的“@Before”注释。
它应该在每次启动测试时延迟执行线程
例如:
@Before
public void initialise() throws InterruptedException {
Thread.sleep(2000);
}
为子孙后代分享
JUnit测试中使用Thread#sleep
总是被静态代码分析工具(如sonarqube
)所阻止。与其使用上述方法,不如使用一些库(如Waitibility
),尽管这不是一个理想的解决方案,但这是一种比使用Thread#sleep
好得多的方法
所需代码更改:
testImplementation('org.awaitility:awaitility:4.0.3')
将下面的代码片段放在需要放置一些延迟的位置。在下面的示例中,我将延迟持续时间设置为1秒。从概念上讲,这将执行与Thread.sleep(1000)
你是对的。耦合b/w测试用例是一种不好的做法。因此,最后我将依赖测试方法移动到私有方法中,从测试用例调用它们,将sleep()放在它们之间。
@RunWith(SleepySuite.class)
@Suite.SuiteClasses({
Some.class,
SomeOther.class
})
@SleepySuite.TimeoutSec(30)
public class YourSuite{
}
@Before
public void initialise() throws InterruptedException {
Thread.sleep(2000);
}
testImplementation('org.awaitility:awaitility:4.0.3')
Awaitility.await().pollDelay(Durations.ONE_SECOND).until(() -> true);