【问题标题】:Xunit.net running Async method leads to error when running at onceXunit.net运行Async方法导致一次运行时出错
【发布时间】:2015-04-15 15:16:46
【问题描述】:

针对另一个SO questions,我在使用 Xunit 和 Visual Studio 2015 ctp6 运行异步任务时遇到了一个问题。

代码如下:

    using System;
    using System.Threading.Tasks;
    using Microsoft.AspNet.TestHost;
    using Microsoft.Framework.DependencyInjection;
    using Xunit;
    using Microsoft.AspNet.Builder;
    using System.Net.Http;

    namespace Multi.Web.Api
    {
        public class TestServerHelper : IDisposable
        {
            public TestServerHelper()
            {
                ClientProvider = new TestClientProvider();

                ApiServer = TestServer.Create((app) =>
                {
                    app.UseServices(services =>
                    {
                        services.AddTransient<IClientProvider>(s => ClientProvider);
                    });
                    app.UseMulti();
                });
            }
            public TestClientProvider ClientProvider { get; private set; }

            public TestServer ApiServer { get; private set; }

            public void Dispose()
            {
                ApiServer.Dispose();
                ClientProvider.Dispose();
            }
        }

        public class MultiMiddlewareTest : IClassFixture<TestServerHelper>
        {

            TestServerHelper _testServerHelper;

            public MultiMiddlewareTest(TestServerHelper testServerHelper)
            {
                _testServerHelper = testServerHelper;

            }

            [Fact]
            public async Task ShouldReturnToday()
            {
                using (HttpClient client = _testServerHelper.ApiServer.CreateClient())
                {
                    var response = await client.GetAsync("http://localhost/today");

                    String content = await response.Content.ReadAsStringAsync();
                    Assert.Equal(content, "2015-04-15 count is 1");
                }
            }

            [Fact]
            public async Task ShouldReturnYesterday()
            {
                using (HttpClient client = _testServerHelper.ApiServer.CreateClient())
                {
                    var response = await client.GetAsync("http://localhost/yesterday");

                    String content = await response.Content.ReadAsStringAsync();
                    Assert.Equal(content, "2015-04-14 count is 1");
                }
            }
        }
    }

在Visual Studio TestExplorer中,在运行测试时(右键单击并选择debug selected test)是可以的,但是运行所有时,没有通过,我有以下错误

Message : Response status code does not indicate success : 404 (Not Fount)

所有代码都可以在另一个问题中找到,在那个问题中,我回答了如何使用多个 TestServer 实例来模拟外部 Api。我认为这与一些同步上下文有关。

我认为我的 Helper 写得不好,因为我看到它在调用实际完成之前处理对象(有时不是......)。有人有同样的问题并对此有解决方案吗?

更新link to full code on github

【问题讨论】:

  • 你使用的是 xunit.runner.kre 还是 xunit.runnner.aspnet。 kre runner 中存在问题:github.com/xunit/aspnet.xunit/issues/20。但是使用aspnet runner还有一个问题:VS 2015 CT6 doesn't discover tests
  • 我正在使用 xunit.runnner.aspnet。对于未发现的测试,如果您将文件命名为与类相同的名称,则可以。所以在文件 MyTest.cs 中,类名必须是 MyTest。如果您有不同的名称,我看到 VS TestExplorer 无限循环并尝试发现但什么也没发现。

标签: c# asp.net asp.net-core xunit.net


【解决方案1】:

当您运行所有测试时,Xunit 默认会并行运行您的测试;我猜这可能会导致您的测试服务器发生冲突并产生副作用。 (我不确定您在 project.json 中使用的所有包,所以我可能会弄错。)查看 Xunit 文档中的 Running tests in parallel 并找到适合您的解决方案。

选项:

  1. 使用CollectionAttribute,例如[Collection("Localhost http server")]
  2. 在命令行中指定-parallel none 作为选项。
  3. 使用程序集属性更改默认行为,例如[assembly: CollectionBehavior(DisableTestParallelization = true)]

更新 2

更新 1 是一个红鲱鱼,所以我完全删除了它。从 FakeExternalApi 中间件中删除 await next(context); 似乎已经消除了间歇性问题。 A comment from @Tratcher 表示“不鼓励调用 Next ...”,但我不确定这是否与 OwinMiddleware 特别相关,或者它是否是一个很好的一般建议,但它似乎适用于此。

【讨论】:

  • 事实上,我看到如果测试方法在同一个类中,它们就不会并行运行。不过,我尝试使用这些 cmets 进行操作,但没有成功。我会等到一些 xunit 库不再处于测试阶段。
  • @CedricDumont,我已经根据您的完整源代码更新了我的答案。感谢您提供!
  • 我在 repo 上提交了更改,但它并没有解决问题。注意:我使用的是视觉工作室 ctp6。也许它来自那里。
  • 哦,哇,是断断续续的;如果我在没有调试的情况下运行,它通常会很好。寻找更多!
  • 希望我终于解决了你的问题,@CedricDumont。这是一个有趣的调试;我自己还没有制作任何中间件。
猜你喜欢
  • 1970-01-01
  • 2022-10-13
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 2021-06-27
相关资源
最近更新 更多