集成测试:模拟外部API与使用外部API沙盒 [英] Integration testing: Mock external API vs. use external API sandbox

查看:281
本文介绍了集成测试:模拟外部API与使用外部API沙盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要使用外部合作伙伴的API。该API是一个良好的状态,我们得到了进入沙盒环境,我们可以使用自动测试。

We're required to use the API of an external partner. The API is in a good shape and we got access to a sandbox environment we can use for automatic testing.

我们已经在使用单元测试测试外部API的每一个电话,但不确定关于集成测试的最佳做法,当涉及到复杂的操作对外部合作伙伴的身边。

We already test every single call of the external API using unit tests but are unsure regarding best practices for integration tests when it comes to complex operations on the side of the external partner.

例如:我们的服务的每个用户也得到了用户对象在我们的外部合作伙伴。当该用户对象上执行外部API调用X,我们预计反对Y以显示该用户(我们可以选择使用不同的电话查询)收集ž内。

Example: Every user of our service also got a user object at our external partner. When performing external API call X on this user object, we expect object Y to appear inside collection Z of this user (which we have to query using a different call).

什么是测试的情况下也是这样吗?

What are best practices for testing cases like this?


  • 嘲笑外部API 尽可能并依靠单元测试做好自己的工作?优点:测试运行从互联网连接的快速和独立。缺点:误区是我们嘲笑可能导致误报

  • Mock the external API as much as possible and rely on the unit tests to do their job? Advantages: Tests run fast and independent from an internet connection. Disadvantages: Mistakes is in our mocks could lead to false positives.

集成外部API沙箱并针对它运行的每个集成测试。优点:贴近现实生活API交互。缺点:测试只能使用一个开放的互联网连接,并且需要更多的时间

Integrate the external API sandbox and run every integration test against it. Advantages: Close to real life API interactions. Disadvantages: Tests can only be run with an open internet connection and take more time.

嘲笑和沙箱数据的使用混合后,设置一个布尔值需要在内部(=轻慢)和外部(=沙箱)环境之间进行切换。优点:可靠的测试。缺点:可能是一个痛苦的建立

Use a hybrid of mocked and sandbox data, set a boolean to switch between the internal (=mocked) and external (=sandbox) environment when required. Advantages: Reliable tests. Disadvantages: Could be a pain to set up.

其他最佳做法?

谢谢!

相关报道:<一href=\"http://stackoverflow.com/questions/7564038/how-are-integration-tests-written-for-interacting-with-external-api\">How对于与外部交互的API编写集成测试?的但是,回答你不知道。你必须真正相信实际的API实际工作。是不是在我们看来就足够了。

Related: How are integration tests written for interacting with external API? However, the answer "You don't. You have to actually trust that the actual API actually works." is not sufficient in our opinion.

我们担心只是针对我们的假设外部API应该如何工作(即使它们是基于单元测试)的集成测试 - 而不是对实际的API - 将留给我们的误报。我们会需要的是一个测试验证我们的假设(嘲笑)实际上是正确的 - 不仅在单元测试的范围内,而且在复杂的操作与几个步骤上下文

We fear that integration testing only against our assumptions how the external API should work (even if they are based on unit tests) – and not against the actual API – will leave us with false positives. What we'd need is a test that verifies that our assumptions (mocks) are actually correct – not only in the context of unit tests but also in the context of complex operations with several steps.

验证可能是一个很好的例子:如果我们搞砸了一体化code和发送畸形数据或数据不使我们送它,因为我们错过了一步上下文任何意义?我们模拟的API,它不验证(或仅在非常有限的范围内)仍将返回有效的数据,而不是通过我们收到来自真正的API错误的。

Validation might be a good example: What if we mess up the integration code and send malformed data or data that does not make any sense in the context we send it in because we missed a step? Our mock API, which does not validate (or only in very limited range) would still return valid data instead of passing the error we would receive from the real API.

推荐答案

我认为应该有验证的2级需要我们,当我们与外部API接口做的:

I believe there should be 2 level of verifications we need to do when we interface with an external API:


  • API验证:验证API的作品根据其规格和/或我们的理解

  • 应用功能验证:验证我们的业务逻辑根据预期的API传递API验证
  • 工作

在我们的例子中,我们使用的模拟API 连同真实和模拟API验证

In our case, we use a mock API together with real and mock API verification.


  • 模拟API允许我们隔离任何运行时错误/例外唯一的应用程序的功能,所以我们不要责怪任何外部党问题

  • 同样的API验证对阵双方真实和模拟API的执行,以确保真正的一件作品,我们期望的方式,以及模拟应该正确地模仿真钞

如果一路走来,外部API的变化,API验证可能变红,引发假API的变化。在模拟API的变化可能使应用验证变红,引发应用程序实现更改。这样,您再也不会错过外部API和应用程序执行(理想情况下)。

If along the way, external API changes, API verification may turn red, triggering changes in mock API. Changes in mock API may make app verification turn red, triggering changes in app implementation. This way you never miss any gap between external API and app implementation (ideally).

有一个模拟API + API验证的另一个额外的好处是,开发人员可以使用它作为的API是如何工作的一个文档/规格。

Another extra benefit of having a mock API + API verification is that your developers can use it as a documentation/specification of how the API is supposed to work.

这篇关于集成测试:模拟外部API与使用外部API沙盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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