如何使用 Swagger 在测试中验证 API? [英] How to validate API in tests with Swagger?

查看:56
本文介绍了如何使用 Swagger 在测试中验证 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出让我的 API 文档成为事实来源的最佳方式,并使用它通过集成测试或类似测试理想地验证实际的 Java REST 代码.我们正在使用契约优先或消费者契约类型的方法,因此我们不希望文档必须从带注释的代码生成并在每次开发人员进行更改时更新.

I'm trying to figure out the best way to have my API documentation be the source of truth and use it to validate the actual Java REST code ideally through integration testing or something of that sort. We're using the contract first or consumer contract type of approach, so we don't want the documentation to be generated from annotated code necessarily and updating every time a developer makes a change.

一个想法是使用 Swagger,但我不确定如何最好地使用它来验证 API.理想情况下,最好在构建或集成测试过程中进行验证,以查看真实响应(以及可能的请求)是否符合预期.我知道 Swagger 有很多用途和工具,我只是想了解一下它.或者是否有更好的替代方法来处理 Java 代码.

One thought has been to use Swagger, but I'm not sure how best to make it be used for validating the API. Ideally, it'd be good to have the validation occur in the build or integration testing process to see if the real response (and request if possible) match what's expected. I know there are a lot of uses and tools for Swagger and just trying to wrap my head around it. Or if there is a better alternative to work with Java code.

推荐答案

最近,我们(swagger-codegen 社区)开始向 API 客户端(C#、PHP、Ruby)添加自动测试用例生成.我们还没有将它添加到 Java 中.以下是 Swagger-Codegen 为 C# 生成的一些示例测试用例:

Recently, we (swagger-codegen community) start adding automatic test case generation to API clients (C#, PHP, Ruby). We've not added that to Java yet. Here are some example test cases generated by Swagger-Codegen for C#:

https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test

这仍然是非常初步的,我们希望听到您的反馈,看看这是否是您正在寻找的内容.

It's still very preliminary and we would like to hear feedback from you to see if that's what you're looking for.

这篇关于如何使用 Swagger 在测试中验证 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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