【发布时间】: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>
有没有人设法将输出显示到控制台?
【问题讨论】: