phpunit簡單使用

1、phpunit和單元測試php

單元測試是對單獨的代碼對象進行測試的過程,好比對函數、類、方法進行測試。單元測試能夠使用任意一段已經寫好的測試代碼,也能夠使用一些已經存在的測試框架,好比phpunit,單元測試框架提供了一系列共同、有用的功能來幫助人們編寫自動化的檢測單元,例如檢查一個實際的值是否符合咱們指望的值的斷言。單元測試框架常常會包含每一個測試的報告,以及給出你已經覆蓋到的代碼覆蓋率。html

搞單元測試的大概步驟是:編寫待測試類,編寫測試用例類,編寫測試類,測試。json

phpunit安裝bootstrap

安裝方法簡單,參考官網。框架

3、基本使用示例ide

1.編寫待測試類,假若有簡單例子:用戶登陸,花費金幣。類代碼如函數

<?php
class User
{
    private $user_array;
    public function __construct()
    {
        $this->user_array = array(
            1 => array('name' => 'tjj', 'psw' => '123456', 'rc_coins' => 100),
            2 => array('name' => 'rc', 'psw' => '789', 'rc_coins' => 10)
        );
    }
    public function login()
    {
        $ret = $this->check($_POST['name'], $_POST['pwd']);
        $_SESSION['uid'] = $ret ? $ret : null;
        return $ret;
    }
    public function check($name = '', $psw = '')
    {
        $ret = false; /*print($psw);*/
        if (!$name || !$psw) {
            return $ret;
        }
        if ($name == 'tjj' && $psw == '123456') {
            $ret = 1;
        }
        return $ret;
    }
    public function get_user_info($uid = '')
    {
        $ret = false;
        if (!$uid) {
            return $ret;
        }
        return json_encode($this->user_array[$uid]);
    }
    public function check_user_pay($user_info, $cost)
    {
        $ret = false;
        if (empty($user_info) || !$cost) {
            return $ret;
        }
        return intval($user_info['rc_coins']) - intval($cost);
    }
}

2.針對待測類編寫測試類單元測試

<?php

class UserTest extends PHPUnit_Framework_TestCase
{
    private $u;
    public function setUp()
    {
        $this->u = new User();
        $_POST['name'] = 'tjj';
        $_POST['pwd'] = '123456';
    }
    public function tearDown()
    {
        unset($this->u);
    }
    public function testLogin()
    {
        $ret = $this->u->login();
        $this->assertEquals(true, $ret !== false);// Assert
        return $ret;
    }
    /**
     * @depends testLogin
     */
    public function testGetUser($uid)//testGetUser依賴方法testLogin獲取用戶信息後回傳
    {
        $ret = $this->u->get_user_info($uid);
        $this->assertEquals(true, $ret !== false);// Assert
        return $ret;
    }
    /**
     * @depends      testGetUser
     * @dataProvider additionProvider
     */
    public function testCheckUserPay($expect, $money, $user_info) //testCheckUserPay依賴方法testGetUser獲取用戶信息,數據供給器提供測試指望值和消費值
    {
        $user_info = json_decode($user_info, true);
        $ret = $this->u->check_user_pay($user_info, $money);
        $this->assertEquals($expect, $ret);// Assert
    }
    //數據供給器
    public function additionProvider()
    {
        return array(
            't1' => array(0, 100),//t1爲名稱,0爲斷言指望值,100爲耗掉的金幣值
            't2' => array(10, 90),
            't3' => array(-10, 120),
            't4' => array(30, 70),
        );
    }
}

3.運行測試測試

phpunit --bootstrap src/autoload.php test/UserTest.phpui

假如把登陸密碼設置錯誤,testLogin斷言失敗,那麼依賴的testLogin的方法(testGetUser和testCheckUserPay)斷言將所有跳過。

4.生成代碼覆蓋率報告

phpunit --bootstrap src/autoload.php --coverage-html ./html   test/UserTest.php

相關文章
相關標籤/搜索