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)
    则表示断言处成功,即返回值与传入的参数值一致。

  • 相关阅读:
    RAC RMAN 备份 RMAN03009 ORA19504 ORA27040 RMAN06012 channel c3 not allocated 错误分析
    Oracle 发送邮件 存储过程 代码
    RAC 之 RMAN 备份
    Oracle 一次小优化
    RAC 实例不能启动
    Oracle Hint
    RAC 之 RMAN 备份
    远程客户端 访问 ASM 实例
    RAC RMAN 备份 RMAN03009 ORA19504 ORA27040 RMAN06012 channel c3 not allocated 错误分析
    RAC the cluster database(DB_NAME) already exits
  • 原文地址:https://www.cnblogs.com/brady-wang/p/10555628.html
Copyright © 2011-2022 走看看