简体   繁体   English

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

[英]Flurl & HttpTest: Unit tests fail when Run All, but pass when run individually

Update: HttpTest is not thread-safe, as per the project's GitHub issue . 更新:根据项目的GitHub问题HttpTest不是线程安全的。 Until the issue is resolved, tests using HttpTest cannot be run in parallel. 在问题解决之前,不能并行运行使用HttpTest测试。

I have a really weird pair of test utilizing Flurl and xUnit that, when Run All in VS Test Explorer, will fail, but if run individually, will pass. 我有一个使用Flurl和xUnit的测试真的很奇怪,当在VS Test Explorer中运行All时,它将失败,但是如果单独运行,则可以通过。 I cannot for the life of me see anywhere where the 2 are even related to each other, but they do. 我一生无法看到两个甚至彼此相关的地方,但它们确实有关系。

I have extracted them out of my project into a new project and the problem persists. 我将它们从项目中提取到一个新项目中,问题仍然存在。 I bundled them into a 7z for anyone interested in loading it to VS, but the full code follows. 我将它们捆绑到7z中,供有兴趣将其加载到VS的任何人使用,但完整代码如下。

Project.Commons 项目通用

GetApi1: 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: 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: 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 单元测试2

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

The comments are correct - lack of thread safety is a known limitation of HttpTest. 注释是正确的-缺乏线程安全性是HttpTest的已知限制。 It is logged and under investigation. 它已记录并正在调查中。 Parallel testing is much more prevalent today than just a couple years ago when this was created, so while a fix is not trivial, we are treating it with high priority. 如今,并行测试比几年前创建时要普及得多,因此,尽管修复程序不容易,但我们还是将其作为高优先级来对待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 为什么使用HostType(“ Moles”)进行的单元测试中的断言在单独运行时可以通过,而在一组测试中运行时却失败? - Why would an assert on a unit test with HostType(“Moles”) pass when run individually, but fail when run with a group of tests? 单元测试在“全部运行”时失败,但在单次运行时通过 - Unit tests fail on Run All but pass when are run single 为什么我的测试一起运行时失败,但单独通过? - Why do my tests fail when run together, but pass individually? 一起运行时单元测试超时,单独运行时成功吗? - Unit Tests timeout when run together, succeed when run individually? 运行所有测试时单元测试失败,但在调试时通过 - Unit Tests failing when I Run All Tests but pass when I Debug 我正在尝试在Visual Studio中运行所有测试,但是当我运行测试时,第一个测试将通过,但所有其他测试将失败 - I am trying to run all tests in visual studio, but when I run the the tests the first one will pass, but all the others will fail 一些 Nunit 测试在组中失败,但在其他环境中单独运行时通过 - Some Nunit tests are failing in groups but pass when run individually in other env C#Selenium WebDriver测试在连续运行时失败但在单独运行时成功 - C# Selenium WebDriver tests failing when run consecutively but succeeding when run individually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM