5.4 KiB
5.4 KiB
Hướng dẫn chạy Unit Tests
🚀 Cách 1: Sử dụng Visual Studio
Bước 1: Mở Test Explorer
- Mở Visual Studio
- Vào menu Test → Test Explorer (hoặc nhấn
Ctrl+E, T) - Hoặc vào menu View → Test Explorer
Bước 2: Build Solution
- Nhấn
Ctrl+Shift+Bhoặc - Vào menu Build → Build Solution
Bước 3: Chạy Tests
- Chạy tất cả tests: Click nút Run All (▶️) ở thanh toolbar Test Explorer
- Chạy test cụ thể: Right-click vào test method → Run
- Chạy tests trong một class: Right-click vào test class → Run
Bước 4: Xem kết quả
- Kết quả hiển thị trong Test Explorer
- ✅ Xanh = Pass
- ❌ Đỏ = Fail
- ⚠️ Vàng = Skipped
🖥️ Cách 2: Sử dụng Command Line (dotnet CLI)
Bước 1: Mở Terminal/Command Prompt
- Windows: PowerShell hoặc Command Prompt
- Visual Studio: View → Terminal hoặc
Ctrl+`
Bước 2: Navigate đến project folder
cd "D:\Phenikaa.Data\Day49_RobotNetV2\RobotNet10\srcs\RobotNet10\Tests\RobotNet10.GlobalPathPlanner.Test"
Bước 3: Chạy tests
Chạy tất cả tests:
dotnet test
Chạy với output chi tiết:
dotnet test --verbosity normal
Chạy với output rất chi tiết:
dotnet test --verbosity detailed
Chạy tests cụ thể (theo tên class):
dotnet test --filter "FullyQualifiedName~DifferentialPlannerTests"
Chạy tests cụ thể (theo tên method):
dotnet test --filter "FullyQualifiedName~PathPlanning_WithValidGraph_ShouldReturnPath"
Chạy tests với code coverage:
dotnet test --collect:"XPlat Code Coverage"
📝 Cách 3: Sử dụng Visual Studio Code
Bước 1: Cài đặt Extension
- Mở VS Code
- Vào Extensions (
Ctrl+Shift+X) - Tìm và cài đặt:
- .NET Core Test Explorer
- C# (Microsoft)
Bước 2: Mở Test Explorer
- Click vào icon Test ở sidebar (hoặc
Ctrl+Shift+T) - Tests sẽ tự động được discover
Bước 3: Chạy Tests
- Click icon ▶️ bên cạnh test để chạy
- Click icon ▶️ bên cạnh test class để chạy tất cả tests trong class
🔧 Troubleshooting
Vấn đề: Tests không được discover
Giải pháp 1: Rebuild solution
dotnet clean
dotnet build
Giải pháp 2: Restore packages
dotnet restore
Giải pháp 3: Kiểm tra project file
- Đảm bảo có
Microsoft.NET.Test.Sdk - Đảm bảo có
xunitvàxunit.runner.visualstudio
Vấn đề: "No test is available"
Giải pháp: Kiểm tra namespace và using statements
- Đảm bảo tests có
using Xunit; - Đảm bảo test methods có attribute
[Fact]
Vấn đề: Tests fail với lỗi "Could not load file or assembly"
Giải pháp: Rebuild và restore
dotnet clean
dotnet restore
dotnet build
dotnet test
📊 Ví dụ Output
Khi chạy thành công:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 50, Skipped: 0, Total: 50, Duration: 2 s
Khi có test fail:
Failed! - Failed: 2, Passed: 48, Skipped: 0, Total: 50, Duration: 2 s
🎯 Chạy Tests theo Category
Chạy tests nhanh (không có timeout):
dotnet test --filter "FullyQualifiedName~GlobalNodeTests"
Chạy tests cho Factory:
dotnet test --filter "FullyQualifiedName~PathPlannerFactoryTests"
Chạy tests cho Planner:
dotnet test --filter "FullyQualifiedName~DifferentialPlannerTests"
🔍 Debug Tests
Trong Visual Studio:
- Đặt breakpoint trong test method
- Right-click test → Debug
- Code sẽ dừng tại breakpoint
Trong VS Code:
- Đặt breakpoint trong test method
- Tạo file
.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Test",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "dotnet",
"args": ["test", "--no-build"],
"cwd": "${workspaceFolder}/srcs/RobotNet10/Tests/RobotNet10.GlobalPathPlanner.Test",
"console": "internalConsole",
"stopAtEntry": false
}
]
}
📈 Test Reports
Tạo HTML report:
dotnet test --logger "html;LogFileName=test-results.html"
Tạo trx report (cho Visual Studio):
dotnet test --logger "trx;LogFileName=test-results.trx"
⚡ Tips
- Chạy tests thường xuyên: Sau mỗi lần thay đổi code
- Chạy tests trước khi commit: Đảm bảo không break existing functionality
- Sử dụng Test Explorer: Dễ dàng xem và chạy tests
- Xem test output: Để hiểu tại sao test fail
- Debug failing tests: Sử dụng debugger để tìm nguyên nhân