App automation test interface documentation and online testing

In this article, we mainly introduce how to make API help documents, introduce the functions of each API, input parameters, output parameters, and online test API functions to API callers (this is also convenient for our own development and debugging)

Let's take a look at the final help documentation of our API and the final effect of online testing:

Summary diagram

GET API

Add product API:

Delete product API

Next, let's implement the above functions by hand.

Add annotation information to all APIs

code show as below

[RoutePrefix("api/products")]
    public class ProductController : ApiController
    {
        /// <summary>
        /// 产品分页数据获取
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("product/getList")]
        public Page<Product> GetProductList()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取单个产品
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        [HttpGet, Route("product/get")]
        public Product GetProduct(Guid productId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost, Route("product/add")]
        public Guid AddProduct(Product product)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="product"></param>
        [HttpPost, Route("product/update")]
        public void UpdateProduct(Guid productId, Product product)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="productId"></param>
        [HttpDelete, Route("product/delete")]
        public void DeleteProduct(Guid productId)
        {
            throw new NotImplementedException();
        }
    }

The api help information shown in the figure above is all extracted from our annotation information, so the API annotation information here is essential.

Add Swagger.Net component (custom modified version, the official has not been updated for many years, and can only be updated by itself)

Add the Swagger.Net component to the project. Since this has been updated a lot in the official version, you can copy it directly from the project code in the process of practice (when necessary, it can be made into a Nuget component for everyone to use after it is released)

Add Swagger.NET steps:

1. Introduce Swagger.Net Project into the project.

2. Add SwaggerNet.cs under App_Start of the Web API project

code show as below

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SwaggerNet), "PreStart")]
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(SwaggerNet), "PostStart")]
namespace Niusys.WebAPI.App_Start
{
    public static class SwaggerNet
    {
        public static void PreStart()
        {
            RouteTable.Routes.MapHttpRoute(
                name: "SwaggerApi",
                routeTemplate: "api/docs/{controller}/{action}",
                defaults: new { swagger = true }
            );
        }

        public static void PostStart()
        {
            var config = GlobalConfiguration.Configuration;
            config.Filters.Add(new SwaggerActionFilter());
        }
    }
}

It is mainly to register the request routing of the api document and intercept the request of the document.

3. Copy the SwaggerUI folder in the WebAPI project, here is the page processing file of the help document.

4. WebAPI project enables XML document generation

At this time, you can start the project, enter the swaggerui ( http://localhost:14527/swaggerui/ ) directory in the URL , and you can access our API help documentation system and online testing.

Summarize:

The principle of the help document here is realized through our XML comments in the code. The principle is also that when the api/doc is requested, at this time, the xml help document corresponding to the controller/action is fetched and displayed later.

Its test is done entirely with jQuery Ajax, which is running wild inside, and is highly integrated with the interface, which fully meets the needs of our project.

Advanced Python interface automation testing essential tutorial (2023 the most detailed in the whole network)

Guess you like

Origin blog.csdn.net/dad22211/article/details/131926554