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

Test Execution via CLI

Master the dotnet test command-line interface for CI integration.

In this lesson, you will learn how to orchestrate professional-grade test suites directly through the dotnet test command-line interface to ensure seamless automation in your CI/CD pipelines.

Moving Beyond the IDE

While we often rely on Visual Studio or JetBrains Rider to trigger tests, true CI integration requires mastery of the CLI. The dotnet test command is a powerful wrapper that discovers, compiles, and executes your test projects. To integrate effectively with build gates, you must move beyond simple execution and start leveraging CLI arguments to control the environment.

Fine-Tuning Execution

When running tests in an automated environment, you often need to filter what runs to keep feedback loops tight. You can use the --filter flag to execute specific subsets of tests based on traits, names, or namespaces:

dotnet test --filter "Category=Integration&Priority=High" --configuration Release

Beyond filtering, CLI control is essential for reporting. For CI servers (like GitHub Actions or Azure DevOps), you rarely want the default console output. Instead, you need machine-readable formats. You can direct the test runner to output results as a TRX (Visual Studio Test Results) file, which CI platforms can parse to render beautiful dashboards and historical graphs:

dotnet test --logger "trx;LogFileName=test-results.trx"

CI/CD Considerations

When executing in CI, your CLI strategy should focus on two goals: determinism and performance.

  1. Determinism: Always specify --configuration Release to ensure you are testing the same binaries that will be deployed.
  2. Performance: Use --no-build if your CI pipeline has already performed a dotnet build step. This prevents redundant compilation and significantly speeds up your pipeline execution.

Furthermore, environment variable injection is your best friend here. If your tests depend on configuration providers, you can override values at runtime without modifying your codebase:

dotnet test --env ConnectionStrings:Default="Server=local;Database=TestDb;"

By mastering these CLI switches, you ensure that your testing strategy is portable, reproducible, and ready for any orchestration platform.

Key Takeaway: Mastering dotnet test via CLI allows you to decouple your test execution from the IDE, enabling optimized, automated, and report-driven validation within your CI/CD pipelines.