Home  >  Article  >  Backend Development  >  PHP code unit testing and integration testing

PHP code unit testing and integration testing

WBOY
WBOYOriginal
2024-05-07 08:00:02796browse

PHP Unit and Integration Testing Guide Unit Testing: Focus on a single unit of code or function and use PHPUnit to create test case classes for verification. Integration testing: Focus on how multiple code units work together, and use PHPUnit's setUp() and tearDown() methods to set up and clean up the test environment. Practical case: Use PHPUnit to perform unit and integration testing in Laravel applications, including creating databases, starting servers, and writing test code.

PHP 代码单元测试与集成测试

PHP code unit testing and integration testing

Introduction

Unit testing and Integration testing is a crucial type of testing in software development, which ensures the correctness and reliability of the code at different levels. This article will guide you in using PHPUnit for unit testing and integration testing of PHP code.

Unit testing

Unit testing focuses on a single unit or function of the code. In order to create unit tests, you need to create test case classes using PHPUnit. Let's use a simple example:

<?php

class SumTest extends PHPUnit_Framework_TestCase
{
    public function testSum()
    {
        $a = 2;
        $b = 3;
        $result = $a + $b;
        $this->assertEquals($result, 5);
    }
}

In this test, the testSum() method verifies whether $a $b is equal to 5.

Integration testing

Integration testing focuses on the correctness of multiple units of code working together. For integration testing, you need to use PHPUnit's setUp() and tearDown() methods to set up and clear the test environment. Let's take a simple example:

<?php

class UserServiceTest extends PHPUnit_Framework_TestCase
{
    protected $userService;

    public function setUp()
    {
        $this->userService = new UserService();
    }

    public function testGetUser()
    {
        $user = $this->userService->getUser(1);
        $this->assertEquals($user->getName(), 'John Doe');
    }

    public function tearDown()
    {
        unset($this->userService);
    }
}

In this test, we first set up the user service in the setUp() method. We then call the getUser() method and verify that the username returned is correct. Finally, we clean up the environment in the tearDown() method.

Practical Case

The following is a practical case using PHPUnit to perform unit and integration testing in Laravel applications.

Create a test environment

# 创建一个名为 "testing" 的数据库
php artisan migrate --database=testing

# 启动 PHP 内置服务器
php artisan serve

Write unit tests

# tests/Feature/UserTest.php
namespace Tests\Feature;

use Tests\TestCase;

class UserTest extends TestCase
{
    public function testCreateUser()
    {
        $response = $this->post('/user', [
            'name' => 'John Doe',
            'email' => 'john@example.com',
            'password' => 'password',
        ]);

        $response->assertStatus(201);
    }
}

Write integration tests

# tests/Feature/UserServiceTest.php
namespace Tests\Feature;

use Tests\TestCase;

class UserServiceTest extends TestCase
{
    public function testGetUser()
    {
        $user = \App\Models\User::factory()->create();

        $response = $this->get('/user/' . $user->id);

        $response->assertStatus(200);
        $response->assertJson(['name' => $user->name]);
    }
}

Run test

# 运行单元测试
phpunit tests/Unit

# 运行集成测试
phpunit tests/Feature

The above is the detailed content of PHP code unit testing and integration testing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn