Skip to content
This repository was archived by the owner on Aug 26, 2026. It is now read-only.

Testguideline

Hans Løken edited this page Dec 5, 2024 · 2 revisions

WIP:Testing guideline

General guideline

  1. Treat Tests as production code

    • Write tests with the same care and quality as production code.
    • Ensure tests are maintainable, readable, and well-organized.
  2. Make Tests Easy to Understand

    • Use clear and consistent naming conventions to describe the purpose and behavior being tested.
    • Follow the AAA (Arrange, Act, Assert) pattern to structure tests logically:
      • Arrange: Set up the context and prerequisites.
      • Act: Perform the action being tested.
      • Assert: Verify the expected outcomes.
  3. Adopt the GWT (Given, When, Then) Naming Pattern

    • Use test names that describe the scenario without explicitly including "Given", "When", or "Then".
    • Example: Instead of GivenUserLogsIn_WhenPasswordIsCorrect_ThenAccessIsGranted, use UserLogsIn_WithCorrectPassword_AccessGranted.

Example

    [Test]
    public async Task ConfiguredXXisTrue_CallTestApiEnpointPerson_ReturnYYinResponse()
    {
        // Arrange
        var appsettingsConfig = new Dictionary<string, string?>
        {
            "XX", "true" },
            
        };
        var testConfiguration = appsettingsConfig.BuildInMemoryConfiguration();
        // Use testframework application factory to override configurations and services
        var appFactory = new TestWebApplicationFactory(testConfiguration, services =>
        {
            ..   
        });

        // Act
        var client = appFactory.CreateClient();
        var response = await client.GetAsync("api/person");

        // Assert
         var content = await response.Content.Content.ReadAsStringAsync();
         Assert.That(content, Is.EqualTo("YY"));

    }

Testframework

In .NET, the following are commonly used builders for configuring and testing applications:

  • HostBuilder
  • WebApplicationBuilder (introduced in .NET 6 with the minimal API model)
  • IWebHostBuilder (used by WebApplicationFactory) These builders can be used in integration tests to simulate different configurations, mock services, or test various application setups. It allows to test different configurations and to simulate different usages by mocking out parts of the services.

The Testframework has some helperclass for WebApplicationBuilder and IWebHostBuilder. See comparisation table below for spesification and when to use what.

Feature WebApplicationBuilderTestHost WebApplicationFactoryTestHost HostBuilder
Based on WebApplicationBuilder (introduced in .NET 6) IWebHostBuilder Not currently in the testframework
Description Simplified setup for ASP.NET Core apps, focusing on minimal hosting. Configures web servers in ASP.NET Core for integration testing. Provides general-purpose hosting for any .NET app.
When to Use? Use when testing specific parts of the application, such as middleware or configurations. Use to test applications with an existing Program.cs entry point. Use for non-web applications, such as console apps or worker services.
Supported Since .NET 6.0 ASP.NET Core 1.0 .NET Core 2.1

Sample can be found in repository xxx

Clone this wiki locally