Backend testing: Difference between revisions
m Maintenance script moved page UserDocumentation:Knowledge Central/KC IT/IT backend team/IT backend policies/IT backend architecture/backend testing to Backend testing: Cleaned title and categorized |
Added category IT backend architecture |
||
| Line 176: | Line 176: | ||
<!-- From bodyContent 1543113.mraw --> | <!-- From bodyContent 1543113.mraw --> | ||
[[Category:IT backend architecture]] | |||
Revision as of 18:51, 19 October 2025
Unitary
Unit Testing is a level of software testing where individual units or components of a software are tested. The goal is to validate that each unit functions as intended in isolation from the rest of the code. It helps to catch and fix bugs early in the development cycle, reducing the cost and effort of fixing them later.
Purpose of Unit Testing
- Ensure that individual units or components work correctly in isolation.
- Provide a solid foundation, ensuring code changes don't introduce regressions.
- Improve software design by forcing developers to consider smaller components, thereby making them write more modular and maintainable code.
- Simplify the debugging process. When a test fails, only the latest changes need to be debugged.
- Serve as documentation since unit tests demonstrate how a piece of functionality is supposed to work.
PHP Example using PHPUnit
Let's say we have a simple class Calculator that provides a method to add two numbers:
// Calculator.php
class Calculator {
public function add($a, $b) {
return $a + $b;
}
}A unit test for the add method could look like this:
// CalculatorTest.php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase {
public function testAdd() {
$calculator = new Calculator();
$result = $calculator->add(5, 3);
$this->assertEquals(8, $result); // Assert that the result of 5 + 3 is 8
}
}To run this test, you'd typically use the phpunit command-line tool, pointing it to the CalculatorTest.php file.
When executed, PHPUnit would instantiate the Calculator class, run the add method with the inputs 5 and 3, and then assert that the output matches the expected value of 8. If the method fails to provide the correct output, the test would fail, alerting the developer to the issue.
Component
Component Testing focuses on verifying individual components or units of a software in isolation. The main purpose is to ensure that each component functions as intended, independent of other units.
Purpose of Component Testing
- Verify Functionality: Ensure that a specific component of the application works as intended.
- Find Issues Early: Detect problems at an early stage of development, making them cheaper and easier to fix.
- Support Refactoring: Safely make changes to the code, knowing that any regressions will be caught.
- Documentation: Test cases serve as documentation, indicating how a component is supposed to behave.
- Continuous Integration: Automated component tests are crucial for CI/CD pipelines, ensuring code quality throughout development.
Example
Let's say we have a simple PHP class that represents a calculator with an addition method:
// Calculator.php
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
}Using PHPUnit, a popular testing framework in PHP, we can create a component test for the add method:
// tests/CalculatorTest.php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
public function testAddition()
{
$calculator = new Calculator();
$result = $calculator->add(3, 4);
$this->assertEquals(7, $result);
}
}In the above test, we're verifying that the add method in the Calculator class correctly adds two numbers. If the method doesn't return 7 when adding 3 and 4, the test will fail.
Component tests like this would be written for every significant function or method in an application. Over time, these tests create a safety net, ensuring each piece of the application behaves as expected, even as changes are made or the application grows.
Integration
They test the infrastructure part, such databases, external APIs or Visual tests (with AWS Rekognition). Due to their medium time cost, they are not recommended to be launched during development phase. They should test every possible failure point of that services.
Purpose of Integration Testing
- Verify Interactions: Ensure that integrated components communicate and operate as expected when combined.
- Data Transfer Checks: Ensure data is correctly passed between different modules or systems.
- Functional Checks: Verify that functions that rely on multiple components work correctly.
- Error Handling: Check that the system handles errors gracefully during integration.
- Performance: Ensure that the integration doesn't introduce performance bottlenecks.
Example
Let's say you're developing an e-commerce application with a Cart class and a Product class. You want to test the integration between these two.
class Cart {
private $products = [];
public function addProduct(Product $product) {
$this->products[] = $product;
}
public function getTotalPrice() {
$total = 0;
foreach ($this->products as $product) {
$total += $product->getPrice();
}
return $total;
}
}
class Product {
private $name;
private $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
public function getPrice() {
return $this->price;
}
}Integration Test:
use PHPUnit\Framework\TestCase;
class CartIntegrationTest extends TestCase {
public function testTotalPriceWithMultipleProducts() {
$product1 = new Product("Shirt", 20);
$product2 = new Product("Hat", 10);
$cart = new Cart();
$cart->addProduct($product1);
$cart->addProduct($product2);
$this->assertEquals(30, $cart->getTotalPrice());
}
}In this test, we're not just testing the individual functions of Cart or Product, but we're testing their integration, i.e., adding products to the cart and then checking if the cart calculates the total price correctly.
Other coding test types
Contract
Validates the interaction between microservices. Consumer microservice launches a serial of requests to the provider microservice and generate contracts from that results. Provider microservice must load that contracts, execute its tests and check that the same contract is obtained.
Mutation
Its a tool that checks that the result of an execution is still valid after applying a serial of logic changes to the code (such == per =)
Property
Analyze results properties, such data types.