【问题标题】:xUnit unable to output to console .NETxUnit 无法输出到控制台 .NET
【发布时间】:2020-12-09 22:41:18
【问题描述】:

尝试学习如何使用 xunit 在控制台中显示输出并苦苦挣扎。我正在使用 VSCode 并在 VSCode 终端中使用 dotnet test 运行测试。我也尝试过从 VSCode 之外的终端运行。

我也试过在VSCode的代码窗口中运行Debug Test上面的Test函数,但是收到了一个OmniSharp Argument Exception。

我使用dotnet new xunit 创建了一个新测试项目,并使用ITestOutputHelper 使用以下测试类。

using System;
using System.IO;
using System.Threading.Tasks;

using Xunit;
using Xunit.Abstractions;
using System.Text.Json;
using WebApp.Data;
using WebApp.UnitTests.Fixtures;


namespace WebApp.UnitTests
{
    [Collection("Motion collection")]
    public class MotionInfoTest
    {
        private const string MotionResourceFile = @"WebApp.UnitTests.TestData.motion.json";

        private MotionDetectionFixture _fixture;
        private ITestOutputHelper _output;

        public MotionInfoTest(MotionDetectionFixture fixture, ITestOutputHelper output) {
            _fixture = fixture;
            _output = output;
        }

        [Fact]
        // public async Task TestMotionInfo()
        public void TestMotionInfo()
        {
           try {
               Stream s = _fixture.GetStream(MotionResourceFile);
               
               _output.WriteLine(s.ReadTimeout.ToString());
               if(s is null) {
                   throw new Exception ("ArgumentException");
               }
               _output.WriteLine("Finished TestMotionInfo");
           }
           catch(Exception e) {
               _output.WriteLine("Exception occurred {0}", e.Message);
           }
            //MotionDetection obj = await JsonSerializer.DeserializeAsync<MotionDetection>(_fixture.GetStream(MotionResourceFile));

            Assert.Equal(1,1);
            //Assert.Equal("TensorFlow-WithFiltering-And-MQTT", obj.Plug);
        }
    }
}

dotnet new xunit创建的csproj文件为:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\WebApp.Data\WebApp.Data.csproj" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Include="TestData\*.json" />
  </ItemGroup>

</Project>

有没有人设法将输出显示到控制台?

【问题讨论】:

    标签: .net-core xunit.net


    【解决方案1】:

    如果我使用以下命令,则输出会出现在终端窗口和 VS Code 嵌入式终端中:

    dotnet test --logger "console;verbosity=detailed"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2013-03-18
      • 2011-05-22
      相关资源
      最近更新 更多