正在这篇文章中,咱们应用 composer 的依赖担保理对象进行phpunit包装置以及治理,composer 民间地点 https://getcomposer.org/,依照提醒进行全局装置便可,另外,咱们也会应用一个十分好用的Monolog记载日记组件记载日记,不便咱们查看。
正在根目次下建设 coomposer.json 的设置装备摆设文件,输出如下内容:
{ "autoload": { "classmap": [ "./" ] } }
下面的意义是将根目次下的一切的类文件都加载出去, 正在饬令行执行 composer install 后,正在根目次会天生出一个vendor的文件夹,咱们当前经过 composer 装置的任何第三方代码城市被天生正在这里。
2、为何要单位测试?
只需你想到输出一些货色到print语句或调试表白式中,就用测试替代它。 --Martin Fowler
PHPUnit 是一个用PHP编程言语开发的开源软件,是一个单位测试框架。PHPUnit由Sebastian Bergmann创立,源于Kent Beck的SUnit,是xUnit家族的框架之一。
单位测试是对独自的代码工具进行测试的进程,比方对函数、类、办法进行测试。单位测试能够应用恣意一段曾经写好的测试代码,也能够应用一些曾经存正在的测试框架,比方JUnit、PHPUnit或许Cantata++,单位测试框架提供了一系列独特、有用的性能来协助人们编写主动化的检测单位,例如反省一个实际的值能否合乎咱们希冀的值的断言。单位测试框架常常会蕴含每一个测试的陈诉,和给出你曾经笼罩到的代码笼罩率。
总之一句话,应用 phpunit 进行主动测试,会使你的代码更强壮,缩小前期保护的老本,也是一种比拟规范的标准,现现在盛行的PHP框架都带了单位测试,如Laraval,Symfony,Yii2等,单位测试曾经成为了标配。
另外,单位测试用例是经过饬令操控测试剧本的,而没有是经过阅读器拜访URL的。
3、装置PHPUnit
应用 composer 形式装置 PHPUnit,其余装置形式请看这里
composer require --dev phpunit/phpunit ^6.2
装置 Monolog 日记包,做 phpunit 测试记载日记用。
composer require monolog/monolog
装置好之后,咱们能够看coomposer.json 文件曾经有这两个扩大包了:
"require": { "monolog/monolog": "^1.23", }, "require-dev": { "phpunit/phpunit": "^6.2" },
4、PHPUnit简略用法
一、单个文件测试
创立目次tests,新建文件 StackTest.php,编纂以下:
<?php /** * 一、composer 装置Monolog日记扩大,装置phpunit单位测试扩大包 * 二、引入autoload.php文件 * 三、测试案例 * * */ namespace App\tests; require_once __DIR__ . '/../vendor/autoload.php'; define("ROOT_PATH", dirname(__DIR__) . "/"); use Monolog\Logger; use Monolog\Handler\StreamHandler; use PHPUnit\Framework\TestCase; class StackTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); // 增加日记文件,假如不装置monolog,则无关monolog的代码均可以正文掉 $this->Log()->error('hello', $stack); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); } public function Log() { // create a log channel $log = new Logger('Tester'); $log->pushHandler(new StreamHandler(ROOT_PATH . 'storage/logs/app.log', Logger::WARNING)); $log->error("Error"); return $log; } }
代码诠释:
StackTest为测试类
StackTest 承继于 PHPUnit\Framework\TestCase
测试办法testPushAndPop(),测试办法必需为public权限,普通以test扫尾,或许你也能够抉择给其加正文@test来表
正在测试办法内,相似于 assertEquals() 这样的断言办法用来对实际值与预期值的婚配做出断言。
饬令行执行:
phpunit 饬令 测试文件定名
➜ framework# ./vendor/bin/phpunit tests/StackTest.php // 或许能够省略文件后缀名 // ./vendor/bin/phpunit tests/StackTest
执行后果:
➜ framework# ./vendor/bin/phpunit tests/StackTest.php PHPUnit 6.4.1 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 56 ms, Memory: 4.00MB OK (1 test, 5 assertions)
咱们能够正在app.log文件中查看咱们打印的日记信息。
二、类文件引入
Calculator.php
<?php class Calculator { public function sum($a, $b) { return $a + $b; } } ?>
单位测试类:
CalculatorTest.php
<?php namespace App\tests; require_once __DIR__ . '/../vendor/autoload.php'; require "Calculator.php"; use PHPUnit\Framework\TestCase; class CalculatorTest extends TestCase { public function testSum() { $obj = new Calculator; $this->assertEquals(0, $obj->sum(0, 0)); } }
饬令执行:
> ./vendor/bin/phpunit tests/CalculatorTest
执行后果:
PHPUnit 6.4.1 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 117 ms, Memory: 4.00MB There was 1 failure:
假如咱们把这里的断言成心写错,$this->assertEquals(1, $obj->sum(0, 0));
看执行后果:
PHPUnit 6.4.1 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 117 ms, Memory: 4.00MB There was 1 failure: 1) App\tests\CalculatorTest::testSum Failed asserting that 0 matches expected 1. /Applications/XAMPP/xamppfiles/htdocs/web/framework/tests/CalculatorTest.php:22 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
会间接报出办法谬误信息及行号,有助于咱们疾速找出bug
三、初级用法
你能否曾经腻烦了正在每个测试办法定名后面加一个test,能否由于只是挪用的参数没有同,却要写多个测试用例而纠结?我最喜爱的初级性能,如今盛大保举给你,叫做框架天生器。
Calculator.php
<?php class Calculator { public function sum($a, $b) { return $a + $b; } } ?>
饬令行启动测试用例,应用要害字 --skeleton
> ./vendor/bin/phpunit --skeleton Calculator.php
执行后果:
PHPUnit 6.4.1 by Sebastian Bergmann and contributors. Wrote test class skeleton for Calculator to CalculatorTest.php.
是否是很简略,由于不测试数据,以是这里加测试数据,而后从新执行上边的饬令
<?php class Calculator { /** * @assert (0, 0) == 0 * @assert (0, 1) == 1 * @assert (1, 0) == 1 * @assert (1, 1) == 2 */ public function sum($a, $b) { return $a + $b; } } ?>
原始类中的每一个办法都进行@assert注解的检测。这些被转变成测试代码,像这样
/** * Generated from @assert (0, 0) == 0. */ public function testSum() { $obj = new Calculator; $this->assertEquals(0, $obj->sum(0, 0)); }
执行后果:
./vendor/bin/phpunit tests/CalculatorTest PHPUnit 6.4.1 by Sebastian Bergmann and contributors. .... Time: 0 seconds OK (4 tests)
保举教程:《PHP教程》
以上就是PHP测试框架PHPUnit根本应用的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学 phpunit monolog
抱歉,评论功能暂时关闭!