PHPUnit實踐二(生命週期)

本系列教程全部的PHPUnit測試基於PHPUnit6.5.9版本,Lumen 5.5框架php

PHPUnit測試一個文件類的生命週期

圖片描述

理解PHPUnit加載機制(Lumen版)

  1. PHPUnit自動測試文件會自動加載引入(include file)
  2. PHPUnit去啓動setUp方法,Lumen裏重寫了setUp,加載了bootstrap/app.php
  3. app.php加載了composer的autoload,藉此你項目全部自動加載環境都有了,不過不包含tests目錄

至此咱們引入了咱們須要構建本身的自動加載類html

增長tests的自動加載

咱們須要給tests下的測試用例建立相似下面的結構

├── BaseCase.php 重寫過Lumen基類的測試基類,用於咱們用這個基類作測試基類,後續會說明
├── bootstrap.php tests自動加載文件
├── Cases 測試用例目錄
│   └── Demo 測試模塊
│       ├── logs 日誌輸出目錄
│       ├── PipeTest.php PHPUnit流程測試用例
│       ├── phpunit.xml phpunit配置文件xml
│       └── README.md 本模塊測試用例說明
├── ExampleTest.php 最原始測試demo
└── TestCase.php Lumen自帶的測試基類

tests自動加載文件代碼

<?php
/**
 * 測試框架的自動加載測試文件類
 * User: qikailin
 */
error_reporting(E_ALL ^ E_NOTICE);
require __DIR__ . '/../vendor/autoload.php';

define('MY_TESTS_DIR_BASE', realpath(dirname(__FILE__)));

set_include_path(implode(PATH_SEPARATOR, array(
    WPT_TEST_DIR_BASE,
    get_include_path()
)));

spl_autoload_register(function ($class) {

    $classFile = MY_TESTS_DIR_BASE . DIRECTORY_SEPARATOR . str_replace(["Test\\", "/", "\\"],
            ["", DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $class) . ".php";
    
    if (file_exists($classFile)) {
        include_once $classFile;
    }

}, true, false);

phpunit.xml

自動加載配置bootstrap文件bootstrap

<?xml version="1.0" encoding="UTF-8"?>

<phpunit
        bootstrap="../../bootstrap.php"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="false"
        convertWarningsToExceptions="false"
        colors="true">
</phpunit>

流程測試代碼

TestCase.php

# 代碼頭部添加 命令空間Test
namespace Test;

PipeTest 流程代碼

<?php
/**
 * 測試類的每一個測試方法都會運行一次 setUp() 和 tearDown() 模板方法(同時,每一個測試方法都是在一個全新的測試類實例上運行的)。
 * 另外,setUpBeforeClass() 與 tearDownAfterClass() 模板方法將分別在測試用例類的第一個測試運行以前和測試用例類的最後一個測試運行以後調用。
 * 若是有須要共享的對象或變量,能夠放在setUpBeforeClass,並設置爲靜態屬性
 * User: qikailin
 */
namespace Test\Cases\Demo;

use Test\BaseCase;

class PipeTest extends BaseCase
{
    public static function setUpBeforeClass()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public function setUp()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    /**
     * 測試方法的前置執行,setUp以後
     */
    protected function assertPreConditions()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public function testOne()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(true);
    }

    public function testTwo()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        // 兩個交換下順序能夠看下效果
        // 正常執行成功assert能夠繼續執行,失敗的會跳出方法
        $this->assertArrayHasKey('d', ['d'=>1, 'e'=>2]);
        $this->assertTrue(false);
    }

    public function testThree()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(false);
    }

    public function testFour()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    /**
     * 測試方法成功後的後置執行,tearDown以前
     */
    protected function assertPostConditions()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public function tearDown()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public static function tearDownAfterClass()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    /**
     * 不成功後攔截方法
     * 必須從新拋出錯誤,若是不拋出錯誤,斷言會當成成功了
     */
    public function onNotSuccessfulTest(\Throwable $e)
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        // 必須從新拋出錯誤,若是不拋出錯誤,斷言會當成成功了
        throw $e;
    }
}

運行

# 你能夠把vendor/bin加入到環境變量PATH
cd tests/Demo
../../../vendor/bin/phpunit

運行輸出

PHPUnit 6.5.9 by Sebastian Bergmann and contributors.

Test\Cases\Demo\PipeTest::setUpBeforeClass
Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testOne
Test\Cases\Demo\PipeTest::assertPostConditions
Test\Cases\Demo\PipeTest::tearDown
.Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testTwo
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::onNotSuccessfulTest
FTest\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testThree
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::onNotSuccessfulTest
FTest\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testFour
Test\Cases\Demo\PipeTest::assertPostConditions
Test\Cases\Demo\PipeTest::tearDown
R                                                                4 / 4 (100%)Test\Cases\Demo\PipeTest::tearDownAfterClass


Time: 1.29 seconds, Memory: 6.00MB

There were 2 failures:

1) Test\Cases\Demo\PipeTest::testTwo
Failed asserting that false is true.

/xxx/tests/Cases/Demo/PipeTest.php:47

2) Test\Cases\Demo\PipeTest::testThree
Failed asserting that false is true.

/xxx/tests/Cases/Demo/PipeTest.php:53

--

There was 1 risky test:

1) Test\Cases\Demo\PipeTest::testFour
This test did not perform any assertions

FAILURES!
Tests: 4, Assertions: 4, Failures: 2, Risky: 1.

Generating code coverage report in HTML format ... done

整理流程輸出

Test\Cases\Demo\PipeTest::setUpBeforeClass
Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testOne
Test\Cases\Demo\PipeTest::assertPostConditions
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testTwo
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::onNotSuccessfulTest
Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testThree
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::onNotSuccessfulTest
Test\Cases\Demo\PipeTest::setUp
Test\Cases\Demo\PipeTest::assertPreConditions
Test\Cases\Demo\PipeTest::testFour
Test\Cases\Demo\PipeTest::assertPostConditions
Test\Cases\Demo\PipeTest::tearDown
Test\Cases\Demo\PipeTest::tearDownAfterClass

總結

一個測試類文件,從setUpBeforeClass加載,且僅此加載一次
每一個測試方法都會走的過程:setUp->assertPreConditions->測試方法->[assert成功執行:assertPostConditions]->tearDown->[assert執行失敗:onNotSuccessfulTest,且本方法須要拋出錯誤]
本個測試類文件執行tearDownAfterClass結束

參考

PHPUnit 6.5 官方文檔app

相關文章
相關標籤/搜索