Flurl & HttpTest:单元测试在全部运行时失败,但在单独运行时通过



根据项目的github问题, HttpTest不是线程安全。在解决问题之前,使用HttpTest的测试不能并行运行。

我有一对非常奇怪的测试,利用Flurl和Xunit,当所有在VS Test Explorer中运行时,都会失败,但是如果单独运行,则会通过。我一生都看不到这两个彼此相关的地方,但它们都可以。

我将它们从项目中提取到一个新项目中,问题仍然存在。我将它们捆绑到了一个有兴趣将其加载到VS的任何人的7Z中,但是完整的代码遵循。

project.commons

getapi1:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
    public class GetApi1
    {
        public async Task<string> ExecuteAsync(string token)
        {
            string apikeyKeyname = "token";
            dynamic response = await "http://www.api.com"
                .SetQueryParams(new { token = token })
                .GetJsonAsync();
            string receivedApiKey = ((IDictionary<string, object>)response)[apikeyKeyname].ToString();
            return receivedApiKey;
        }
    }
}

getapi2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
    public class GetApi2
    {
        public async Task<IList<string>> ExecuteAsync()
        {
            var responses = await "http://www.api.com"
                .GetJsonAsync<List<string>>();
            var result = new List<string>();
            foreach (var response in responses)
            {
                result.Add("refined stuff");
            }
            return result;
        }
    }
}

project.tests

UnitTest1:

using Project.Commons;
namespace Project.Tests
{
    public class UnitTest1
    {
        private ITestOutputHelper output;
        public UnitTest1(ITestOutputHelper output)
        {
            this.output = output;
        }
        [Fact]
        public async Task ShouldBeAbleToGetApiKeyFromToken()
        {
            // Arrange
            using (var httpTest = new HttpTest())
            {
                var jsonResponse = new { token = "abcdef" };
                string expectedApiKey = "abcdef";
                httpTest.RespondWithJson(jsonResponse);
                var api = new GetApi1();
                // Act
                var receivedApiKey = await api.ExecuteAsync("mockToken");
                output.WriteLine("Received apikey = " + receivedApiKey);
                // Assert
                Assert.Equal(expectedApiKey, receivedApiKey);
            }
        }
    }
}

UnitTest2

using Flurl.Http.Testing;
using Project.Commons;
using Xunit;
using Xunit.Abstractions;
namespace Project.Tests
{
    public class UnitTest2
    {

        #region Mock API JSON Response
        private IList<string> mockResponse = new List<string>()
        {
            "raw stuff", "raw stuff", "raw stuff"
        };
        #endregion
        #region Expected Result
        private IList<string> expectedResult = new List<string>()
        {
            "refined stuff", "refined stuff", "refined stuff"
        };
        #endregion
        [Fact]
        public async Task CanGetProjectsByWeek()
        {
            // Arrange
            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWithJson(mockResponse);
                // Act
                var api = new GetApi2();
                var actualResult = await api.ExecuteAsync();
                // Assert
                Assert.Equal(expectedResult,actualResult);
            }
        }
    }
}

评论是正确的 - 缺乏线程安全是最httptpt的限制。它已记录并正在调查中。今天的平行测试比几年前创建的时要多得多,因此,尽管修复并不小,但我们正在以高优先级对待它。

相关内容

最新更新