PHP单元测试

首先 本地环境安装phpunit

mac上的检查有没有安装phpunit

phpunit --version

安装phpunit

brew install phpunit

这一步在国内网络环境可能卡住,需要自备梯子

安装完后,再查看

wenqidongdeMBP:~ wenqidong$ phpunit --version
PHPUnit 8.4.1 by Sebastian Bergmann and contributors.

This version of PHPUnit is supported on PHP 7.2, PHP 7.3, and PHP 7.4.
You are using PHP 7.1.32 (/usr/local/Cellar/php@7.1/7.1.32_1/bin/php).
wenqidongdeMBP:~ wenqidong$ 

第二步 使用composer全局安装phpunit

wenqidongdeMBP:hitpoints-open-api wenqidong$ composer global require phpunit/phpunit
Changed current directory to /Users/wenqidong/.composer
Using version ^7.5 for phpunit/phpunit
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 28 installs, 0 updates, 0 removals
  - Installing sebastian/version (2.0.1): Loading from cache
  - Installing sebastian/resource-operations (2.0.1): Downloading (100%)         
  - Installing sebastian/recursion-context (3.0.0): Loading from cache
  - Installing sebastian/object-reflector (1.1.1): Loading from cache
  - Installing sebastian/object-enumerator (3.0.3): Loading from cache
  - Installing sebastian/global-state (2.0.0): Loading from cache
  - Installing sebastian/exporter (3.1.2): Loading from cache
  - Installing sebastian/environment (4.2.2): Downloading (100%)         
  - Installing sebastian/diff (3.0.2): Downloading (100%)         
  - Installing sebastian/comparator (3.0.2): Downloading (100%)         
  - Installing phpunit/php-timer (2.1.2): Downloading (100%)         
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing phpunit/php-file-iterator (2.0.2): Downloading (100%)         
  - Installing theseer/tokenizer (1.1.3): Loading from cache
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
  - Installing phpunit/php-token-stream (3.1.1): Downloading (100%)         
  - Installing phpunit/php-code-coverage (6.1.4): Downloading (100%)         
  - Installing doctrine/instantiator (1.2.0): Loading from cache
  - Installing symfony/polyfill-ctype (v1.12.0): Loading from cache
  - Installing webmozart/assert (1.5.0): Loading from cache
  - Installing phpdocumentor/reflection-common (2.0.0): Loading from cache
  - Installing phpdocumentor/type-resolver (1.0.1): Loading from cache
  - Installing phpdocumentor/reflection-docblock (4.3.2): Loading from cache
  - Installing phpspec/prophecy (1.9.0): Loading from cache
  - Installing phar-io/version (2.0.1): Downloading (100%)         
  - Installing phar-io/manifest (1.0.3): Downloading (100%)         
  - Installing myclabs/deep-copy (1.9.3): Loading from cache
  - Installing phpunit/phpunit (7.5.17): Downloading (100%)         
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
Writing lock file
Generating autoload files

wenqidongdeMBP:hitpoints-open-api wenqidong$ 

第三步 在项目根目录创建一个配置文件phpunit.xml

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="service">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

第四步 在IDE中配置PHPUnit

第五步 配置单元测试类提示

第六步 编写单测试