October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

ASP.NET Testing: A Practical Guide to Unit, Integration, and Browser Tests

A practical ASP.NET Core testing strategy: use unit tests for isolated logic, WebApplicationFactory for focused integration coverage, and browser automation for user-facing flows.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use unit tests for isolated application logic, integration tests for important behavior across components and infrastructure, and browser automation when you need to verify a user-facing flow. In ASP.NET Core, WebApplicationFactory<TEntryPoint> provides a practical integration-test host: it starts the application in a test server and gives your tests an HTTP client.

The key is to choose the least costly test that proves the behavior. Keep most routine logic in unit tests; reserve integration and browser tests for the boundaries where wiring, infrastructure, or the actual UI matters.

What each test level should prove

Test level What it exercises Use it to answer
Unit An individual unit of work, focused on code under your control Does this method or handler produce the right result for these inputs?
Integration Two or more components working together, often including infrastructure Does the application pipeline work with its services, data access, or other important dependencies?
Browser/end-to-end A real browser interacting with the user-facing application Can a user complete an important UI flow?

Microsoft advises that unit tests should focus on code within the developer’s control and recommends choosing a unit test when either a unit or integration test can verify the behavior. Integration tests use production components, require more code and data processing, and take longer, so keep them focused on important infrastructure scenarios.

Build a balanced ASP.NET Core test plan

Put routine logic in unit tests

Test individual methods or units of work without making them depend on databases, file systems, or network resources. When code interacts with infrastructure, use fakes or mocks where appropriate. For Minimal API handlers that return IResult, Microsoft’s example demonstrates unit testing with xUnit and replacing an external database with an in-memory database.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use integration tests at important boundaries

Choose representative cases that verify components work together: for example, request and response handling through the application pipeline, or important database operations. Microsoft recommends a focused set of infrastructure scenarios, such as representative reads, writes, updates, and deletes, rather than reproducing every combination of data behavior in integration tests.

Add browser automation for user-facing flows

For a single-page application, browser automation can check behavior that an HTTP client test cannot establish, such as whether a user-facing flow works in a browser. Microsoft points to Playwright for .NET as one option for SPA testing. Treat browser tests as a separate layer; the available guidance does not prescribe a full browser-test architecture.

Choose a test framework and platform

A test framework is the tool you write tests with; a test platform is the engine that runs them and communicates with your IDE or command line. Microsoft lists MSTest, NUnit, TUnit, and xUnit.net as framework choices, and VSTest and Microsoft.Testing.Platform as platform choices. TUnit is built on Microsoft.Testing.Platform and does not support VSTest; the MSTest, NUnit, and xUnit.net documentation describes support for both platforms.

The sources do not establish one framework as universally best. Compare the options against your project and team:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Compatibility with the application’s target .NET version and chosen test platform.
  • How the framework fits your IDE and command-line workflow.
  • Team familiarity and the cost of migrating existing tests.
  • Required runner and SDK packages.
  • Existing conventions and integrations in the project.

Check the framework’s current official documentation for compatibility and package requirements before pinning versions. These details can change.

Set up ASP.NET Core integration tests with WebApplicationFactory

The typical setup is a separate test project that references the application under test and the Microsoft.AspNetCore.Mvc.Testing package. Use the Web SDK in the test project. WebApplicationFactory<TEntryPoint> uses the application entry point—usually Program.cs—to create a TestServer and an HTTP client for requests and responses.

Expose the entry point for minimal hosting

For an application using minimal hosting, the generated Program type may not be visible to the test project by default. Microsoft documents two options in the application project: add an InternalsVisibleTo declaration for the test assembly, or make the entry point public with a partial declaration at the end of Program.cs:

public partial class Program { }

Use the assembly name of your test project if you choose InternalsVisibleTo. Keep this adjustment in the application project, not in the test code.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write a request-and-response integration test

Assuming the application has an HTTP endpoint at /health that returns a successful response, the test can use the factory’s client:

Rank #4
Test-Drive ASP.NET MVC
  • New
  • Mint Condition
  • Dispatch same day for order received before 12 noon
  • Guaranteed packaging
  • No quibbles returns
using System.Net;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

public class HealthEndpointTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;

    public HealthEndpointTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task Health_returns_success()
    {
        using var response = await _client.GetAsync("/health");

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}

This test exercises the app’s request pipeline and route through the test host; it is not a test of a real network deployment. Replace /health and the expected response with a route and behavior your application actually provides. Add assertions for meaningful response content or headers when those are part of the contract.

Run the test project

From the solution directory, run dotnet test path/to/YourTests.csproj. The test project must reference the application project, Microsoft.AspNetCore.Mvc.Testing, a test framework, and the appropriate runner/platform packages for its setup. Microsoft’s example uses xUnit, xunit.runner.visualstudio, and AngleSharp; it notes that setups using xunit.runner.visualstudio 2.4.2 or later must also reference Microsoft.NET.Test.Sdk. That threshold is specific to the described setup, so check current package guidance and your target framework rather than copying old versions blindly.

Configure the test host and its dependencies

Tests should use deliberate, safe settings and data, not silently inherit assumptions about production. Microsoft notes that if the SUT environment is unset, it defaults to Development. Configure test-specific settings and data so the test host cannot accidentally connect to production services.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When the default host is not representative or safe for a test, customize the factory’s web host and service collection. This lets you adjust configuration, replace dependencies, or change authentication behavior. A common pattern is to substitute a test database or service while preserving the application’s request pipeline and the integration boundary the test is meant to exercise.

Keep unit and integration tests in separate projects when doing so helps keep infrastructure dependencies out of the unit suite or lets the team control which suite runs. Separation is useful when it serves those goals, not a requirement for every application.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo is a screenshot API and MCP server, not an ASP.NET test runner or a replacement for assertions. It can capture a rendered page for visual review or an artifact without requiring you to set up browser automation for that capture. One GET request returns an image or PDF; for example, this cURL request saves a WebP screenshot of your deployed page:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://your-app.example/ -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. An MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Learn about ScreenshotNeo, then sign up for the free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot common integration-test problems

  • The test cannot find Program. For minimal hosting, expose the generated entry point with InternalsVisibleTo or a public partial Program declaration, and ensure the test references the correct application project.
  • The test runner does not discover tests. Check that the test project has the framework, runner, and test SDK packages needed for its chosen platform. In Microsoft’s cited xUnit runner setup, xunit.runner.visualstudio 2.4.2 or later also requires Microsoft.NET.Test.Sdk.
  • The request returns an unexpected status or content. Confirm the route, method, configuration, and test data. A test client exercises the in-process test host, so investigate host setup and replaced services as well as endpoint logic.
  • The test uses the wrong environment or service. Set the test environment and configuration deliberately, and replace production dependencies with test-safe alternatives where needed.
  • A unit test is slow or fails when infrastructure is unavailable. The test may be exercising a database, file system, or network dependency. Isolate the unit and use fakes or mocks where appropriate; cover the relevant integration boundary in a focused integration test.

Keep the suite useful over time

Prefer fast, isolated tests for routine logic and add integration coverage where component wiring or infrastructure could break important behavior. Browser tests add another level for user-facing flows, but should answer questions that the lower-level tests cannot. This division keeps the suite focused while still checking the parts of the application that matter at each boundary.

Frequently Asked Questions

Can WebApplicationFactory test a deployed application over the network?

No. It creates a test server from the application entry point and supplies a client for requests to that test host; it is not a network test of a deployed environment.

Do I need a separate project for integration tests?

A separate test project is useful when it isolates infrastructure dependencies or lets your team control which suite runs. The guidance does not make it mandatory.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.