Skip to content

Integration Testing

Giriş

Integration Testing (Entegrasyon Testi), yazılım bileşenlerinin birbiriyle etkileşimini test etmek için kullanılan bir test türüdür. Bu testler, farklı modüllerin, servislerin veya sistemlerin birlikte doğru çalışıp çalışmadığını kontrol eder.

Integration Testing'in Önemi

  1. Sistem Bütünlüğü
  2. Bileşenler arası etkileşimleri doğrulama
  3. Sistem genelinde veri akışını test etme
  4. Bileşen entegrasyonlarını kontrol etme

  5. Hata Tespiti

  6. Bileşenler arası iletişim hatalarını bulma
  7. Veri dönüşüm hatalarını tespit etme
  8. API uyumsuzluklarını belirleme

  9. Sistem Davranışı

  10. Gerçek dünya senaryolarını test etme
  11. Sistem performansını ölçme
  12. Ölçeklenebilirliği test etme

Integration Testing Türleri

  1. Big Bang Testing
  2. Tüm bileşenlerin aynı anda entegre edilmesi
  3. Hızlı test süreci
  4. Hata tespiti zorluğu

  5. Top-Down Testing

  6. Üst seviyeden başlayarak test etme
  7. Stub'lar kullanma
  8. Kritik modülleri öncelikli test etme

  9. Bottom-Up Testing

  10. Alt seviyeden başlayarak test etme
  11. Driver'lar kullanma
  12. Temel fonksiyonları önce test etme

  13. Sandwich Testing

  14. Top-Down ve Bottom-Up yaklaşımların kombinasyonu
  15. Paralel test süreci
  16. Karmaşık sistemler için uygun

.NET'te Integration Testing

TestServer Kullanımı

public class IntegrationTests
{
    private readonly TestServer _server;
    private readonly HttpClient _client;

    public IntegrationTests()
    {
        _server = new TestServer(new WebHostBuilder()
            .UseStartup<Startup>());
        _client = _server.CreateClient();
    }

    [Fact]
    public async Task GetUsers_ReturnsSuccessStatusCode()
    {
        // Act
        var response = await _client.GetAsync("/api/users");

        // Assert
        response.EnsureSuccessStatusCode();
        var content = await response.Content.ReadAsStringAsync();
        var users = JsonConvert.DeserializeObject<List<User>>(content);
        Assert.NotEmpty(users);
    }
}

Entity Framework Core ile Test

public class UserRepositoryTests
{
    private readonly DbContextOptions<ApplicationDbContext> _options;

    public UserRepositoryTests()
    {
        _options = new DbContextOptionsBuilder<ApplicationDbContext>()
            .UseInMemoryDatabase(databaseName: "TestDb")
            .Options;
    }

    [Fact]
    public async Task CreateUser_ShouldAddUserToDatabase()
    {
        // Arrange
        using (var context = new ApplicationDbContext(_options))
        {
            var repository = new UserRepository(context);
            var user = new User { Name = "Test User" };

            // Act
            await repository.CreateUser(user);

            // Assert
            var savedUser = await context.Users.FirstOrDefaultAsync();
            Assert.NotNull(savedUser);
            Assert.Equal("Test User", savedUser.Name);
        }
    }
}

Integration Testing Best Practices

  1. Test Ortamı
  2. Gerçeğe yakın test ortamı
  3. İzole edilmiş test veritabanı
  4. Harici servislerin mock'lanması

  5. Test Verileri

  6. Gerçekçi test verileri
  7. Veri temizleme stratejileri
  8. Test verilerinin yönetimi

  9. Test Organizasyonu

  10. Mantıksal test grupları
  11. Bağımlılık yönetimi
  12. Test sırası kontrolü

Mülakat Soruları ve Cevapları

Temel Sorular

  1. Integration Testing nedir ve neden önemlidir?
  2. Cevap: Integration Testing, yazılım bileşenlerinin birbiriyle etkileşimini test etmek için kullanılan bir test türüdür. Önemi:

    • Bileşenler arası etkileşimleri doğrular
    • Sistem bütünlüğünü kontrol eder
    • Entegrasyon hatalarını erken tespit eder
    • Sistem davranışını doğrular
    • Performans sorunlarını belirler
  3. Unit Testing ve Integration Testing arasındaki farklar nelerdir?

  4. Cevap:

    • Unit Testing:
    • Tek bir bileşeni test eder
    • Bağımlılıklar mock'lanır
    • Hızlı çalışır
    • İzole edilmiş testler
    • Integration Testing:
    • Birden fazla bileşeni test eder
    • Gerçek bağımlılıklar kullanılır
    • Daha yavaş çalışır
    • Sistem genelinde testler
  5. Integration Testing türleri nelerdir?

  6. Cevap:

    • Big Bang Testing
    • Top-Down Testing
    • Bottom-Up Testing
    • Sandwich Testing
    • Continuous Integration Testing
  7. Integration Testing'in avantajları ve dezavantajları nelerdir?

  8. Cevap:

    • Avantajlar:
    • Sistem bütünlüğünü doğrular
    • Gerçek senaryoları test eder
    • Performans sorunlarını tespit eder
    • Dezavantajlar:
    • Test süresi uzundur
    • Bakım maliyeti yüksektir
    • Hata tespiti zordur
  9. Integration Testing'de hangi araçlar kullanılır?

  10. Cevap:
    • TestServer (ASP.NET Core)
    • Entity Framework Core
    • xUnit/NUnit
    • Docker
    • Postman
    • Swagger

Teknik Sorular

  1. TestServer nasıl kullanılır?
  2. Cevap:

    public class ApiTests
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;
    
        public ApiTests()
        {
            _server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>()
                .ConfigureTestServices(services =>
                {
                    services.AddScoped<IService, MockService>();
                }));
            _client = _server.CreateClient();
        }
    
        [Fact]
        public async Task GetData_ReturnsExpectedResult()
        {
            var response = await _client.GetAsync("/api/data");
            response.EnsureSuccessStatusCode();
        }
    }
    

  3. In-Memory Database nasıl kullanılır?

  4. Cevap:

    public class DatabaseTests
    {
        private readonly DbContextOptions<AppDbContext> _options;
    
        public DatabaseTests()
        {
            _options = new DbContextOptionsBuilder<AppDbContext>()
                .UseInMemoryDatabase(databaseName: "TestDb")
                .Options;
        }
    
        [Fact]
        public async Task SaveData_ShouldWork()
        {
            using (var context = new AppDbContext(_options))
            {
                var repository = new Repository(context);
                await repository.Save(new Data());
    
                Assert.True(await context.Data.AnyAsync());
            }
        }
    }
    

  5. Integration Test'lerde mock nasıl kullanılır?

  6. Cevap:

    public class ServiceTests
    {
        [Fact]
        public async Task ProcessData_ShouldWork()
        {
            var mockService = new Mock<IExternalService>();
            mockService.Setup(x => x.GetData())
                      .ReturnsAsync(new Data());
    
            var service = new Service(mockService.Object);
            var result = await service.Process();
    
            Assert.NotNull(result);
        }
    }
    

  7. Test verileri nasıl yönetilir?

  8. Cevap:

    public class TestData
    {
        public static List<User> GetTestUsers()
        {
            return new List<User>
            {
                new User { Id = 1, Name = "User1" },
                new User { Id = 2, Name = "User2" }
            };
        }
    }
    
    public class UserTests
    {
        [Fact]
        public void TestWithData()
        {
            var users = TestData.GetTestUsers();
            // Test logic
        }
    }
    

  9. Test sırası nasıl kontrol edilir?

  10. Cevap:
    [Collection("IntegrationTests")]
    public class OrderedTests
    {
        [Fact]
        [TestOrder(1)]
        public void FirstTest()
        {
            // Test logic
        }
    
        [Fact]
        [TestOrder(2)]
        public void SecondTest()
        {
            // Test logic
        }
    }
    

Pratik Sorular

  1. Aşağıdaki API endpoint'ini test edin:

    [ApiController]
    [Route("api/[controller]")]
    public class UsersController : ControllerBase
    {
        private readonly IUserRepository _repository;
        private readonly IEmailService _emailService;
    
        public UsersController(IUserRepository repository, IEmailService emailService)
        {
            _repository = repository;
            _emailService = emailService;
        }
    
        [HttpPost]
        public async Task<IActionResult> CreateUser([FromBody] UserDto userDto)
        {
            var user = new User
            {
                Name = userDto.Name,
                Email = userDto.Email
            };
    
            await _repository.AddAsync(user);
            await _emailService.SendWelcomeEmail(user.Email);
    
            return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
        }
    }
    

  2. Cevap:

    public class UsersControllerTests
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;
        private readonly Mock<IEmailService> _mockEmailService;
    
        public UsersControllerTests()
        {
            _mockEmailService = new Mock<IEmailService>();
    
            _server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>()
                .ConfigureTestServices(services =>
                {
                    services.AddScoped(_ => _mockEmailService.Object);
                }));
    
            _client = _server.CreateClient();
        }
    
        [Fact]
        public async Task CreateUser_ShouldCreateUserAndSendEmail()
        {
            // Arrange
            var userDto = new UserDto
            {
                Name = "Test User",
                Email = "test@example.com"
            };
    
            // Act
            var response = await _client.PostAsJsonAsync("/api/users", userDto);
    
            // Assert
            response.EnsureSuccessStatusCode();
            _mockEmailService.Verify(x => x.SendWelcomeEmail("test@example.com"), Times.Once);
        }
    }
    

  3. Aşağıdaki repository'yi test edin:

    public class UserRepository : IUserRepository
    {
        private readonly ApplicationDbContext _context;
    
        public UserRepository(ApplicationDbContext context)
        {
            _context = context;
        }
    
        public async Task<User> GetUserWithOrders(int userId)
        {
            return await _context.Users
                .Include(u => u.Orders)
                .FirstOrDefaultAsync(u => u.Id == userId);
        }
    }
    

  4. Cevap:

    public class UserRepositoryTests
    {
        private readonly DbContextOptions<ApplicationDbContext> _options;
    
        public UserRepositoryTests()
        {
            _options = new DbContextOptionsBuilder<ApplicationDbContext>()
                .UseInMemoryDatabase(databaseName: "TestDb")
                .Options;
        }
    
        [Fact]
        public async Task GetUserWithOrders_ShouldReturnUserWithOrders()
        {
            // Arrange
            using (var context = new ApplicationDbContext(_options))
            {
                var user = new User { Id = 1, Name = "Test User" };
                user.Orders.Add(new Order { Id = 1, Amount = 100 });
    
                context.Users.Add(user);
                await context.SaveChangesAsync();
            }
    
            // Act
            using (var context = new ApplicationDbContext(_options))
            {
                var repository = new UserRepository(context);
                var result = await repository.GetUserWithOrders(1);
    
                // Assert
                Assert.NotNull(result);
                Assert.Single(result.Orders);
                Assert.Equal(100, result.Orders.First().Amount);
            }
        }
    }
    

İleri Seviye Sorular

  1. Microservices mimarisinde Integration Testing nasıl yapılır?
  2. Cevap:

    • Service mesh kullanımı
    • API Gateway testleri
    • Event-driven testler
    • Contract testing
    • Chaos engineering
  3. Distributed sistemlerde Integration Testing nasıl yapılır?

  4. Cevap:

    • Message queue testleri
    • Distributed transaction testleri
    • Event sourcing testleri
    • Saga pattern testleri
    • Circuit breaker testleri
  5. CI/CD pipeline'da Integration Testing nasıl entegre edilir?

  6. Cevap:

    • Test ortamı yönetimi
    • Test verisi yönetimi
    • Paralel test çalıştırma
    • Test sonuçları raporlama
    • Test otomasyonu
  7. Performance testing ile Integration Testing nasıl birleştirilir?

  8. Cevap:

    • Load testing entegrasyonu
    • Stress testing entegrasyonu
    • Endurance testing
    • Spike testing
    • Scalability testing
  9. Security testing ile Integration Testing nasıl birleştirilir?

  10. Cevap:
    • Authentication testleri
    • Authorization testleri
    • Input validation testleri
    • SQL injection testleri
    • XSS testleri