How to validate a swagger rest API using visual studio

倖福魔咒の 提交于 2021-02-19 06:00:09

问题


I am new to API testing as well as using Swagger Rest API. I am looking for examples on how to validate a POST request Rest API call from swagger. I am trying to figure out how to write a unit test for validating swagger REST API in visual studio 2015 edition in C#.

Please let me know how I can proceed and if you have any examples to get it worked then it would be a real great help for me.

Thanks, Anusha


回答1:


Here are some things to consider when testing API projects in the context of Visual Studio.

Note this advice deviates from the op's question by (1) addresses integration testing in addition to just unit testing and (2) only discusses Visual Studio 2017 (not 2015 as requested).

Testing the API at the endpoints (integration testing and acceptance)

This is the level of testing that Swagger helps with directly.

Dredd can take different spec types -- viz., Open API / Swagger and API Blueprint -- and call example transactions. Support for doing this with API Blueprint seems better than with Swagger, IMO.

There are alot of Javascript tools to help with test generation and mocking, e.g., swagger-test-templates, SwagMock. Mocha, Chai, and Jest are popular tools for this.

Some services that specialize in test management at this level: e.g., SauceLabs, Ready! API, Assertible. Other popular tools are free and integrate with a test management platform: e.g, SwaggerInspector and SoapUI.

Visual Studio 2017 Enterprise has the webtest feature, which is also supported in Visual Studio Team Services Cloud-based Load Testing products. This has nice integration in VSTS so you can trigger webtests on Build. However, this tool is oriented towards performance testing (I don't know how good it is for integration testing). Here is an example of recording and replaying calls with Swagger UI and Visual Studio.

Another, more involved, approach for this (which have been hinted at in the comments) is to write basic unit tests that make API calls manually (instead of some kind of request/response framework). As a shortcut, you could generate the API client from Swagger using Visual Studio or NSwag. Then, in your unit tests, make the calls to your API with the generated client. Alternatively, you can just use .NET libraries to make requests and inspect responses. I suggest looking into integrating Javascript-based testing with Visual Studio, instead of writing these in C# though.

Here are some (less organized) thoughts about integration testing...

  • Specifying the tests (req/resp) yourself - like in an API Blueprint spec
  • Generating mocks and templates for tests (and then finish specifying them) - swagger-test-templates or SwagMock)
  • Record & replay - (e.g., Visual Studio webtests and Jest)
  • Service virtualization - Mountebank, Wiremock, MockServer

Unit-testing models, controllers, services, etc.

These are different than the previous example of testing API endpoints, and are thought of as a unit testing activities. As seen in this example of testing controllers in ASP.NET Core, the tricky part can be refactoring your code to support dependency injection, in case you want to swap out databases or services at test time.



来源:https://stackoverflow.com/questions/38330632/how-to-validate-a-swagger-rest-api-using-visual-studio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!