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
Open cmd command executes tests all the following file commands are below
PHPUnit Tissue Testing