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

  • 相关阅读:
    ASP.NET Web Form(八)
    ASP.NET Web Form(八)
    软考知识点总结
    软考知识点总结
    c# 引用外部dll
    c# 引用外部dll
    ORA600 [Kcbz_check_objd_typ_1] Running a Job (Doc ID 785899.1)
    oracle创建em
    1918: 等值数目
    1917: 支配值数目
  • 原文地址:https://www.cnblogs.com/brady-wang/p/10555628.html
Copyright © 2011-2022 走看看