编写单元测试一个REST API FUL [英] Writing unit tests for a REST-ful API

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

问题描述

我打算写单元测试的REST-FUL API,我想知道我应该采取的办法。

I'm planning on writing unit tests for a REST-ful API and I'm wondering about the approach I should take.

这是我最关心的的方面是涉及到数据库的状态。我的理解是,测试目标的环境中,或在初始状态下,应该对每个试验中,这意味着该数据库也应该是每个测试相同相同。我如何做到这一点时,我有一个沉重的数据库?此外,如何处理在数据库模式中可能的变化?

The aspect that concerns me the most is related to the database state. My understanding is that the environment, or the initial state, of a test target should be the same for each test, which means that the database should also be the same for each test. How do I achieve that when I have a heavy database? Also, how do I handle eventual changes in the database schema?

推荐答案

问题是你要测试的。你认为将在(这是接收HTTP请求,即层),最上面的API层打破?

Question is what do you want to test. What do you think will break at the topmost api-layer (i.e. layer which is receiving HTTP request)?

通常编写单元测试REST风格的API,听起来有点像一个矛盾;)通过定义单元测试是为使用入口点的HTTP数据库小得多。这听起来更是你的问题是基于如何写大测试(或验收,端至高端测试)。

Generally writing a unit-test restful-api sounds a bit like an oxymoron ;) By definition unit-tests are much smaller as using entry-point HTTP to database. It sounds more that your question is based on how to write large-tests (or acceptance, end-to-end test).

要注意的是实施这种大测试(终端到终端的测试)包括高力:

Beware that implementing such large-tests (end-to-end test) involves high effort:


  • 测试往往要慢得多

  • 测试维护成本,因为测试是更难COM prehend(因为这一切都依赖条件+测试数据设置)

  • 它们更容易引起假阳性测试(测试表明'红',虽然它应该是'绿色')。原因又是更多的依赖是参与你的测试,脆弱性的可能性更大。

在测试粒度我的经验多样性是王,所以我混合/结合的方法:

In my experience diversity of test-granularity is king, therefore I mix/combine approaches:


  • 单元测试对API的内部(例如一些更复杂的映射要求,认证,棘手的验证规则,复杂的if / else逻辑,...)

  • href=\"http://en.wikipedia.org/wiki/Smoke_testing\">更粗粒级,HTTP客户端交谈API烟雾测试,即测试集成。这些测试会告诉我:服务器无法启动,主要的API使用情况下工作。作为工具,我建议皂UI

  • 有关数据库状态:我经常先从最基础的数据(例如现有的API的用户或predefined不变的测试数据)。试验数据对于每个测试应予以隔离。如果我的测试包括更为复杂的流程(如一个整体的用例是在多个HTTP调用小号$ P $垫),测试数据可以依赖于测试阶段(即调用-2可能依赖于服务器的状态,这有通过调用-1)
  • 改变
  • unit-testing for api-internals (e.g. several more complex mapping requirements, authentication, tricky validation rules, complicated if/else logic, ...)
  • doing smoke-tests on more coarse grained level, HTTP client is talking to api, i.e. testing integration. These tests would show me: server could start-up, main api use-cases works. As tool I recommend soap-ui.
  • regarding database-state: I often start with most basic data (e.g. existing api-users or predefined immutable test-data). Test-data for each test should be isolated. If my test includes more complicated flow (e.g. a whole use-case is spread over multiple HTTP calls), test-data is allowed to depend on test-steps (i.e. call-2 might depend on server-state which got changed by call-1)

也许你可以给你想要测试特定用例更多的投入,这样可以给更多的帮助?

Maybe you can give more input on a specific use-case you want to test, so can give more help?

这篇关于编写单元测试一个REST API FUL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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