Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect logs from WebApplicationFactory to XUnit console for easier debugging #196

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageVersion Include="Azure.Identity" Version="1.10.1" />
<PackageVersion Include="Bogus" Version="34.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Meziantou.Extensions.Logging.Xunit" Version="1.0.6" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
Expand All @@ -32,6 +33,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.0.0" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.0.1" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions sample/CustomerApi.Tests/CustomerApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Bogus" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Meziantou.Extensions.Logging.Xunit" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
Expand Down
22 changes: 15 additions & 7 deletions sample/CustomerApi.Tests/CustomerController/CreateCustomerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
using CustomerApi.Uris;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace CustomerApi.Tests.CustomerController;

public class CreateCustomerTests
{
private readonly ITestOutputHelper _testOutputHelper;

public CreateCustomerTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Valid_ReturnsOk(bool disableAutoProvisioning)
{
using var customerApi = new TestCustomerApi(disableAutoProvisioning);
using var customerApi = new TestCustomerApi(_testOutputHelper, disableAutoProvisioning);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Create");

var client = customerApi.CreateClient(authHeader);
Expand All @@ -30,7 +38,7 @@ public async Task Valid_ReturnsOk(bool disableAutoProvisioning)
[InlineData(false)]
public async Task Valid_StoredCorrectly(bool disableAutoProvisioning)
{
using var customerApi = new TestCustomerApi(disableAutoProvisioning);
using var customerApi = new TestCustomerApi(_testOutputHelper, disableAutoProvisioning);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Create");

var client = customerApi.CreateClient(authHeader);
Expand All @@ -51,7 +59,7 @@ await customerApi.Then.TheCustomerShouldMatch(
[Fact]
public async Task Invalid_DuplicateEmailAddress()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var customerUri = CustomerUri.Parse("/customers/CustomerId");
await customerApi.Given.AnExistingCustomer(customerUri, emailAddress: "bob@bobertson.co.uk");
Expand All @@ -69,7 +77,7 @@ public async Task Invalid_DuplicateEmailAddress()
[Fact]
public async Task Valid_DeleteCustomer_DuplicateEmailAddress()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var customerUri = CustomerUri.Parse("/customers/CustomerId");
await customerApi.Given.AnExistingCustomer(customerUri, emailAddress: "bob@bobertson.co.uk");
Expand All @@ -96,7 +104,7 @@ public async Task Valid_DeleteCustomer_DuplicateEmailAddress()
[InlineData("not-an-email-address", "Bob", "Bobertson")]
public async Task Invalid_ReturnsBadRequest(string email, string firstName, string lastName)
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Create");

var client = customerApi.CreateClient(authHeader);
Expand All @@ -110,7 +118,7 @@ public async Task Invalid_ReturnsBadRequest(string email, string firstName, stri
[Fact]
public async Task Unauthorized()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var client = customerApi.CreateClient();
var request = new CreateCustomerRequest("bob@bobertson.co.uk", "Bob", "Bobertson");
Expand All @@ -123,7 +131,7 @@ public async Task Unauthorized()
[Fact]
public async Task Forbidden()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.InvalidRole");

var client = customerApi.CreateClient(authHeader);
Expand Down
22 changes: 15 additions & 7 deletions sample/CustomerApi.Tests/CustomerController/DeleteCustomerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
using CustomerApi.Uris;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace CustomerApi.Tests.CustomerController;

public class DeleteCustomerTests
{
private readonly ITestOutputHelper _testOutputHelper;

public DeleteCustomerTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task NoExisting_ReturnsNotFound()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Delete");
var client = customerApi.CreateClient(authHeader);

Expand All @@ -24,7 +32,7 @@ public async Task Existing_ReturnsNoContent()
{
var customerUri = CustomerUri.Parse("/customers/CustomerId");

using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Delete");
await customerApi.Given.AnExistingCustomer(customerUri);
var client = customerApi.CreateClient(authHeader);
Expand All @@ -39,7 +47,7 @@ public async Task ExistingDeleted_ReturnsNoContent()
{
var customerUri = CustomerUri.Parse("/customers/CustomerId");

using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Delete");
await customerApi.Given.AnExistingCustomer(customerUri);
await customerApi.Given.TheCustomerIsDeleted(customerUri);
Expand All @@ -55,7 +63,7 @@ public async Task Existing_HasBeenRemoved()
{
var customerUri = CustomerUri.Parse("/customers/CustomerId");

using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Delete");
await customerApi.Given.AnExistingCustomer(customerUri);
var client = customerApi.CreateClient(authHeader);
Expand All @@ -70,7 +78,7 @@ public async Task ExistingDeleted_HasBeenRemoved()
{
var customerUri = CustomerUri.Parse("/customers/CustomerId");

using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Delete");
await customerApi.Given.AnExistingCustomer(customerUri);
await customerApi.Given.TheCustomerIsDeleted(customerUri);
Expand All @@ -84,7 +92,7 @@ public async Task ExistingDeleted_HasBeenRemoved()
[Fact]
public async Task Unauthorized()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var client = customerApi.CreateClient();

var response = await client.DeleteAsync("/customers/CustomerId");
Expand All @@ -95,7 +103,7 @@ public async Task Unauthorized()
[Fact]
public async Task Forbidden()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.InvalidRole");
var client = customerApi.CreateClient(authHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
using FluentAssertions;
using LogOtter.JsonHal;
using Xunit;
using Xunit.Abstractions;

namespace CustomerApi.Tests.CustomerController;

public class GetAllCustomersTests
{
private readonly ITestOutputHelper _testOutputHelper;

public GetAllCustomersTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task Valid_ReturnsOk()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"), "bob@bobertson.co.uk", "Bob", "Bobertson");
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/AnotherExistingUser"), "bobetta@bobson.co.uk", "Bobetta", "Bobson");
Expand Down Expand Up @@ -59,7 +67,7 @@ public async Task Valid_ReturnsOk()
[Fact]
public async Task Valid_MultiplePages_Page1()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
for (var i = 0; i < 10; i++)
{
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse($"/customers/ExistingUser{i}"));
Expand Down Expand Up @@ -92,7 +100,7 @@ public async Task Valid_MultiplePages_Page1()
[Fact]
public async Task Valid_MultiplePages_Page2()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
for (var i = 0; i < 20; i++)
{
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse($"/customers/ExistingUser{i}"));
Expand Down Expand Up @@ -126,7 +134,7 @@ public async Task Valid_MultiplePages_Page2()
[Fact]
public async Task Valid_NoCustomers_ReturnsOk()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Read");

Expand All @@ -152,7 +160,7 @@ public async Task Valid_NoCustomers_ReturnsOk()
[InlineData("foo")]
public async Task Invalid_Page(string page)
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Read");

Expand All @@ -166,7 +174,7 @@ public async Task Invalid_Page(string page)
[Fact]
public async Task Unauthorized()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"));

var client = customerApi.CreateClient();
Expand All @@ -179,7 +187,7 @@ public async Task Unauthorized()
[Fact]
public async Task Forbidden()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"));
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.InvalidRole");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
using CustomerApi.Uris;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace CustomerApi.Tests.CustomerController;

public class GetCustomerByIdTests
{
private readonly ITestOutputHelper _testOutputHelper;

public GetCustomerByIdTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public async Task Valid_ReturnsOk()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"), "bob@bobertson.co.uk", "Bob", "Bobertson");

var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Read");
Expand All @@ -36,7 +44,7 @@ public async Task Valid_ReturnsOk()
[Fact]
public async Task NotFound()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);

var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Read");

Expand All @@ -50,7 +58,7 @@ public async Task NotFound()
[Fact]
public async Task Unauthorized()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"));

var client = customerApi.CreateClient();
Expand All @@ -63,7 +71,7 @@ public async Task Unauthorized()
[Fact]
public async Task Forbidden()
{
using var customerApi = new TestCustomerApi();
using var customerApi = new TestCustomerApi(_testOutputHelper);
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"));
var authHeader = await customerApi.Given.AnExistingConsumer("Customers.InvalidRole");

Expand Down
Loading