英文:
REST assured: Wait for a condition before running all tests
问题
我有一个服务,在加载时需要一些时间(几分钟)直到所有信息都被存入数据库。
如果我过早地运行测试,由于数据库中缺少项目,测试将失败。
是否有一些REST Assured的功能来处理这个问题?还是我需要拥有自己的机制来处理这个?
英文:
I have a service that when it's being loaded, it takes a while (few minutes) until all the information is getting into the database.
If I run the tests prematurely, they will fail for that reason (missing items in DB).
Is there some REST assured feature to deal with it? Or I need to have my own mechanism to do that?
答案1
得分: 2
Rest-Assured 无法确定服务是否已启动,除非服务本身提供了用于检查其状态的端点。此外,Rest-Assured 不管理测试生命周期。
但是,JUnit 可以。您可以通过在 @BeforeAll
和 @BeforeEach
方法中编写条件逻辑来实现所需的操作,如下所示:
boolean serviceStarted = false;
@BeforeAll
public void waitForServiceStart(){
try{
// 在此等待条件
serviceStarted = true;
}catch (Throwable e){
// 在此处理异常
}
}
@BeforeEach
public void makeSureEverythingIsReady(){
Assumptions.assumeTrue(serviceStarted);
}
英文:
Rest-Assured cannot decide if the service has started unless the service itself provides the endpoint to check its status. Moreover Rest-Assured does not manage test life-cycle.
But JUnit does. You can achieve what you need by coding the condition logic in @BeforeAll
and @BeforeEach
methods like this:
boolean serviceStarted = false;
@BeforeAll
public void waitForServiceStart(){
try{
// Wait for conditions here
serviceStarted = true;
}catch (Throwable e){
// Process exception here
}
}
@BeforeEach
public void makeSureEverythingIsReady(){
Assumptions.assumeTrue(serviceStarted);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论