REST API与代码覆盖的集成测试 [英] Integration Test of REST APIs with Code Coverage

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

问题描述

我们已经构建了一个暴露大量业务服务的REST API - 业务服务可以调用其他平台/实用程序服务来执行数据库读写,执行服务授权等。

We have built a REST API that exposes bunch of business services - a business service may invoke other platform/utility services to perform database read and writes, to perform service authorization etc.

我们已将这些服务部署为Tomcat中的WAR文件。

We have deployed these services as WAR files in Tomcat.

我们希望使用集成测试套件测试整个设置,我们也希望将其视为回归测试套件。

We want to test this whole setup using a integration test suite which we would like to also treat as regression test suite.

在这个和任何可以加速套件开发的工具上执行集成测试的好方法是什么?以下是我们认为需要解决的一些要求:

What would be a good approach to perform integration testing on this and any tools that can speed up the development of suite? Here are few requirements we think we need to address:


  1. 能够定义执行业务场景的集成测试用例。

  2. 在套件运行之前设置带有测试数据的数据库。

  3. 调用在远程服务器(Tomcat)上运行的REST API

  4. 验证数据库后测试执行以验证预期输出

  5. 拥有REST API的代码覆盖率报告,以便我们知道应该对套件所涵盖的方案有多大信心。

  1. Ability to define integration test cases which exercise business scenarios.
  2. Set up the DB with test data before suite is run.
  3. Invoke the REST API that is running on a remote server (Tomcat)
  4. Validate the DB post test execution for verifying expected output
  5. Have code coverage report of REST API so that we know how confident we should be in the scenarios covered by the suite.


推荐答案

在我的工作中,我们最近组建了几个测试套件来测试一些RESTful我们建立的API。与您的服务一样,我们的服务可以调用他们依赖的其他RESTful API。我们将它分成两个套房。

At my work we have recently put together a couple of test suites to test some RESTful APIs we built. Like your services, ours can invoke other RESTful APIs they depend on. We split it into two suites.


  • 套房1 - 测试每个服务隔离

    • Suite 1 - Testing each service in isolation
      • Mocks any peer services the API depends on using restito. Other alternatives include rest-driver, wiremock, pre-canned and betamax.
      • The tests, the service we are testing and the mocks all run in a single JVM
      • Launches the service we are testing in Jetty

      我肯定会建议这样做。它对我们来说非常有效。主要优点是:

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


      • 对等服务被模拟,因此您无需执行任何复杂的数据设置。在每次测试之前,您只需使用restito来定义您希望对等服务的行为方式,就像使用Mockito进行单元测试时的类一样。

      • 套件非常快,因为模拟服务提供预先存储的内存中响应。所以我们可以获得良好的覆盖率,而不需要套件运行年龄。

      • 该套件可靠且可重复,因为它独立于自己的JVM中,因此无需担心其他套件/人员在套件运行的同时与共享环境混淆导致测试失败。


      • 套件2 - 完全结束


        • 套件针对跨多台计算机部署的完整环境运行

        • API部署在环境中的Tomcat上

        • 对等服务是真实的'实时'完整部署

        • Suite 2 - Full End to End
          • Suite runs against a full environment deployed across several machines
          • API deployed on Tomcat in environment
          • Peer services are real 'as live' full deployments

          此套件要求我们在对等服务中进行数据设置,这意味着测试通常需要更多时间来编写。我们尽可能使用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.

          关于您的积分,以下是我们的工作:

          With regards to your points, here is what we do:


          • 能够定义执行业务场景的集成测试用例。


            • 我们使用 cucumber-jvm 来定义上述两个套件的业务场景。这些场景是英文纯文本文件,业务用户可以理解并驱动测试。

            • Ability to define integration test cases which exercise business scenarios.
              • We use cucumber-jvm to define business scenarios for both of the above suites. These scenarios are English plain text files that business users can understand and also drive the tests.

              • 我们不对集成套件执行此操作,但过去我使用过 unitils 并且运行良好。

              • We don't do this for our integration suites, but in the past I have used unitils with dbunit for unit tests and it worked pretty well.

              • 我们使用放心,这是一个专门用于测试REST API的优秀HTTP客户端。

              • We use rest-assured, which is a great HTTP client geared specifically for testing REST APIs.

              • 我不能在这里提供任何建议,因为我们不使用任何库来帮助简化这一过程,我们只是手动完成。如果您发现任何问题,请告诉我。


              • 我们不测量集成测试的代码覆盖率,仅用于我们的单元测试,所以我再次这里不能提供任何建议。

              让你的眼睛盯着我们的< a href =http://techblog.net-a-porter.com/ =noreferrer> techblog 因为未来可能会有更多详细信息。

              Keep your eyes peeled on our techblog as there may be more details on their in the future.

              这篇关于REST API与代码覆盖的集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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