This repository was archived by the owner on Aug 26, 2026. It is now read-only.
Repository navigation
Testguideline
Hans Løken edited this page Dec 5, 2024
·
2 revisions
-
Treat Tests as production code
- Write tests with the same care and quality as production code.
- Ensure tests are maintainable, readable, and well-organized.
-
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.
-
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, useUserLogsIn_WithCorrectPassword_AccessGranted.
[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"));
}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
Packages
- Overview
- HelseId Web
- HelseId Worker
- HelseId Api
- Tutorials: