简体   繁体   中英

Performance testing a REST API with Visual Studio 2013

I'm new to testing and test automation and I'm trying to test a REST API for performance. For this my primary preference is Visual Studio but I'd like to hear about other options too. I want to capture the json response from a REST call, extract some parameters from the JSON response I got and pass them to the next REST call. It's like automatic parameter detection. I did a search online but could only find something like this https://msdn.microsoft.com/library/dn250793.aspx but no where they really talk about testing REST service with Visual Studio. Any pointers will be of great help. Thank you.!

You can easily talk to a JSON REST Web API service from C# code. You'd need the service running then you can write tests which talk to the API service and give you timings or parse the response and call the next API method, etc.

Here's a simple example

    public async Task<YourResponseDTO> GetResponseDTO()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("localhost/your-web-api/");

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("your-first-endpoint");
            if (!response.IsSuccessStatusCode)
            {
                return null;
            }

            var mediaType = response.Content.Headers.ContentType.MediaType;
            if (mediaType != "application/json")
            {
                return null;
            }

            var responseObject = await response.Content.ReadAsAsync<YourResponseDTO>();

            return responseObject;
        }
    }

You simply write the class YourResponseDTO to match whatever fields come out of JSON and this code will automatically populate the fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM