MAC PHP 安装PHPUNIT 单元测试,并在phpstorm中支持

1.简介

PHPUnit 是 PHP 程式语言中最常见的单元测试 (unit testing) 框架,PHPUnit 是参考 xUnit 架构利用 PHP
为什么要使用 PHPUnit 来测试呢?虽然,要做单元测试可以自己写程式来测试, 但是 PHPUnit 提供了一些测试时常用的 library 及解决测试时会遇到问题的方法,所以我们会使用 PHPUnit 来做单元测试。

3.安装过程

composer 全局安装

composer global require phpunit/phpunit

创建项目目录

mkdir test

初始化composer 仓库

composer init

在你的项目目录下引入

composer require --dev phpunit/phpunit

在项目目录中新建tests文件夹单元测试文件

mkdir tests

新建phpunit.xml为了引入autoload.php

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

在tests目录中新建EqualsTest.php

<?php

use PHPUnit\Framework\TestCase;

class EqualsTest extends TestCase {
    public function testFailure()
    {
        $this->assertEquals(1, 0);
    }

    public function testFailure2()
    {
        $this->assertEquals('bar', 'baz');
    }

    public function testFailure3()
    {
        $this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");
    }
}

目录结构

执行

10306662-06269f97f3f72082.png
image.png

4.配置phpstorm

10306662-45e33406a304bfeb.png
image.png

5.相关资源

手册:http://www.phpunit.cn/manual/7.0/zh_cn/index.html

https://www.yiichina.com/tutorial/1042

http://www.php.cn/php-weizijiaocheng-380671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值