Modul 11 von 16 · 📖 5 min Lesezeit · ⏱ 30 min gesamt
FI-AE 11 Testing — Unit, Integration, E2E (EN)
Inhaltsverzeichnis (6 Abschnitte)
FI-AE 11 Testing — Unit, Integration, E2E
Software quality begins with targeted testing. This module introduces you to the methods of modern software testing. You will learn to understand the test pyramid, effectively design unit tests, integration tests, and end-to-end tests, and apply mocking techniques for isolated test environments. The focus is on practical implementation with a TDD approach and meaningful code coverage measurement in CI pipelines.
Concepts and Background
- Test Pyramid
- A model for distributing test types that provides a large base of unit tests, a middle layer of integration tests, and a peak of end-to-end tests. The width of the base represents the high number of fast, isolated tests while the peak represents few, but comprehensive system tests.
- Mocking
- Technique for replacing dependencies with controlled doubles that provide predefined responses. Allows isolated testing of components that depend on external resources like databases or APIs without actually having to call them.
- TDD (Test-Driven Development)
- Development approach where first a test is written for a function that does not yet exist. Only when this test fails is the minimal code implemented that allows the test to pass successfully. The cycle repeats with refactoring.
- Code Coverage
- Metric for measuring what percentage of the source code is executed by tests. Important metrics are line, branch, and path coverage. High coverage does not automatically guarantee quality, but it uncovers untested code areas.
- CI Integration
- Incorporation of tests into Continuous Integration pipelines to perform automated quality checks on every code commit. Ensures that tests are run regularly and regressions are detected early.
Architecture Diagram
flowchart TD
A[Code] --> B[Unit Tests]
A --> C[Integration Tests]
A --> D[E2E Tests]
B --> E[Test Runner]
C --> E
D --> E
E --> F[CI Pipeline]
F --> G[Test Report]
F --> H[Code Coverage]
F --> I[Build Status]
Practical Steps
- Install and configure a unit testing framework for your language. This forms the basis for all further testing activities.
- Create a simple unit test for a basic function with a positive and a negative test case. This ensures that your test setup is working correctly.
- Implement mocks for external dependencies using a mocking framework like Mockito or sinon.js. Allows you to run tests in isolation and quickly.
- Write integration tests that verify the interaction between multiple components. These tests uncover errors not visible at the unit level.
- Integrate tests into your build process with a command like
ornpm test
. Automated tests are essential for CI/CD.mvn test - Configure code coverage reports with tools like JaCoCo or Istanbul. This gives you insights into which parts of the code are covered by tests.
- Implement a TDD cycle: Red (write test, fail), Green (write minimal code), Refactoring. This improves code quality and design.
- Create end-to-end tests with tools like Cypress or Selenium that simulate user interactions. These tests verify the entire system from a user perspective.
- Integrate tests into your CI pipeline with a command like
. This ensures that tests are run automatically on every commit.git commit -m "Add feature" && git push origin main - Regularly analyze test reports and coverage metrics to identify and fix weaknesses in test design.
Common Pitfalls
Further Resources
- Martin Fowler - Practical Test Pyramid
- JUnit 5 User Guide
- Cypress E2E Testing Documentation
- Martin Fowler - Test-Driven Development
- SonarQube - Code Coverage
Knowledge Check
Four questions for self-assessment. Click on each question to see the correct answer and explanation.
Which principle correctly describes the test pyramid?
- A) The higher up in the pyramid, the more tests should be performed.
- B) The base of the pyramid consists of many fast unit tests, while the peak contains few slow E2E tests.
- C) All tests should be evenly distributed across all levels of the pyramid.
- D) The test pyramid states that integration tests are more important than unit tests.
Correct Answer: B. The test pyramid models that many fast, isolated unit tests form the base, while few comprehensive E2E tests form the peak. Option A is incorrect because more tests are needed in the base. Option C ignores the different speeds and scopes of the test types. Option D is incorrect because both test types are important, but serve different purposes.
What is the main advantage of mocking in software testing?
- A) It speeds up test execution by replacing real dependencies.
- B) It allows testing of components that depend on external resources without actually having to call them.
- C) It automatically increases code coverage.
- D) It completely eliminates the need for integration tests.
Correct Answer: B. Mocking allows isolated testing of components that depend on external resources without actually having to call them. Option A is partially correct, but not the main advantage. Option C is incorrect as mocking does not automatically increase coverage. Option D is incorrect as mocking does not eliminate the need for integration tests.