win+PHPUnit单元测试

1.首先安装phpunit环境
1)先去官网(https://phpunit.de)下载适合自己php版本的phpunit,把下载的文件重命名为phpunit.phar
2)把放phpunit.phar的目录设置为环境变量,我的放在C:\phpStudy\PHPTutorial\WWW\phpunit下,如图:
win+PHPUnit单元测试
3)cmd进入到命令行,切换到C:\phpStudy\PHPTutorial\WWW\phpunit目录下,执行命令 echo @php “%~dp0phpunit.phar” %* > phpunit.cmd,如图:
win+PHPUnit单元测试
4)在执行 phpunit --version,如果出现PHPUnit 6.5.13 by Sebastian Bergmann and contributors.,则证明安装成功,如图:
win+PHPUnit单元测试
2.代码实现单元测试
1)新建文件car.php

<?php
	class car{
		public function run(){
			return 'running';
		}
	}

2)新建文件test.php

<?php
	include 'car.php';
	use PHPUnit\Framework\TestCase;
	class testHuman extends TestCase{
		public function testEat(){
			$car = new car();
			$this->assertEquals('running', $car->run());
		}
	}

3)然后去test.php所在目录下执行命令 phpunit test.php,如图
win+PHPUnit单元测试

相关文章: