簡體   English   中英

使用Visual Studio 2013對REST API進行性能測試

[英]Performance testing a REST API with Visual Studio 2013

我是測試和測試自動化的新手,我正在嘗試測試REST API的性能。 為此,我的主要偏好是Visual Studio,但我也想聽聽其他選項。 我想從REST調用中捕獲json響應,從我獲得的JSON響應中提取一些參數,並將它們傳遞給下一個REST調用。 這就像自動參數檢測。 我在網上搜索過,但只能找到類似這樣的內容https://msdn.microsoft.com/library/dn250793.aspx,但沒有他們真正談論用Visual Studio測試REST服務的地方。 任何指針都會有很大的幫助。 謝謝。!

您可以輕松地從C#代碼與JSON REST Web API服務進行通信。 您需要運行該服務,然后您可以編寫與API服務對話的測試,並為您提供計時或解析響應並調用下一個API方法等。

這是一個簡單的例子

    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;
        }
    }

您只需編寫類YourResponseDTO以匹配JSON中出現的任何字段,此代碼將自動填充字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM