phpunit安裝

composer安裝php

composer require phpunit/phpunit  注意版本的依賴關係;phpunit5.3須要php 5.6以上,注意安裝位置composer

把phpunit加入到環境變量編輯器

C:\kaifa\php\vendor\bin (composer安裝完成以後在bin目錄下有批處理文件phpunit.bat)ide

在cmd下phpunit --version 查看版本狀況;測試

 

phpunit在phpstrom中的配置(File-->Settings)ui

其實配置一下自動加載就好了this

 

編寫測試文件注意須要把phpunit 類庫加載到編輯器便於提示(項目目錄下的External Libraries 右鍵 Configure PHP include path)code

 

一個phpunit文件的簡單例子get

<?php

class dependTest extends PHPUnit_Framework_TestCase
{
    public function testOne()
    {
        $this->assertEmpty('');
        return '1';
    }

    /**
     * @depends testOne
     */
    public function testTwo($a)
    {
        $this->assertEquals(1, $a);
        return '2';
    }

    /**
     * 測試多個依賴
     * @depends testOne
     * @depends testTwo
     */
    public function testMoreDepend()
    {
        $this->assertEquals(
            array(1,2),
            func_get_args()
        );
    }

    /**
     * 測試異常
     */
    public function testException()
    {
        $this->expectException(InvalidArgumentException::class);
        throw new InvalidArgumentException("xx");
    }

    /**
     * 用標註來表示
     * @expectedException InvalidArgumentException
     */
    public function testException2()
    {
        throw new InvalidArgumentException();
    }

    /**
     * 測試錯誤
     * @expectedException PHPUnit_Framework_Error
     */
    public function testFailingInclude()
    {
        include 'xx.php';
    }

    /**
     * 測試輸出
     */
    public function testPrint()
    {
        $this->expectOutputString('foo');
        print 'foo';
    }

    /**
     * 比較不一樣;查看錯誤報告
     */
    public function testEquals()
    {
        $this->assertEquals(array(1,2,3), array('1',2,33));
    }

    /**
     * 數據提供者
     * @dataProvider addProvider
     * @param $a
     * @param $b
     * @param $expected
     */
    public function testDataProvider($a, $b, $expected)
    {
        $this->assertEquals($expected, $a + $b);
    }

    public function addProvider()
    {
        return array(
            'test1'=>array(1,2, 3),
            'test2'=>array(2,2, 4),
            'test3'=>array(3,2, 5),
        );
    }
}

在該文件點擊右鍵Run 'xxx'cmd

相關文章
相關標籤/搜索