Created
July 17, 2018 08:13
-
-
Save sandcastle/949b0219f771675c7dafba28dce853e1 to your computer and use it in GitHub Desktop.
Asp.net Core 2.1 Xunit Test Configuration
This file contains hidden or 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
| public static class Tests | |
| { | |
| public const string Integration = "IntegrationTests"; | |
| } | |
| public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup> | |
| { | |
| protected override IWebHostBuilder CreateWebHostBuilder() | |
| { | |
| return new WebHostBuilder() | |
| .UseSerilog() | |
| .UseStartup<Startup>(); | |
| } | |
| } | |
| public class IntegrationTestFixture : ICollectionFixture<IntegrationTestApplicationFactory> | |
| { | |
| public IntegrationTestFixture() | |
| { | |
| var factory = new IntegrationTestApplicationFactory(); | |
| Client = factory.CreateClient(); | |
| } | |
| public readonly HttpClient Client; | |
| } | |
| [CollectionDefinition(Tests.Integration)] | |
| public class IntegrationTestsCollection : ICollectionFixture<IntegrationTestFixture> | |
| { | |
| // This class has no code, and is never created. Its purpose is simply | |
| // to be the place to apply [CollectionDefinition] and all the | |
| // ICollectionFixture<> interfaces. | |
| } | |
| [Collection(Tests.Integration)] | |
| public class HealthTests | |
| { | |
| readonly IntegrationTestFixture _fixture; | |
| public HealthTests(IntegrationTestFixture fixture) | |
| { | |
| _fixture = fixture; | |
| } | |
| [Fact] | |
| public async Task Ping_should_return_the_date() | |
| { | |
| var result = await _fixture.Client | |
| .For("http://localhost/ping") | |
| .GetAsync(); | |
| await result.EnsureValid(); | |
| string content = await result.Content.ReadAsStringAsync(); | |
| Assert.True(result.IsSuccessStatusCode); | |
| Assert.True(content.Length > 0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment