我必须使用什么样的测试

What kind of Testing I have to use?

本文关键字:什么样 测试      更新时间:2023-10-16

我写了一些代码,可以随时侦听Web服务。根据结果,我将向 Web 服务发送一些请求,并根据响应发送进一步的请求。如果一切正常,我将开始监听Web服务,直到发生一些中断。

遵循伪代码(种类)

class listner
{
string sendReq(Request)
{
   curl_easy_perform();    
  return responseString;
}
connectWebServive()
{
   curl = curl_easy_init();// curl member variable CURL *curl;
    while(true)
    {
       String res = listener(Request1)
       if(res == “some thing”)
       {
           String res = listener(Request2)
       }
       else
       {
           String res = listener(Request3)    
       }
       while(true)
       {
          String res = listener(request4)
          if(somethingWrong)
          {
             break;
          }                  
       }
    }
}
}

如何测试此代码?我应该使用分支覆盖率测试还是代码覆盖率测试?

谢谢

分支和行/语句覆盖率是不同的指标,可帮助了解正在测试的内容。在任何一种情况下,这都将取决于作为 API 响应的数据或事件,因此对覆盖率指标的任何解释都意味着,如果您希望执行准确的分析,则需要为所有分支创建测试。

为了提高可读性,我建议不要使用嵌套的while循环。应该有一个等待响应的侦听器循环。该循环的内部结构包含处理逻辑和 if-then-else 树。应该有一个默认的起始请求(入口点),然后逻辑应该检查输入以决定下一个请求是什么。

current_request = construct_original_request()
while(true) 
{
  response = do_request(current_request) // make a blocking call in thread
  if(response like 'condition A') 
  {
    // do A logic, if any
    current_request = construct_B_request()... // then set up next request
  } else if(response like 'condition B') 
  {
    // do B logic if any
    current_request = construct_C_request()... // then set up next request
  } else 
  {
    current_request = construct_original_request() // back to square one
  }
  wait(1) // let the thread/processor sleep for a non-zero amount of time
}