Home  >  Article  >  Backend Development  >  How to use PHPUnit for testing?

How to use PHPUnit for testing?

藏色散人
藏色散人Original
2019-03-01 13:11:324059browse

PHPUnit is a unit testing framework for the PHP language. Most of the site owners want to implement PHPUnit testing because then we can simply use commands for testing. If you don’t know PHPUnit and don’t know how to test and use it, then just follow the example below to learn.

Here I will give a very simple PHPUnit example and how it works from scratch. I made a simple example using phpunit command. We need to create two files as follows:

1)MyTest.php

2)MyTestTests.php

Before starting the example, make sure you have installed PHP. So you just need to follow the file below to complete the example.

1. Download PHPUnit

wget https://phar.phpunit.de/phpunit.phar

php phpunit.phar --version

2. Create the MyTest.php file

<?php

class MyTest
{

    public function add($a, $b)
    {
        return $a + $b;
    }

}

3. Create MyTestTests .php file

<?php
require &#39;MyTest.php&#39;;

use PHPUnit\Framework\TestCase;

class MyTestTests extends TestCase
{
    private $mytest;

    protected function setUp()
    {
        $this->mytest = new MyTest();
    }

    protected function tearDown()
    {
        $this->mytest = NULL;
    }

    public function testAdd()
    {
        $result = $this->mytest->add(1, 3);
        $this->assertEquals(4, $result);
    }

}

Now we are ready to run the simple example below:

php phpunit.phar MyTestTests.php

You will find the result like this:

How to use PHPUnit for testing?

Related recommendations: "PHP Tutorial"

This article is a simple introduction to the use of PHPUnit testing. I hope it will be helpful to friends in need!

The above is the detailed content of How to use PHPUnit for 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