# Hướng dẫn chạy Unit Tests ## 🚀 Cách 1: Sử dụng Visual Studio ### Bước 1: Mở Test Explorer 1. Mở Visual Studio 2. Vào menu **Test** → **Test Explorer** (hoặc nhấn `Ctrl+E, T`) 3. Hoặc vào menu **View** → **Test Explorer** ### Bước 2: Build Solution 1. Nhấn `Ctrl+Shift+B` hoặc 2. 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 ```bash 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: ```bash dotnet test ``` #### Chạy với output chi tiết: ```bash dotnet test --verbosity normal ``` #### Chạy với output rất chi tiết: ```bash dotnet test --verbosity detailed ``` #### Chạy tests cụ thể (theo tên class): ```bash dotnet test --filter "FullyQualifiedName~DifferentialPlannerTests" ``` #### Chạy tests cụ thể (theo tên method): ```bash dotnet test --filter "FullyQualifiedName~PathPlanning_WithValidGraph_ShouldReturnPath" ``` #### Chạy tests với code coverage: ```bash dotnet test --collect:"XPlat Code Coverage" ``` ## 📝 Cách 3: Sử dụng Visual Studio Code ### Bước 1: Cài đặt Extension 1. Mở VS Code 2. Vào Extensions (`Ctrl+Shift+X`) 3. Tìm và cài đặt: - **.NET Core Test Explorer** - **C#** (Microsoft) ### Bước 2: Mở Test Explorer 1. Click vào icon **Test** ở sidebar (hoặc `Ctrl+Shift+T`) 2. 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 ```bash dotnet clean dotnet build ``` **Giải pháp 2**: Restore packages ```bash dotnet restore ``` **Giải pháp 3**: Kiểm tra project file - Đảm bảo có `Microsoft.NET.Test.Sdk` - Đảm bảo có `xunit` và `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 ```bash 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): ```bash dotnet test --filter "FullyQualifiedName~GlobalNodeTests" ``` ### Chạy tests cho Factory: ```bash dotnet test --filter "FullyQualifiedName~PathPlannerFactoryTests" ``` ### Chạy tests cho Planner: ```bash dotnet test --filter "FullyQualifiedName~DifferentialPlannerTests" ``` ## 🔍 Debug Tests ### Trong Visual Studio: 1. Đặt breakpoint trong test method 2. Right-click test → **Debug** 3. Code sẽ dừng tại breakpoint ### Trong VS Code: 1. Đặt breakpoint trong test method 2. Tạo file `.vscode/launch.json`: ```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: ```bash dotnet test --logger "html;LogFileName=test-results.html" ``` ### Tạo trx report (cho Visual Studio): ```bash dotnet test --logger "trx;LogFileName=test-results.trx" ``` ## ⚡ Tips 1. **Chạy tests thường xuyên**: Sau mỗi lần thay đổi code 2. **Chạy tests trước khi commit**: Đảm bảo không break existing functionality 3. **Sử dụng Test Explorer**: Dễ dàng xem và chạy tests 4. **Xem test output**: Để hiểu tại sao test fail 5. **Debug failing tests**: Sử dụng debugger để tìm nguyên nhân ## 📚 Tài liệu tham khảo - [xUnit Documentation](https://xunit.net/) - [.NET Testing Documentation](https://docs.microsoft.com/en-us/dotnet/core/testing/) - [dotnet test command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)