Skip to content

Commit 925a5ec

Browse files
committed
Add example of upgrading test host and WebApplicationFactory to .NET Core 3.0
1 parent cd5f8f6 commit 925a5ec

File tree

118 files changed

+79787
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+79787
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.App" />
10+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
13+
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\ExampleApp2\ExampleApp2.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc.Testing;
3+
using Microsoft.AspNetCore.TestHost;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
using Xunit.Abstractions;
8+
9+
namespace ExampleApp2.IntegrationTests
10+
{
11+
public class ExampleAppTestFixture : WebApplicationFactory<ExampleApp.Program>
12+
{
13+
public ITestOutputHelper Output { get; set; }
14+
15+
protected override IWebHostBuilder CreateWebHostBuilder()
16+
{
17+
var builder = base.CreateWebHostBuilder();
18+
builder.ConfigureLogging(logging =>
19+
{
20+
logging.ClearProviders();
21+
logging.AddXUnit(Output);
22+
});
23+
24+
return builder;
25+
}
26+
27+
protected override void ConfigureWebHost(IWebHostBuilder builder)
28+
{
29+
builder.ConfigureTestServices((services) =>
30+
{
31+
services.RemoveAll(typeof(IHostedService));
32+
});
33+
}
34+
35+
protected override void Dispose(bool disposing)
36+
{
37+
// Have to disable logging here as occurs
38+
// outside a test context
39+
Output = null;
40+
base.Dispose(disposing);
41+
}
42+
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace ExampleApp2.IntegrationTests
8+
{
9+
public class HttpTests: IClassFixture<ExampleAppTestFixture>, IDisposable
10+
{
11+
readonly ExampleAppTestFixture _fixture;
12+
readonly HttpClient _client;
13+
14+
public HttpTests(ExampleAppTestFixture fixture, ITestOutputHelper output)
15+
{
16+
_fixture = fixture;
17+
fixture.Output = output;
18+
_client = fixture.CreateClient();
19+
}
20+
21+
public void Dispose() => _fixture.Output = null;
22+
23+
[Fact]
24+
public async Task CanCallApi()
25+
{
26+
var result = await _client.GetAsync("/");
27+
28+
result.EnsureSuccessStatusCode();
29+
30+
var content = await result.Content.ReadAsStringAsync();
31+
32+
Assert.Contains("Welcome", content);
33+
}
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:50589/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"ExampleApp.IntegrationTests": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:50590/"
25+
}
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.TestHost;
6+
using Xunit;
7+
8+
namespace ExampleApp2.IntegrationTests
9+
{
10+
public class WebHostExampleTests
11+
{
12+
[Fact]
13+
public async Task ShouldReturnHelloWorld()
14+
{
15+
// Arrange
16+
var webHostBuilder = new WebHostBuilder()
17+
.Configure(app => app.Run(async ctx =>
18+
await ctx.Response.WriteAsync("Hello World!")
19+
));
20+
21+
var server = new TestServer(webHostBuilder);
22+
var client = server.CreateClient();
23+
24+
// Act
25+
var response = await client.GetAsync("/");
26+
27+
// Assert
28+
response.EnsureSuccessStatusCode();
29+
var responseString = await response.Content.ReadAsStringAsync();
30+
Assert.Equal("Hello World!", responseString);
31+
}
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
<RootNamespace>ExampleApp</RootNamespace>
7+
</PropertyGroup>
8+
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.App" />
12+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace ExampleApp
7+
{
8+
public class LoggingHostedService : IHostedService
9+
{
10+
readonly ILogger<LoggingHostedService> _logger;
11+
12+
public LoggingHostedService(ILogger<LoggingHostedService> logger)
13+
{
14+
_logger = logger;
15+
}
16+
17+
public async Task StartAsync(CancellationToken cancellationToken)
18+
{
19+
await Task.CompletedTask;
20+
_logger.LogWarning("Starting service");
21+
}
22+
23+
public async Task StopAsync(CancellationToken cancellationToken)
24+
{
25+
await Task.CompletedTask;
26+
_logger.LogWarning("Stopping service");
27+
}
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.RazorPages;
8+
9+
namespace ExampleApp.Pages
10+
{
11+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
12+
public class ErrorModel : PageModel
13+
{
14+
public string RequestId { get; set; }
15+
16+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
17+
18+
public void OnGet()
19+
{
20+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
21+
}
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@page
2+
@model IndexModel
3+
@{
4+
ViewData["Title"] = "Home page";
5+
}
6+
7+
<div class="text-center">
8+
<h1 class="display-4">Welcome</h1>
9+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
10+
</div>

0 commit comments

Comments
 (0)