diff --git a/TUnit.AspNetCore/TestWebApplicationFactory.cs b/TUnit.AspNetCore/TestWebApplicationFactory.cs index 65cd53eb7f..4af8d8bd06 100644 --- a/TUnit.AspNetCore/TestWebApplicationFactory.cs +++ b/TUnit.AspNetCore/TestWebApplicationFactory.cs @@ -21,7 +21,6 @@ public WebApplicationFactory GetIsolatedFactory( TestContext testContext, WebApplicationTestOptions options, Action configureIsolatedServices, - Action configureIsolatedStartupConfiguration, Action configureIsolatedAppConfiguration, Action? configureWebHostBuilder = null) { @@ -29,7 +28,6 @@ public WebApplicationFactory GetIsolatedFactory( { var configurationBuilder = new ConfigurationManager(); ConfigureStartupConfiguration(configurationBuilder); - configureIsolatedStartupConfiguration(configurationBuilder); foreach (var keyValuePair in configurationBuilder.AsEnumerable()) { diff --git a/TUnit.AspNetCore/WebApplicationTest.cs b/TUnit.AspNetCore/WebApplicationTest.cs index 8512b05431..a81923172e 100644 --- a/TUnit.AspNetCore/WebApplicationTest.cs +++ b/TUnit.AspNetCore/WebApplicationTest.cs @@ -115,7 +115,6 @@ public async Task InitializeFactoryAsync(TestContext testContext) testContext, _options, ConfigureTestServices, - ConfigureTestConfiguration, (_, config) => ConfigureTestConfiguration(config), ConfigureWebHostBuilder)); diff --git a/TUnit.Example.Asp.Net.TestProject/FactoryMethodOrderTests.cs b/TUnit.Example.Asp.Net.TestProject/FactoryMethodOrderTests.cs index f40ee370d6..18343cea7a 100644 --- a/TUnit.Example.Asp.Net.TestProject/FactoryMethodOrderTests.cs +++ b/TUnit.Example.Asp.Net.TestProject/FactoryMethodOrderTests.cs @@ -10,7 +10,7 @@ namespace TUnit.Example.Asp.Net.TestProject; /// /// This allows: /// - Factory to provide base configuration shared across tests -/// - Tests to override factory defaults via ConfigureTestConfiguration (order 5) +/// - Tests to override factory defaults via ConfigureTestConfiguration (order 6) /// public class FactoryMethodOrderTests : TestsBase { @@ -105,7 +105,7 @@ await Assert.That(ConfigureTestServicesCalledOrder) } [Test] - [DisplayName("Complete relative execution order: Options → Setup → Config → WebHost → Services → Startup")] + [DisplayName("Complete relative execution order: Options → Setup → WebHost → Config → Services → Startup")] public async Task Full_Relative_Order() { _ = Factory.CreateClient(); @@ -113,8 +113,8 @@ public async Task Full_Relative_Order() // Verify all hooks were called await Assert.That(ConfigureTestOptionsCalledOrder).IsGreaterThan(0); await Assert.That(SetupCalledOrder).IsGreaterThan(0); - await Assert.That(ConfigureTestConfigurationCalledOrder).IsGreaterThan(0); await Assert.That(ConfigureWebHostBuilderCalledOrder).IsGreaterThan(0); + await Assert.That(ConfigureTestConfigurationCalledOrder).IsGreaterThan(0); await Assert.That(ConfigureTestServicesCalledOrder).IsGreaterThan(0); await Assert.That(StartupCalledOrder).IsGreaterThan(0); @@ -124,16 +124,16 @@ await Assert.That(ConfigureTestOptionsCalledOrder) .Because("ConfigureTestOptions runs before SetupAsync"); await Assert.That(SetupCalledOrder) - .IsLessThan(ConfigureTestConfigurationCalledOrder) - .Because("SetupAsync runs before ConfigureTestConfiguration"); - - await Assert.That(ConfigureTestConfigurationCalledOrder) .IsLessThan(ConfigureWebHostBuilderCalledOrder) - .Because("ConfigureTestConfiguration runs before ConfigureWebHostBuilder"); + .Because("SetupAsync runs before ConfigureWebHostBuilder"); await Assert.That(ConfigureWebHostBuilderCalledOrder) + .IsLessThan(ConfigureTestConfigurationCalledOrder) + .Because("ConfigureWebHostBuilder runs before ConfigureTestConfiguration"); + + await Assert.That(ConfigureTestConfigurationCalledOrder) .IsLessThan(ConfigureTestServicesCalledOrder) - .Because("ConfigureWebHostBuilder runs before ConfigureTestServices"); + .Because("ConfigureTestConfiguration runs before ConfigureTestServices"); await Assert.That(ConfigureTestServicesCalledOrder) .IsLessThan(StartupCalledOrder) diff --git a/TUnit.Example.Asp.Net.TestProject/TestsBase.cs b/TUnit.Example.Asp.Net.TestProject/TestsBase.cs index 71e23d77cb..6e176950b1 100644 --- a/TUnit.Example.Asp.Net.TestProject/TestsBase.cs +++ b/TUnit.Example.Asp.Net.TestProject/TestsBase.cs @@ -67,13 +67,8 @@ protected override void ConfigureTestServices(IServiceCollection services) protected override void ConfigureTestConfiguration(IConfigurationBuilder config) { - // Track first call only (this method is called twice - once for startup config, once for app config) - if (ConfigureTestConfigurationCalledOrder == 0) - { - ConfigureTestConfigurationCalledOrder = GetNextOrder(); - ConfigureTestConfigurationCalledAt = DateTime.UtcNow; - } - + ConfigureTestConfigurationCalledOrder = GetNextOrder(); + ConfigureTestConfigurationCalledAt = DateTime.UtcNow; base.ConfigureTestConfiguration(config); } diff --git a/docs/docs/examples/aspnet.md b/docs/docs/examples/aspnet.md index 6a37760caa..97ef80494f 100644 --- a/docs/docs/examples/aspnet.md +++ b/docs/docs/examples/aspnet.md @@ -124,8 +124,8 @@ Understanding the execution order is critical for writing correct tests. Here's │ 3. Factory.ConfigureWebHost Base factory configuration │ │ 4. Factory.ConfigureStartup... Base factory startup config │ │ ─────────────────────────────────────────────────────────── │ -│ 5. ConfigureTestConfiguration Test config (overrides factory) │ -│ 6. ConfigureWebHostBuilder Escape hatch (low-level access) │ +│ 5. ConfigureWebHostBuilder Escape hatch (low-level access) │ +│ 6. ConfigureTestConfiguration Test config (overrides factory) │ │ 7. ConfigureTestServices Test services (overrides) │ │ ─────────────────────────────────────────────────────────── │ │ 8. Application Startup Server starts │ @@ -143,8 +143,8 @@ Understanding the execution order is critical for writing correct tests. Here's | `SetupAsync` | Per-test | Async operations before config (create DB tables) | | `Factory.ConfigureWebHost` | Shared | Base configuration for all tests | | `Factory.ConfigureStartupConfiguration` | Shared | Base startup configuration | -| `ConfigureTestConfiguration` | Per-test | Override factory configuration | | `ConfigureWebHostBuilder` | Per-test | Low-level escape hatch | +| `ConfigureTestConfiguration` | Per-test | Override factory configuration | | `ConfigureTestServices` | Per-test | Override factory services | :::tip Tests Can Override Factory @@ -790,7 +790,7 @@ The key benefits: **Problem:** You set a value in `ConfigureTestConfiguration` but the factory's value is still used. -**Solution:** Make sure you're using the same configuration key. The test configuration runs **after** the factory configuration (step 5 vs steps 3-4), so it should override. Check that: +**Solution:** Make sure you're using the same configuration key. The test configuration runs **after** the factory configuration (step 6 vs steps 3-4), so it should override. Check that: 1. You're using `AddInMemoryCollection` which adds to the config sources 2. The configuration key path is exactly the same @@ -927,14 +927,14 @@ public class LifecycleDebugTest : WebApplicationTest