zoukankan      html  css  js  c++  java
  • PHP单元测试PHPUnit

    配置说明

    1.全局安装phpunit命令脚本

    1
    2
    3
    4
    5
    $ wget https://phar.phpunit.de/phpunit-7.0.phar
    chmod +x phpunit-7.0.phar
    $ sudo mv phpunit-7.0.phar /usr/local/bin/phpunit
    $ phpunit --version
    PHPUnit x.y.z by Sebastian Bergmann and contributors.

    2.全局安装安装phpunit代码

    1
    composer global require phpunit/phpunit

    3.创建 phpunit.xml放在你的项目根目录, 这个文件是 phpunit 会默认读取的一个配置文件:

    1
    2
    3
    4
    5
    6
    7
    <phpunit bootstrap="vendor/autoload.php">
        <testsuites>
            <testsuite name="service">
                <directory>tests</directory>
            </testsuite>
        </testsuites>
    </phpunit>

    先写一个需要测试的类,该类有一个eat方法,方法返回字符串:eating,文件名为Human.php

    <?php

    class Human
    {
    public function eat()
    {
    return 'eating';
    }
    }
    再写一个phpunit的测试类,测试Human类的eat方法,必须引入Human.php文件、phpunit,文件名为test1.php
    <?php

    include 'Human.php';

    use PHPUnitFrameworkTestCase;
    class TestHuman extends TestCase
    {
    public function testEat()
    {
    $human = new Human;
    $this->assertEquals('eating', $human->eat());
    }
    }
    ?>
    其中assertEquals方法为断言,判断eat方法返回是否等于'eating',如果返回一直则成功否则返回错误,运行测试:打开命令行,进入test1.php的路径,然后运行测试:
    phpunit test1.php
    返回信息:

    返回信息:
    PHPUnit 4.8.35 by Sebastian Bergmann and contributors.

    .

    Time: 202 ms, Memory: 14.75MB

    OK (1 test, 1 assertion)
    则表示断言处成功,即返回值与传入的参数值一致。

  • 相关阅读:
    Hadoop 2.7 伪分布式环境搭建
    Linux 安装配置 Tomcat
    Hadoop hdfs完全分布式搭建教程
    Hadoop中ssh+IP、ssh+别名免秘钥登录配置
    VMware 克隆多台Linux机器并配置IP
    Linux 安装JDK
    连表更新数据
    设置让php能够以root权限来执行exec() 或者 shell_exec()
    Jsonp 关键字详解及json和jsonp的区别,ajax和jsonp的区别
    php7安装mongoDB扩展
  • 原文地址:https://www.cnblogs.com/brady-wang/p/10555628.html
Copyright © 2011-2022 走看看