Course contentsShow
← C#
Lesson 1691 of 1,90815. Software Quality and TestingFree lesson

Customizing Test Discovery

Filter test execution based on traits, names, or file modification status.

In this lesson, you will learn how to optimize your test feedback loop by programmatically filtering which tests run during your development cycle.

As your test suite grows, running every single test for every minor change becomes a bottleneck. Rather than relying solely on manual test selection, you can customize test discovery to execute only what is relevant to your current focus. This is achieved by manipulating the metadata or state of your test suite.

Filtering by Traits

The most common approach is using Traits. By decorating your tests with [Trait("Category", "Smoke")] or [Trait("Type", "Integration")], you can instruct the test runner to ignore large swaths of tests. Using the .NET CLI, you narrow the scope instantly:

dotnet test --filter "Category=Smoke"

This is essential for CI pipelines where you might want to run fast unit tests on every commit but delay slow integration tests until the deployment phase.

Filtering by Name

When you are debugging a specific feature, running the entire project is counterproductive. You can leverage pattern matching to execute tests that contain specific strings in their namespace or method name:

dotnet test --filter "FullyQualifiedName~MyFeature.Services"

The ~ operator performs a "contains" search, allowing you to isolate specific business logic modules without moving individual test files.

Filtering by File Modification (Change-Based Testing)

For advanced workflows, you can integrate file system monitoring. By utilizing tools or scripts that track Git status, you can discover which files have changed since your last commit. If a test file or its associated source file hasn't been modified, you can exclude those tests from the current execution. This "delta testing" approach significantly reduces the time between writing code and receiving a green light.

By moving beyond "run everything," you transform your test suite from a slow gatekeeper into a responsive tool that adapts to the task at hand. You aren't just running tests; you are surgically verifying the areas of the system that were actually touched.

Key Takeaway: Customizing test discovery allows you to balance comprehensive verification with rapid iteration by surgically targeting tests via traits, name patterns, and change-set analysis.