欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP使用phpunit進(jìn)行單元測試示例

 更新時間:2019年09月23日 08:45:17   作者:巴八靈  
這篇文章主要介紹了PHP使用phpunit進(jìn)行單元測試,結(jié)合實(shí)例形式分析了phpunit的安裝及單元測試相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP使用phpunit進(jìn)行單元測試。分享給大家供大家參考,具體如下:

1. linux服務(wù)器上安裝phpunit

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

建立phpunit短命令

phpunit --version

[root@dongzi phpunit_test]# phpunit --version
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

2. 創(chuàng)建單元測試文件

文件名稱為UnitTest.php

我們可以在單元測試文件內(nèi)的方法里面調(diào)用功能模塊,用數(shù)據(jù)模擬看是否運(yùn)行正常,如果通則會報錯,斷掉

<?php
  class UnitTest extends PHPUnit_Framework_TestCase{
    public function testPushAndPop(){
      $stack = array();
      $this->assertEquals(0,count($stack));
      array_push($stack,'foo');
      //斷言插入數(shù)據(jù)到$stack數(shù)組后值是否等于1
      $this->assertEquals(1,count($stack));
    }
    /**
     *定義test標(biāo)簽聲明該方法是測試方法
     *@test
     ***/
    public function indexEquals(){
      $stack = array(1,2,3,4);
      //斷言$stack[0]等于2
      $this->assertEquals(2,$stack[0]);
    }
  }
?>

3. phpunit運(yùn)行文件

[root@dongzi phpunit_test]# phpunit UnitTest.php
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.
.F                                 2 / 2 (100%)
Time: 82 ms, Memory: 6.75MB
There was 1 failure:
1) UnitTest::indexEquals
Failed asserting that 1 matches expected 2.
/wwwroot/phpunit_test/UnitTest.php:18
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.

結(jié)果顯示測試php文件中共運(yùn)行兩個模塊,有一個模塊錯誤

錯誤測試方法名為indexEquals報錯行為18行。

因?yàn)橐驗(yàn)閟tack等于0不等于斷言的1,所以報錯,定位錯誤成功。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《php優(yōu)秀開發(fā)框架總結(jié)

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論