REST assured: 在运行所有测试之前等待条件

huangapple go评论83阅读模式
英文:

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);
}

huangapple
  • 本文由 发表于 2020年7月31日 00:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定