thinkphp5 配置php单元测试

环境配置

依赖扩展:

  • xdebug
  • phpunit
  • 目录结构

  • 需要配置项

  • 【启动入口文件】tests/bootstrap.php 
  • 【单元测试配置】phpunit.xml 
  • 【可选】phpunit-dev.xml 【本地不生成测试报告和全量覆盖扫描】

tests/bootstrap.php

<?php

// [ 应用入口文件 ]

// 定义应用目录

define('APP_PATH', __DIR__ . '/../application/');

// 加载框架引导文件

require __DIR__ . '/../thinkphp/base.php';

think\App::initCommon();

phpunit.xml 

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

<phpunit bootstrap="tests/bootstrap.php"

         backupGlobals="false"

         backupStaticAttributes="false"

         beStrictAboutCoversAnnotation="true"

         beStrictAboutOutputDuringTests="true"

         beStrictAboutTestsThatDoNotTestAnything="true"

         beStrictAboutTodoAnnotatedTests="true"

         forceCoversAnnotation="true"

         strict="true"

         verbose="true">

    <testsuites>

        <testsuite name="ProjectName">

            <directory suffix="Test.php">tests</directory>

        </testsuite>

    </testsuites>

    <logging>

        <log type="coverage-html" target="build/coverage"/>

        <log type="coverage-clover" target="build/logs/clover.xml"/>

        <log type="coverage-xml" target="build/logs/coverage"/>

        <log type="junit" target="build/logs/junit.xml"/>

    </logging>

    <filter>

        <whitelist processUncoveredFilesFromWhitelist="true">

            <directory suffix=".php">./application</directory>

            <exclude>

                <directory suffix=".php">./application/common/conf</directory>

            </exclude>

        </whitelist>

    </filter>

</phpunit>

phpunit-dev.xml

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

<phpunit bootstrap="tests/bootstrap.php"

         backupGlobals="false"

         backupStaticAttributes="false"

         beStrictAboutCoversAnnotation="true"

         beStrictAboutOutputDuringTests="true"

         beStrictAboutTestsThatDoNotTestAnything="true"

         beStrictAboutTodoAnnotatedTests="true"

         forceCoversAnnotation="true"

         strict="true"

         verbose="true">

    <testsuites>

        <testsuite name="ProjectName">

            <directory suffix="Test.php">tests</directory>

        </testsuite>

    </testsuites>

    <filter>

        <whitelist processUncoveredFilesFromWhitelist="false">

            <directory suffix=".php">./application</directory>

            <exclude>

                <directory suffix=".php">./application/common/conf</directory>

            </exclude>

        </whitelist>

    </filter>

</phpunit>

猜你喜欢

转载自blog.csdn.net/Baron0071/article/details/82712609