PHPUnit Tissue Testing

Source: Internet
Author: User

Https://www.cnblogs.com/foreversun/p/6837147.html

The first is the directory structure

source folder is src/

Test folder is tests/

user.php

<?phpclass errorcode{    Const NAME_IS_NULL = 0;} Class user{public    $name;    Public function __construct ($name)    {        $this->name= $name;    }    Public Function Isempty ()    {        try{            if (empty ($this->name))            {                throw new Exception (' its null ', errorcode::name_is_null);            }        } catch (Exception $e) {            return $e->getmessage ();        }        Return ' welcome '. $this->name;    }}

The corresponding unit test file usertest.php

<?phpuse phpunit\framework\testcase;class Usertest extends testcase{    protected $user;    Public Function setUp ()    {        $this->user = new User (');    }    Public Function Testisempty ()    {        $this->user->name= ' mark ';        $result = $this->user->isempty ();        $this->assertequals (' Welcome mark ', $result);        $this->user->name= ';        $results = $this->user->isempty ();        $this->assertequals (' Its null ', $results);}    }

The second unit test code because you want to introduce the class to be tested here you can use auto-load to avoid too many files. Include

So write autoload.php in the src/folder.

<?phpfunction __autoload ($class) {    include $class. php ';} Spl_autoload_register (' __autoload ');

When you need the user class, go include User.php . __autoload()after you have finished writing spl_autoload_register() the function, use the registration.

Although it can be loaded automatically, the command to execute becomes longer.

Open the cmd command as follows

1 phpunit --bootstrap src/autoload.php tests/UserTest

So we can also write a configuration file phpunit.xml in the root directory to specify bootstrap for the project, so it doesn't have to be written in the command every time.

Phpunit.xml

<phpunit bootstrap= "src/autoload.php" ></phpunit>

And then

Open the cmd command to execute the moneytest command as follows

1 phpunit tests/UserTest

Open cmd command executes tests all the following file commands are below

1 phpunit tests

  

PHPUnit Tissue Testing

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.