Files
Denso/srcs/RobotNet10/Tests/RobotNet10.GlobalPathPlanner.Test/HOW_TO_RUN_TESTS.md
2026-07-03 16:31:37 +07:00

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

  1. Mở Visual Studio
  2. Vào menu TestTest Explorer (hoặc nhấn Ctrl+E, T)
  3. Hoặc vào menu ViewTest Explorer

Bước 2: Build Solution

  1. Nhấn Ctrl+Shift+B hoặc
  2. Vào menu BuildBuild 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

  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

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ó xunitxunit.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:

  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:
{
    "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

  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