Visual Studio
04 / 04

Testing & Extensions

Testing & Extensions

Visual Studio's Test Explorer, Live Unit Testing, and Code Coverage tools provide a first-class testing experience. The extension marketplace adds capabilities from code quality tools to AI assistants.

Test Explorer

# Open Test Explorer
# Test → Test Explorer (Ctrl+E, T)

# Supported test frameworks (via NuGet packages):
# MSTest:   Microsoft.VisualStudio.TestTools.UnitTesting
# xUnit:    xunit + xunit.runner.visualstudio
# NUnit:    NUnit + NUnit3TestAdapter

# Run tests
Ctrl+R, A       # Run All Tests
Ctrl+R, T       # Run test under cursor
Ctrl+R, L       # Rerun last test run (failed tests by default)
Ctrl+R, Ctrl+A  # Debug All Tests
Ctrl+R, Ctrl+T  # Debug test under cursor

# Test filtering — search box in Test Explorer
# Traits:  Trait:"Category:Integration"
# Outcome: Outcome:Failed  |  Outcome:Passed  |  Outcome:NotRun
# Project: Project:MyProject.Tests

# Playlist — save a subset of tests to run repeatedly
# Select tests → right-click → Add to Playlist → New Playlist
# Saved as .playlist file in solution

# Test output — view when a test fails
# Click failed test in Explorer → Output pane shows assertion message + stack trace
# Console.WriteLine() and Debug.WriteLine() appear in test output

# Sample xUnit test class
# using Xunit;
# using Moq;
# public class OrderServiceTests {
#   [Fact]
#   public async Task ProcessOrder_ValidOrder_ReturnsOrderId() {
#     var mockRepo = new Mock<IOrderRepository>();
#     mockRepo.Setup(r => r.SaveAsync(It.IsAny<Order>())).ReturnsAsync("order-123");
#     var svc = new OrderService(mockRepo.Object);
#     var result = await svc.ProcessAsync(new Order { Amount = 100 });
#     Assert.Equal("order-123", result.Id);
#   }
#   [Theory]
#   [InlineData(0)]
#   [InlineData(-1)]
#   public async Task ProcessOrder_InvalidAmount_ThrowsException(decimal amount) {
#     var svc = new OrderService(Mock.Of<IOrderRepository>());
#     await Assert.ThrowsAsync<ArgumentException>(() => svc.ProcessAsync(new Order { Amount = amount }));
#   }
# }

Live Unit Testing & Code Coverage

# Live Unit Testing (Visual Studio Enterprise only)
# Test → Live Unit Testing → Start
# Runs affected tests in background as you type
# Shows inline icons next to each line of code:
#   Green checkmark ✓ — covered by a passing test
#   Red X ✗           — covered by a failing test
#   Blue dash —        — not covered by any test

# Pause LUT during refactoring (prevents constant re-runs)
# Test → Live Unit Testing → Pause

# Configure LUT to exclude generated code / specific projects
# .runsettings file (can be specified in Test → Configure Run Settings):
# <RunSettings>
#   <LiveUnitTesting>
#     <Inclusions>
#       <Include><ModulePath>MyProject.Core</ModulePath></Include>
#     </Inclusions>
#     <Exclusions>
#       <Exclude><ModulePath>.*Generated.*</ModulePath></Exclude>
#     </Exclusions>
#   </LiveUnitTesting>
# </RunSettings>

# Code Coverage (requires VS Enterprise; VS Community/Pro with coverlet)
# VS Enterprise: Test → Analyze Code Coverage for All Tests
# Results shown in Code Coverage Results window
# Highlights covered (blue) and uncovered (orange/red) lines in editor

# coverlet (open source — works with all VS editions)
# dotnet add package coverlet.collector
# dotnet add package ReportGenerator (for HTML report)
dotnet test --collect:"XPlat Code Coverage" --results-directory ./coverage
dotnet tool run reportgenerator -- -reports:"coverage/**/coverage.cobertura.xml" -targetdir:"coverage-report" -reporttypes:Html
# Open coverage-report/index.html in browser

Popular Extensions

# Install extensions: Extensions → Manage Extensions → Online tab
# Or download .vsix from marketplace.visualstudio.com

# Productivity
# ReSharper (JetBrains)       — most popular; refactoring, navigation, code analysis
#                               Adds Alt+Enter (context action), Ctrl+Alt+Shift+T (refactor)
#                               Expensive but transformative; free for students/OSS
# CodeMaid                    — clean up formatting, organize using statements, sort members
# Productivity Power Tools    — (free, Microsoft) PowerCommands, Solution Error Visualizer

# Code Quality & Analysis
# SonarLint                   — real-time code smells, bugs, security vulnerabilities
#                               Integrates with SonarQube/SonarCloud server
# .NET Upgrade Assistant      — automated migration from .NET Framework to .NET 6/7/8

# AI Coding Assistants
# GitHub Copilot              — AI code completion, chat; $10/mo or free for students
#                               Install: Extensions → Manage Extensions → search "GitHub Copilot"
# IntelliCode                 — (free, Microsoft) ML-based completion, starred suggestions

# Version Control
# GitFlow for Visual Studio   — visual branch management for GitFlow workflow
# Git History Visualizer      — improved git log/blame view in VS

# .vsix installation (offline)
# 1. Download .vsix from marketplace
# 2. Double-click the .vsix file → VS Extension Installer opens
# 3. Or: Extensions → Manage Extensions → Installed → Install from VSIX

# Solution templates
# File → New → Project → search template name
# Install additional templates via NuGet: dotnet new install
dotnet new install Ardalis.CleanArchitecture.Template
dotnet new cleanarch -o MyProject

# View installed templates
dotnet new list

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free