zoukankan      html  css  js  c++  java
  • phpunit

    教程及文档:

    https://www.jianshu.com/p/abcca5aa3ad6

    http://www.phpunit.cn/manual/current/zh_cn/phpunit-book.html#installation.requirements

    https://phpunit.readthedocs.io/zh_CN/latest/

    https://www.kancloud.cn/manual/phpunit-book/68615

    http://phpunit.cn/manual/7.0/zh_cn/index.html

    注意安装分为档案包phpunit.phar和composer安装

    phpunit.phar :将所有的类都压缩到这个档案包里了   
    
    composer:    没有压缩默认会下载到项目的vendor目录里

    写好测试类后需要在窗口命令行执行才行!

    phpunit ArrayTest
    
    例子中,PHPUnit 命令行测试执行器将在当前工作目录中寻找 ArrayTest.php 源文件并加载之。而在此源文件中应当能找到 ArrayTest 测试用例类,此类中的测试将被执行

    phpunit.xml

    在使用phpunit时将命令路径移动到测试文件目录,使用phpunit命令要不然会出现读取不了PHPUnit.xml

    bootstrap="./autoload.php"     在测试之前加载的的PHP 文件,一般可以做一个初始化工作

    name:                 套件名称

    directory :           套件测试的目录,目录下一般放测试文件的用例

        ----suffix :          测试文件后缀,如果不填写,则默认后缀为*Test.php,即phpunit 默认会执行*Test.php  的文件

        ----action:         测试目录名

    file:                    可以单独设置测试文件

    exclude:            排除不需要测试的文件

    可以用 phpVersion 和 phpVersionOperator 属性来指定 PHP 版本需求。在以下例子中,仅当 PHP 版本至少为 5.3.0 时才会将 /path/to/*Test.php 文件与 /path/to/MyTest.php 文件添加到测试套件中

    <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
     <php>
      <includePath>.</includePath>
      <ini name="foo" value="bar"/>
      <const name="foo" value="bar"/>
      <var name="foo" value="bar"/>
      <env name="foo" value="bar"/>
      <post name="foo" value="bar"/>
      <get name="foo" value="bar"/>
      <cookie name="foo" value="bar"/>
      <server name="foo" value="bar"/>
      <files name="foo" value="bar"/>
      <request name="foo" value="bar"/>
    </php>  
    
    这段xml 可以对应以下PHP 代码
    
    includePath
    
    ini_set('foo', 'bar');
    define('foo', 'bar');
    $GLOBALS['foo'] = 'bar';
    $_ENV['foo'] = 'bar';
    $_POST['foo'] = 'bar';
    $_GET['foo'] = 'bar';
    $_COOKIE['foo'] = 'bar';
    $_SERVER['foo'] = 'bar';
    $_FILES['foo'] = 'bar';
    $_REQUEST['foo'] = 'bar';

    目录结构

    ├── reports
    ├── phpunit.xml
    ├── src
    │   ├── autoload.php
    │   ├── Money.php
    └── tests
        └── MoneyTest.php

    1、定义

    D:xampphtdocs estPHPunit>phpunit --bootstrap ./src/Money.php ./tests/MoneyTest.php

    手动指定autoload.php       D:xampphtdocs estPHPunit>phpunit --bootstrap ./src/autoload.php ./tests/MoneyTest

    定义xml文件后     D:xampphtdocs estPHPunit>phpunit ./tests/MoneyTest   

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 它将在递归遍历添加在tests的所有 *Test.php 文件中找到的 *Test 类 -->
    <phpunit bootstrap="./src/autoload.php" verbose="true">
        <testsuites>
            <testsuite name="money test">
                <directory suffix="Test.php">tests</directory>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist processUncoveredFilesFromWhitelist="true">
                <directory suffix=".php">./src</directory>
            </whitelist>
        </filter>    
    </phpunit>

    日志 

    利用PHP CodeCoverage來计算程序代码覆盖率(code coverage),需要安裝 Xdebug

    https://pecl.php.net/package/xdebug 下载 然后将dll扩展放到php/ext  ,php.ini extendsion=xxxx...

    先在项目下建立一个reports/目录,存放code coverage分析的结果。

    然后执行   

    phpunit --coverage-html reports/ tests/
    D:xampphtdocs estPHPunit>phpunit --coverage-html ./reports/ ./tests/

    或者执行

    phpunit --bootstrap vendor/autoload.php --coverage-html reports/ tests/

    当然,也可以使用XML来设定。

        <logging>
            <log type="coverage-html" target="./reports" charset="UTF-8"/>
        </logging>

    接着执行测试:

    phpunit tests/EventTest.php

    断言:

    assertArrayHasKey()
    assertClassHasAttribute()
    assertArraySubset()
    assertClassHasStaticAttribute()
    assertContains()
    assertContainsOnly()
    assertContainsOnlyInstancesOf()
    assertCount()
    assertEmpty()
    assertEqualXMLStructure()
    assertEquals()
    assertFalse()
    assertFileEquals()
    assertFileExists()
    assertGreaterThan()
    assertGreaterThanOrEqual()
    assertInfinite()
    assertInstanceOf()
    assertInternalType()
    assertJsonFileEqualsJsonFile()
    assertJsonStringEqualsJsonFile()
    assertJsonStringEqualsJsonString()
    assertLessThan()
    assertLessThanOrEqual()
    assertNan()
    assertNull()
    assertObjectHasAttribute()
    assertRegExp()
    assertStringMatchesFormat()
    assertStringMatchesFormatFile()
    assertSame()
    assertStringEndsWith()
    assertStringEqualsFile()
    assertStringStartsWith()
    assertThat()
    assertTrue()
    assertXmlFileEqualsXmlFile()
    assertXmlStringEqualsXmlFile()
    assertXmlStringEqualsXmlString()

    标注:
    @author
    @after
    @afterClass
    @backupGlobals
    @backupStaticAttributes
    @before
    @beforeClass
    @codeCoverageIgnore*
    @covers
    @coversDefaultClass
    @coversNothing
    @dataProvider
    @depends 测试类中一个方法依赖另一个方法的标注
    @expectedException
    @expectedExceptionCode
    @expectedExceptionMessage
    @expectedExceptionMessageRegExp
    @group
    @large
    @medium
    @preserveGlobalState
    @requires
    @runTestsInSeparateProcesses
    @runInSeparateProcess
    @small
    @test
    @testdox
    @ticket
    @uses

  • 相关阅读:
    【Maven】构建子项目命令Maven clean/install modules
    【DB】Oracle批量导入CSV数据
    【DB】索引与性能
    【Postfix】邮件转存
    【Mybatis】Oracle Sequence序列的失序问题
    【Java】java.sql.SQLRecoverableException 解释
    【I/O】NAS 性能测试
    【Java】JVM线程状态查询
    【Oracle】记录一次数据库连接池性能瓶颈问题
    【JavaMail】
  • 原文地址:https://www.cnblogs.com/lichihua/p/9521644.html
Copyright © 2011-2022 走看看