REST API 的自动化测试 [英] Automated testing for REST Api

查看:44
本文介绍了REST API 的自动化测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 REST API 编写一个自动化测试套件.当我们完成新服务时,我们想检查以确保所有以前创建的服务都按预期工作.关于用于实现此目的的最佳工具的任何建议?我知道有像 Apigee 这样的工具可以让您一次测试一项服务,但我们希望找到一种方法,只需点击一下按钮即可测试所有服务.

I would like to write an automated testing suite for a REST API. As we complete new services, we'd like to check to make sure all the previously created services are working as expected. Any suggestions on the best tools to use to accomplish this? I know tools like Apigee exist that allow you to test 1 service at a time, but we'd like for a way to test all services with the click of a button.

推荐答案

在我的工作中,我们最近整理了几个用 Java 编写的测试套件来测试我们构建的一些 RESTful API.我们的服务可以调用它们依赖的其他 RESTful API.我们把它分成两套.

At my work we have recently put together a couple of test suites written in Java to test some RESTful APIs we built. Our Services could invoke other RESTful APIs they depend on. We split it into two suites.

  • 套件 1 - 单独测试每个服务
    • 使用 restito 模拟 API 所依赖的任何对等服务.其他替代方案包括 rest-driverwiremockbetamax.
    • 测试我们正在测试的服务,所有模拟都在单个 JVM 中运行
    • 在 Jetty 中启动服务
    • Suite 1 - Testing each service in isolation
      • Mock any peer services the API depends on using restito. Other alternatives include rest-driver, wiremock and betamax.
      • Tests the service we are testing and the mocks all run in a single JVM
      • Launches the service in Jetty

      我绝对会推荐这样做.它对我们来说非常有效.主要优点是:

      I would definitely recommend doing this. It has worked really well for us. The main advantages are:

      • 对等服务是模拟的,因此您无需执行任何复杂的数据设置.在每次测试之前,您只需使用 restito 来定义您希望对等服务的行为方式,就像您使用 Mockito 在单元测试中使用类一样.
      • 您可以询问模拟的对等服务是否被调用.您无法使用真正的对等服务轻松地进行这些断言.
      • 该套件速度非常快,因为模拟服务可以提供预制的内存响应.因此,我们可以获得良好的覆盖范围,而无需运行套件.
      • 该套件可靠且可重复,因为它隔离在自己的 JVM 中,因此无需担心其他套件/人员在套件运行的同时使用共享环境并导致测试失败.
      • 套件 2 - 完整的端到端
        • Suite 在部署在多台机器上的完整环境中运行
        • 部署在 Tomcat 环境中的 API
        • 对等服务是真正的实时"完整部署

        这个套件要求我们在对等服务中设置数据,这意味着测试通常需要更多时间来编写.我们尽可能使用 REST 客户端在对等服务中进行数据设置.

        This suite requires us to do data set up in peer services which means tests generally take more time to write. As much as possible we use REST clients to do data set up in peer services.

        该套件中的测试通常需要更长的时间来编写,因此我们将大部分覆盖范围放在套件 1 中.话虽如此,该套件仍然具有明显的价值,因为我们在套件 1 中的模拟可能表现得不太像真正的服务.

        Tests in this suite usually take longer to write, so we put most of our coverage in Suite 1. That being said there is still clear value in this suite as our mocks in Suite 1 may not be behaving quite like the real services.

        这篇关于REST API 的自动化测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆