-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
247 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,77 @@ | ||
# Logging.Xunit | ||
Microsoft.Extensions.Logging adapter for xUnit test output | ||
# Devodo.Logging.Xunit | ||
|
||
[](https://www.nuget.org/packages/Devodo.Logging.Xunit) | ||
[](https://github.com/devodo/Logging.Xunit/actions/workflows/build.yml) | ||
|
||
A Microsoft.Extensions.Logging adapter for xUnit test output | ||
|
||
# Installing | ||
|
||
Install `Devodo.Logging.Xunit` as a [NuGet package](https://www.nuget.org/packages/Devodo.Logging.Xunit): | ||
|
||
```sh | ||
Install-Package Devodo.Logging.Xunit | ||
``` | ||
|
||
Or via the .NET command line interface: | ||
|
||
```sh | ||
dotnet add package Devodo.Logging.Xunit | ||
``` | ||
|
||
# Usage | ||
|
||
Given a simple ASP.NET Minimal API as follows: | ||
|
||
```csharp | ||
var app = WebApplication.Create(args); | ||
|
||
app.MapGet("/echo/{message}", (string message, ILogger<Program> logger) => | ||
{ | ||
logger.LogInformation("Handling echo request with input: {Message}", message); | ||
|
||
return message; | ||
}); | ||
|
||
app.Run(); | ||
``` | ||
|
||
Log output can be captured in Xunit tests as follows: | ||
|
||
```csharp | ||
public class ExampleTests | ||
{ | ||
private readonly ITestOutputHelper _outputHelper; | ||
|
||
public ExampleTests(ITestOutputHelper outputHelper) | ||
{ | ||
_outputHelper = outputHelper; | ||
} | ||
|
||
[Fact] | ||
public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello() | ||
{ | ||
// ARRANGE | ||
var httpClient = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureLogging(logging => | ||
{ | ||
// Add an xUnit logging provider that writes to the ITestOutputHelper | ||
logging.AddXunit(_outputHelper); | ||
}); | ||
}).CreateClient(); | ||
|
||
// ACT | ||
var response = await httpClient.GetAsync("/echo/hello xunit logging"); | ||
|
||
// ASSERT | ||
Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync()); | ||
} | ||
} | ||
``` | ||
|
||
The test above outputs the following: | ||
``` | ||
info: Program[0] | ||
Handling echo request with input: hello xunit logging | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Xunit.Abstractions; | ||
|
||
namespace Logging.Xunit.ExampleTests; | ||
|
||
public class ExampleTests | ||
{ | ||
private readonly ITestOutputHelper _outputHelper; | ||
|
||
public ExampleTests(ITestOutputHelper outputHelper) | ||
{ | ||
_outputHelper = outputHelper; | ||
} | ||
|
||
[Fact] | ||
public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello() | ||
{ | ||
// ARRANGE | ||
var httpClient = new WebApplicationFactory<Program>().WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureLogging(logging => | ||
{ | ||
// Add an xUnit logging provider that writes to the ITestOutputHelper | ||
logging.AddXunit(_outputHelper); | ||
}); | ||
}).CreateClient(); | ||
|
||
// ACT | ||
var response = await httpClient.GetAsync("/echo/hello xunit logging"); | ||
|
||
// ASSERT | ||
Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
examples/Logging.Xunit.ExampleTests/Logging.Xunit.ExampleTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Logging.Xunit\Logging.Xunit.csproj" /> | ||
<ProjectReference Include="..\Logging.Xunit.ExampleWebApp\Logging.Xunit.ExampleWebApp.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
13 changes: 13 additions & 0 deletions
13
examples/Logging.Xunit.ExampleWebApp/Logging.Xunit.ExampleWebApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="Logging.Xunit.ExampleTests" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var app = WebApplication.Create(args); | ||
|
||
app.MapGet("/echo/{message}", (string message, ILogger<Program> logger) => | ||
{ | ||
logger.LogInformation("Handling echo request with input: {Message}", message); | ||
|
||
return message; | ||
}); | ||
|
||
app.Run(); |
37 changes: 37 additions & 0 deletions
37
examples/Logging.Xunit.ExampleWebApp/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:28128", | ||
"sslPort": 44397 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:5201", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:7139;http://localhost:5201", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/Logging.Xunit.ExampleWebApp/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.