PHPUnit Unit Test

Source: Internet
Author: User

Installing PHPUnit on a 1.linux server

wget Https://phar.phpunit.de/phpunit.phar
chmod +x Phpunit.phar
sudo mv Phpunit.phar/usr/local/bin/phpunit

Establish phpunit Short command
PHPUnit--version

[Email protected] phpunit_test]# phpunit--versionphpunit 5.6.1 by Sebastian Bergmann and contributors.

  

2. Create a unit test file

File name is called unittest.php

We can call the function module inside the method of the unit test file, use the data simulation to see if it is running normally, if the general rule will error, break off

<?php   class  UnitTest extends phpunit_framework_testcase{public        function Testpushandpop () {            $ stack = array ();            $this->assertequals (0,count ($stack));            Array_push ($stack, ' foo ');            Asserts whether the value after inserting data into the $stack array is equal to 1            $this->assertequals (1,count ($stack));        }         /**         * Defines the test label declaration that the method is a testing method         * @test         ***/public        function Indexequals () {            $stack = array (1,2,3,4 );            Assert $stack[0] equals 2            $this->assertequals (2, $stack [0]);        }      }? >

3.phpunit Running Files

[Email protected] phpunit_test]# phpunit unittest.php phpunit 5.6.1 by Sebastian Bergmann and contributors. F                                                                  2/2 (100%) time:82 MS, Memory:6.75mbthere is 1 failure:1) unittest::indexequalsfailed asserting that 1 matches E Xpected 2./wwwroot/phpunit_test/unittest.php:18failures! Tests:2, Assertions:3, failures:1.

The results show that the test PHP file runs two modules with a module error

The error test method named Indexequals 18 lines.

Because the stack equals 0 not equal to 1 of the assertion, the error is correct, and the location errors succeed.

PHPUnit Unit Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.