Skip to content

Commit

Permalink
Внедрение ITestOutputHelper через xUnit DI +semver:skip (#116)
Browse files Browse the repository at this point in the history
* Внедрение ITestOutputHelper через xUnit DI

* re ident 4 spaces
  • Loading branch information
Stepami authored Oct 9, 2024
1 parent c36f9bf commit d29fb72
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="Xunit.DependencyInjection" Version="9.4.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System.CommandLine.Parsing;
using FluentAssertions;
using Xunit.Abstractions;

namespace HydraScript.IntegrationTests.ErrorPrograms;

public class VariableInitializationTests(
TestHostFixture fixture,
ITestOutputHelper testOutputHelper) : IClassFixture<TestHostFixture>
public class VariableInitializationTests(TestHostFixture fixture) : IClassFixture<TestHostFixture>
{
[Theory, MemberData(nameof(VariableInitializationScripts))]
public void VariableWithoutTypeDeclared_AccessedBeforeInitialization_ExitCodeHydraScriptError(string script)
{
var runner = fixture.GetRunner(
testOutputHelper,
configureTestServices: services => services.SetupInMemoryScript(script));
configureTestServices: services =>
services.SetupInMemoryScript(script));
var code = runner.Invoke(fixture.InMemoryScript);
code.Should().Be(ExitCodes.HydraScriptError);
fixture.LogMessages.Should()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<NoWarn>CA1816</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="MartinCostello.Logging.XUnit" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="Xunit.DependencyInjection" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
7 changes: 7 additions & 0 deletions tests/HydraScript.IntegrationTests/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HydraScript.IntegrationTests;

// ReSharper disable once UnusedType.Global
/// <summary>
/// https://github.com/pengweiqhca/Xunit.DependencyInjection#4-default-startup
/// </summary>
public class Startup;
7 changes: 2 additions & 5 deletions tests/HydraScript.IntegrationTests/SuccessfulProgramsTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.CommandLine.Parsing;
using FluentAssertions;
using Xunit.Abstractions;

namespace HydraScript.IntegrationTests;

public class SuccessfulProgramsTests(
TestHostFixture fixture,
ITestOutputHelper testOutputHelper) : IClassFixture<TestHostFixture>
public class SuccessfulProgramsTests(TestHostFixture fixture) : IClassFixture<TestHostFixture>
{
[Theory, MemberData(nameof(SuccessfulProgramsNames))]
public void Invoke_NoError_ReturnCodeIsZero(string fileName)
{
var runner = fixture.GetRunner(testOutputHelper);
var runner = fixture.GetRunner();
var code = runner.Invoke([$"Samples/{fileName}"]);
code.Should().Be(ExitCodes.Success);
}
Expand Down
17 changes: 12 additions & 5 deletions tests/HydraScript.IntegrationTests/TestHostFixture.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;
using HydraScript.Infrastructure;
using MartinCostello.Logging.XUnit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace HydraScript.IntegrationTests;

public class TestHostFixture : IDisposable
public class TestHostFixture(
Xunit.DependencyInjection.ITestOutputHelperAccessor accessor) :
IDisposable, ITestOutputHelperAccessor
{
private readonly List<string> _logMessages = [];

public readonly string[] InMemoryScript = ["file.js"];
public IReadOnlyCollection<string> LogMessages => _logMessages;

public Parser GetRunner(
ITestOutputHelper testOutputHelper,
Action<IServiceCollection>? configureTestServices = null) =>
public ITestOutputHelper? OutputHelper
{
get => accessor.Output;
set { }
}

public Parser GetRunner(Action<IServiceCollection>? configureTestServices = null) =>
Program.GetRunner(configureHost: builder => builder
.ConfigureLogging(x => x.ClearProviders()
.AddXUnit(testOutputHelper)
.AddXUnit(this)
.AddFakeLogging(options =>
{
options.OutputSink = logMessage => _logMessages.Add(logMessage);
Expand Down

0 comments on commit d29fb72

Please sign in to comment.