Introduction to SoftwareTesting
• Software testing ensures software quality
• Detecting bugs and verifying functionality.
• Validating that software meets requirements and behaves as expected.
• JUnit is a popular testing framework for Java applications.
3.
Types of SoftwareTesting
• Manual Testing - Performed by humans without automation.
• Automated Testing - Uses software tools to run tests automatically.
• Unit Testing - Tests individual components of code.
• Integration Testing - Verifies interactions between components.
• System Testing - Tests all system’s functionality.
• User Acceptance Testing - Validates software requirements from user
perspective.
4.
Junit – Testingframrwork for Java
• Provides a structured way to write and execute tests.
• Supports test automation.
• Integrates well with build tools (e.g., Maven, Gradle).
• Helps ensure code reliability and maintainability.
Writing a BasicJUnit Test
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAddition() {
assertEquals(5, 2 + 3);
}
}
7.
JUnit Annotations
• @Test- Marks a test method.
• @BeforeEach - Runs before each test.
• @AfterEach - Runs after each test.
• @BeforeAll - Runs once before all tests.
• @AfterAll - Runs once after all tests.
8.
Setup and TeardownMethods
import org.junit.jupiter.api.*;
class TestLifecycle {
@BeforeEach
void setUp() {
System.out.println("Setting up test");
}
@AfterEach
void tearDown() {
System.out.println("Cleaning up test");
}
@Test
void exampleTest() {
assertTrue(true);
}
}
9.
Testing Exceptions
import staticorg.junit.jupiter.api.Assertions.*;
class ExceptionTest {
@Test
void testException() {
assertThrows(ArithmeticException.class, () -> {
int result = 10 / 0;
});
}
}
Test Coverage Metrics
•Statement Coverage - Tests if each line is executed.
• Branch Coverage - Ensures all branches in control structures execute.
• Path Coverage - Tests all possible paths in a function.