Testing Integration
Running and Debugging Tests
Execute and debug your .NET tests directly from VS Code with integrated test explorer functionality.
Overview
Section titled “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
Section titled “Test Explorer”Accessing Test Explorer
Section titled “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+Pand type “Test: Focus on Test Explorer View” - Status Bar: Click on the test status indicator in the status bar
Test Explorer Features
Section titled “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
Section titled “Running Tests”Run All Tests
Section titled “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
Section titled “Run Specific Tests”Run Single Test
Section titled “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
Section titled “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
Section titled “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
Section titled “Run Tests from Code Editor”Execute tests directly from your code:
Using Code Lens
Section titled “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
Section titled “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
Section titled “Using Keyboard Shortcuts”- Place cursor in a test method
- Press the configured shortcut (check settings)
- Test executes immediately
Debugging Tests
Section titled “Debugging Tests”Debug Single Test
Section titled “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
Section titled “From Code Lens”- Open test file
- Click “Debug Test” link above test method
- Debugger starts with breakpoint at test
Debug Configuration
Section titled “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
Section titled “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
Section titled “Test Results”Viewing Results
Section titled “Viewing Results”Test results appear in multiple locations:
In Test Explorer
Section titled “In Test Explorer”- Green checkmark: Test passed
- Red X: Test failed
- Yellow circle: Test skipped
- Gray circle: Not yet run
Output Panel
Section titled “Output Panel”- Open Output panel (
Ctrl+Shift+U) - Select “Test” from dropdown
- View detailed test execution logs
Problems Panel
Section titled “Problems Panel”- Failed test assertions appear as problems
- Click to navigate to failing test
- Shows expected vs actual values
Test Output
Section titled “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
Section titled “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
Section titled “Test Filtering”Filter by Status
Section titled “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
Section titled “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
Section titled “Continuous Testing”Watch Mode
Section titled “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
Section titled “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
Section titled “Test Configuration”Run Settings
Section titled “Run Settings”Configure test execution behavior:
- Create
runsettingsfile 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
Section titled “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
Section titled “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
Section titled “Customizing Shortcuts”- Open Command Palette (
Ctrl+Shift+P) - Type “Preferences: Open Keyboard Shortcuts”
- Search for “test”
- Assign your preferred shortcuts
Test Frameworks Support
Section titled “Test Frameworks Support”The extension supports multiple test frameworks:
- Automatic test discovery
- Fact and Theory tests
- Inline and member data
- Test collections
- Test fixtures and tests
- TestCase attributes
- SetUp and TearDown
- Parameterized tests
MSTest
Section titled “MSTest”- TestClass and TestMethod
- DataRow attributes
- Initialize and Cleanup
- Test categories
Integration with CI/CD
Section titled “Integration with CI/CD”Test Results Export
Section titled “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
Section titled “Command Line Integration”Run tests from terminal:
dotnet testResults sync with Test Explorer automatically.
Troubleshooting
Section titled “Troubleshooting”Tests Not Appearing
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “Best Practices”Organizing Tests
Section titled “Organizing Tests”- Group related tests in test classes
- Use descriptive test names
- Organize by feature or component
- Keep test projects separate
Running Tests Efficiently
Section titled “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
Section titled “Test Maintenance”- Keep tests fast and focused
- Remove obsolete tests
- Update tests with code changes
- Monitor test reliability
Related Features
Section titled “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