Backend testing: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 18: Line 18:
Let's say we have a simple class Calculator that provides a method to add two numbers:
Let's say we have a simple class Calculator that provides a method to add two numbers:


<syntaxhighlight>// Calculator.php
<syntaxhighlight lang="php">// Calculator.php
class Calculator {
class Calculator {
     public function add($a, $b) {
     public function add($a, $b) {
Line 26: Line 26:
A unit test for the add method could look like this:
A unit test for the add method could look like this:


<syntaxhighlight>// CalculatorTest.php
<syntaxhighlight lang="php">// CalculatorTest.php


use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestCase;
Line 62: Line 62:
Let's say we have a simple PHP class that represents a calculator with an addition method:
Let's say we have a simple PHP class that represents a calculator with an addition method:


<syntaxhighlight>// Calculator.php
<syntaxhighlight lang="php">// Calculator.php
class Calculator
class Calculator
{
{
Line 72: Line 72:
Using PHPUnit, a popular testing framework in PHP, we can create a component test for the add method:
Using PHPUnit, a popular testing framework in PHP, we can create a component test for the add method:


<syntaxhighlight>// tests/CalculatorTest.php
<syntaxhighlight lang="php">// tests/CalculatorTest.php


use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestCase;
Line 110: Line 110:
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.
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.


<syntaxhighlight>class Cart {
<syntaxhighlight lang="php">class Cart {
     private $products = [];
     private $products = [];


Line 141: Line 141:
Integration Test:
Integration Test:


<syntaxhighlight>use PHPUnit\Framework\TestCase;
<syntaxhighlight lang="php">use PHPUnit\Framework\TestCase;


class CartIntegrationTest extends TestCase {
class CartIntegrationTest extends TestCase {
Line 174: Line 174:


Analyze results properties, such data types.
Analyze results properties, such data types.
<!-- From bodyContent 1543113.mraw -->
[[Category:IT backend architecture]]
 

Latest revision as of 08:29, 20 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-&gt;add(5, 3);

        $this-&gt;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-&gt;add(3, 4);

        $this-&gt;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-&gt;products[] = $product;
    }

    public function getTotalPrice() {
        $total = 0;
        foreach ($this-&gt;products as $product) {
            $total += $product-&gt;getPrice();
        }
        return $total;
    }
}

class Product {
    private $name;
    private $price;

    public function __construct($name, $price) {
        $this-&gt;name = $name;
        $this-&gt;price = $price;
    }

    public function getPrice() {
        return $this-&gt;price;
    }
}

Integration Test:

use PHPUnit\Framework\TestCase;

class CartIntegrationTest extends TestCase {
    public function testTotalPriceWithMultipleProducts() {
        $product1 = new Product(&quot;Shirt&quot;, 20);
        $product2 = new Product(&quot;Hat&quot;, 10);

        $cart = new Cart();
        $cart-&gt;addProduct($product1);
        $cart-&gt;addProduct($product2);

        $this-&gt;assertEquals(30, $cart-&gt;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.