Running and Debugging Tests
Execute and debug your .NET tests directly from VS Code with integrated test explorer functionality.
Overview
The C# Dev Tools extension provides seamless integration with VS Code's native Test Explorer, allowing you to run, debug, and manage your .NET tests without leaving the editor.
Test Explorer
Accessing Test Explorer
The Test Explorer is available in multiple ways:
- Activity Bar: Click the Testing icon (flask/beaker icon) in the Activity Bar
- Command Palette: Press
Ctrl+Shift+P
and type "Test: Focus on Test Explorer View" - Status Bar: Click on the test status indicator in the status bar
Test Explorer Features
The Test Explorer provides a hierarchical view of your tests:
- Test Projects: Organized by project and namespace
- Test Classes: Group tests by class
- Individual Tests: Each test method is listed
- Test Status: Visual indicators for pass/fail/pending
- Filter Options: Show only failed, passed, or all tests
Running Tests
Run All Tests
Execute all tests in your solution:
- Open Test Explorer
- Click the "Run All Tests" button (play icon at the top)
- Watch test execution in real-time
- View results in the Test Explorer
Alternatively, use the Command Palette:
- Press
Ctrl+Shift+P
- Type "Test: Run All Tests"
- Press Enter
Run Specific Tests
Run Single Test
- Navigate to the test in Test Explorer
- Click the play icon next to the test
- View the result immediately
Run Test Class
- Find the test class in Test Explorer
- Click the play icon next to the class
- All tests in the class execute
Run Test Project
- Locate the test project in Test Explorer
- Click the play icon next to the project
- All tests in the project execute
Run Tests from Code Editor
Execute tests directly from your code:
Using Code Lens
- Open a test file
- Look for "Run Test" links above test methods
- Click "Run Test" to execute
- Click "Debug Test" to debug
Using Context Menu
- Right-click on a test method
- Select "Run Test" from the context menu
- Test executes and results appear in Test Explorer
Using Keyboard Shortcuts
- Place cursor in a test method
- Press the configured shortcut (check settings)
- Test executes immediately
Debugging Tests
Debug Single Test
Start debugging a specific test:
- Open Test Explorer or your test file
- Click the "Debug Test" icon next to the test
- Debugger attaches automatically
- Set breakpoints in your test or production code
- Step through code execution
From Code Lens
- Open test file
- Click "Debug Test" link above test method
- Debugger starts with breakpoint at test
Debug Configuration
Tests use the standard .NET debugging configuration:
- Breakpoints work in both test and production code
- Watch variables and expressions
- Call stack navigation
- Console output visible in Debug Console
Debug Test Class or Project
- Right-click on test class/project in Test Explorer
- Select "Debug Tests"
- All tests in the scope run with debugger attached
- Execution pauses at any breakpoint
Test Results
Viewing Results
Test results appear in multiple locations:
In Test Explorer
- Green checkmark: Test passed
- Red X: Test failed
- Yellow circle: Test skipped
- Gray circle: Not yet run
Output Panel
- Open Output panel (
Ctrl+Shift+U
) - Select "Test" from dropdown
- View detailed test execution logs
Problems Panel
- Failed test assertions appear as problems
- Click to navigate to failing test
- Shows expected vs actual values
Test Output
View detailed output for each test:
- Click on a test in Test Explorer
- View test output in the bottom panel
- See:
- Execution time
- Console output
- Error messages
- Stack traces
Failed Tests
When tests fail:
- Red X appears in Test Explorer
- Error message displayed
- Stack trace available
- Click to jump to failing line
- Fix and re-run
Test Filtering
Filter by Status
Filter tests based on their execution status:
- Show All: Display all tests
- Show Failed: Only failed tests
- Show Passed: Only successful tests
- Show Not Run: Tests not yet executed
Click the filter icon in Test Explorer to toggle filters.
Search Tests
Find specific tests quickly:
- Click in the Test Explorer search box
- Type test name, class, or namespace
- Results filter as you type
- Clear search to show all tests
Continuous Testing
Watch Mode
Run tests automatically when code changes:
- Open Command Palette (
Ctrl+Shift+P
) - Type "Test: Toggle Auto Run"
- Tests run automatically on file save
- Immediate feedback on code changes
Test Coverage
View code coverage after running tests:
- Run tests with coverage enabled
- Coverage indicators appear in gutter
- Green: Line covered
- Red: Line not covered
- Coverage report in Output panel
Test Configuration
Run Settings
Configure test execution behavior:
- Create
runsettings
file in your project - Configure:
- Test execution timeout
- Parallel execution
- Test adapters
- Data collection
Example .runsettings file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<ResultsDirectory>.\TestResults</ResultsDirectory>
<TargetFramework>net8.0</TargetFramework>
</RunConfiguration>
<MSTest>
<Parallelize>
<Workers>4</Workers>
</Parallelize>
</MSTest>
</RunSettings>
Extension Settings
Configure test-related settings:
- Open Settings (
Ctrl+,
) - Search for "csharp-dev-tools.testing"
- Adjust:
- Auto-discover tests
- Test adapter paths
- Default test framework
- Code lens visibility
Keyboard Shortcuts
Common shortcuts for testing:
- Run Test: Varies (configure in Keyboard Shortcuts)
- Debug Test: Varies (configure in Keyboard Shortcuts)
- Run All Tests: Varies (configure in Keyboard Shortcuts)
- Focus Test Explorer:
Ctrl+Shift+P
→ "Test: Focus on Test Explorer View"
Customizing Shortcuts
- Open Command Palette (
Ctrl+Shift+P
) - Type "Preferences: Open Keyboard Shortcuts"
- Search for "test"
- Assign your preferred shortcuts
Test Frameworks Support
The extension supports multiple test frameworks:
xUnit
- Automatic test discovery
- Fact and Theory tests
- Inline and member data
- Test collections
NUnit
- Test fixtures and tests
- TestCase attributes
- SetUp and TearDown
- Parameterized tests
MSTest
- TestClass and TestMethod
- DataRow attributes
- Initialize and Cleanup
- Test categories
Integration with CI/CD
Test Results Export
Export test results for CI/CD pipelines:
- Results saved in standard formats (TRX, JUnit)
- Configure output directory
- Integrate with build systems
- Track test history
Command Line Integration
Run tests from terminal:
dotnet test
Results sync with Test Explorer automatically.
Troubleshooting
Tests Not Appearing
If tests don't appear in Test Explorer:
- Ensure test project references test framework NuGet packages
- Build the project (
Ctrl+Shift+B
) - Check Output panel for errors
- Reload window (
Ctrl+Shift+P
→ "Developer: Reload Window")
Tests Fail to Run
If tests won't execute:
- Verify .NET SDK is installed
- Check project builds successfully
- Ensure test framework adapters are installed
- Review test configuration files
Debugger Not Attaching
If debugger doesn't attach:
- Check launch.json configuration
- Verify .NET debugger is enabled
- Ensure project is built in Debug mode
- Try restarting VS Code
Slow Test Execution
If tests run slowly:
- Enable parallel test execution
- Check for slow tests (use profiling)
- Optimize test setup/teardown
- Review resource-intensive operations
Best Practices
Organizing Tests
- Group related tests in test classes
- Use descriptive test names
- Organize by feature or component
- Keep test projects separate
Running Tests Efficiently
- Run affected tests only during development
- Use continuous testing for immediate feedback
- Run full suite before commits
- Debug individual tests when troubleshooting
Test Maintenance
- Keep tests fast and focused
- Remove obsolete tests
- Update tests with code changes
- Monitor test reliability
Related Features
- Test Generation - Auto-generate unit tests
- SpecFlow Support - BDD testing
- Playwright Integration - E2E testing
- Code Coverage - Analyze test coverage
- Test Explorer API - VS Code testing integration