Перейти к содержанию

Тестирование

Unit тесты

Структура

FuelProcessing.Tests/
├── Core/                    # Тесты Core модуля
│   ├── Models/
│   └── Settings/
├── Data/                    # Тесты Data модуля
│   ├── Repositories/
│   └── Services/
├── Web/                     # Тесты Web модуля
│   ├── Controllers/
│   └── Validation/
└── ClientApi/               # Тесты API
    └── Controllers/

Запуск тестов

# Все тесты
dotnet test

# С покрытием
dotnet test --collect:"XPlat Code Coverage"

# Конкретный проект
dotnet test FuelProcessing.Tests/Core/

# Детальный вывод
dotnet test --logger "console;verbosity=detailed"

Integration тесты

Настройка TestServer

public class WebTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;

    public WebTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task GetHomePage_ReturnsSuccess()
    {
        var response = await _client.GetAsync("/");
        response.EnsureSuccessStatusCode();
    }
}

Тестирование API

# Пример с curl
curl -X POST https://localhost:5002/api/v2/account/authuser \
  -H "Content-Type: application/json" \
  -d '{"email": "test@example.com", "password": "Test123!"}'

Тестирование ВИНК интеграций

Mock API клиентов

public class MockTrackApiService : IApiService
{
    public List<Card> GetCards(int directContractWithVendorId)
    {
        return new List<Card>
        {
            new Card { Number = "1234567890", CardType = CardType.Трасса }
        };
    }
    // ... другие методы
}

Тестирование с TestContainers

# Установка TestContainers для .NET
dotnet add package Testcontainers
dotnet add package Testcontainers.PostgreSql
dotnet add package Testcontainers.RabbitMq

Load тестирование

K6

// load-test.js
import http from 'k6/http';
import { check } from 'k6';

export default function () {
  const res = http.post('https://api.example.com/api/v2/account/authuser',
    JSON.stringify({
      email: 'test@example.com',
      password: 'Test123!'
    }),
    { headers: { 'Content-Type': 'application/json' } }
  );

  check(res, {
    'status is 200': (r) => r.status === 200,
    'has token': (r) => JSON.parse(r.body).isSuccess === true
  });
}
# Запуск
k6 run --vus 100 --duration 30s load-test.js

Метрики покрытия

Модуль Покрытие Цель
Core 60% 80%
Data 50% 70%
Web 40% 60%
ClientApi 70% 80%

CI/CD тесты

.gitlab-ci.yml:

test:
  stage: test
  script:
    - dotnet test --collect:"XPlat Code Coverage"
  coverage: '/Coverage:\s+\d+\.\d+%/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.cobertura.xml

Следующие шаги