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

    tp5版本: 5.0.24

    单元测试版本:1.*

    1. 安装单元测试扩展:

    composer require topthink/think-testing 1.*

    2.安装完毕,运行 php think unit 出现问题

    [thinkexceptionErrorException]
      The each() function is deprecated. This message will be suppressed on further calls

    php7.2版本将each()方法废除,项目中使用each()的地方都会出现以上的错误

    解决:将each()替换成foreach()

    while (list($key, $val) = each($para)) {  }  改成   foreach ($para as $key => $val) {  }

    配置环境变量中的PATH参数:

    这时候将E:phpStudyPHPTutorialphpphp-5.6.27-nts目录中 php.exe 复制一份 php56.exe,在命令行就可以使用 php -v = php7.2 使用php56 -v = php5.6 的了,当然也可以同理使用 php71 -v = php7.1

    3. 问题解决了,现在可以重新执行 php71 think unit 了

    执行代码:

    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: yunwuxin <448901948@qq.com>
    // +----------------------------------------------------------------------
    namespace tests;
    
    class ExampleTest extends TestCase
    {
    
    //    public function testBasicExample()
    //    {
    //        $this->visit('/')->see('ThinkPHP');
    //    }
    
        // 测试查询模型返回,查询到数据则返回ok,否则返回失败
    //    public function testModel()
    //    {
    //        $this->assertNotNull(User::getOne('123'));
    //    }
        // 测试方法返回,如果结果和输入的值相同返回ok,否则返回失败
        public function testMethod()
        {
            $this->assertEquals(4,$this->add(1,3));
        }
        // 方法
        public function add($a,$b)
        {
            return $a+$b;
        }
    }

    执行结果:

    E:code	p	p_5_0_24>php71 think unit
    PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
    
    .
    
    Time: 124 ms, Memory: 4.00MB
    
    OK (1 test, 1 assertion)

    再试试测试,如果不通过的结果:

    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: yunwuxin <448901948@qq.com>
    // +----------------------------------------------------------------------
    namespace tests;
    
    class ExampleTest extends TestCase
    {
    
    //    public function testBasicExample()
    //    {
    //        $this->visit('/')->see('ThinkPHP');
    //    }
    
        // 测试查询模型返回,查询到数据则返回ok,否则返回失败
    //    public function testModel()
    //    {
    //        $this->assertNotNull(User::getOne('123'));
    //    }
        // 测试方法返回,如果结果和输入的值相同返回ok,否则返回失败
        public function testMethod()
        {
            $this->assertEquals(5,$this->add(1,3));
        }
        // 方法
        public function add($a,$b)
        {
            return $a+$b;
        }
    }
    E:code	p	p_5_0_24>php71 think unit
    PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
    
    F
    
    Time: 131 ms, Memory: 4.00MB
    
    There was 1 failure:
    
    1) testsExampleTest::testMethod
    Failed asserting that 4 matches expected 5.
    
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkConstraintIsEqual.php:137
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkAssert.php:2255
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkAssert.php:513
    E:code	p	p_5_0_24	estsExampleTest.php:29
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestCase.php:908
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestCase.php:768
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestResult.php:612
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestCase.php:724
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestSuite.php:722
    E:code	p	p_5_0_24vendorphpunitphpunitsrcFrameworkTestSuite.php:722
    E:code	p	p_5_0_24vendorphpunitphpunitsrcTextUITestRunner.php:440
    E:code	p	p_5_0_24vendorphpunitphpunitsrcTextUICommand.php:149
    E:code	p	p_5_0_24vendor	opthink	hink-testingsrccommandTest.php:42
    E:code	p	p_5_0_24	hinkphplibrary	hinkconsoleCommand.php:175
    E:code	p	p_5_0_24	hinkphplibrary	hinkConsole.php:674
    E:code	p	p_5_0_24	hinkphplibrary	hinkConsole.php:242
    E:code	p	p_5_0_24	hinkphplibrary	hinkConsole.php:185
    E:code	p	p_5_0_24	hinkphplibrary	hinkConsole.php:145
    E:code	p	p_5_0_24	hinkphpconsole.php:20
    
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.

    测试函数,更多测试请看帮助文档

    assertTrue 为判断是否真
    assertFalse 为判断是否假
    assertGreaterThan为判断是否大于
    assertLessThanOrEqual判断是否小于或等于
    assertEquals为判断是否相等

    tp5单元测试使用帮助文档 http://www.kancloud.cn/manual/thinkphp5/182511

    最后可能存在很多单元测试错误,可以将错误结果重定向到一个日志文件中:

    php71 think unit > unit.error.txt

    一些问题

    问题1:如果tp框架和app不再同一级目录,我们该怎么办呢?

    1. 重写一个cmd文件,内容如下:

    #!/usr/bin/env php
    <?php
    
    // 定义项目路径
    define('APP_PATH', __DIR__ . '/application/');
    // 定义配置文件目录和应用目录同级
    define('CONF_PATH', __DIR__ . '/config/');
    // 定义第三方项目文件根目录
    define('VENDOR_PATH', __DIR__ . '/tp_5_0_24/vendor/');
    // 扩展类库目录
    define('EXTEND_PATH', __DIR__ . '/extend/');
    // 加载框架引导文件
    require __DIR__.'/tp_5_0_24/thinkphp/console.php';

    2. 将 tp_5_0_24/phpunit.xml 文件复制到当前目录

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit backupGlobals="false"
             backupStaticAttributes="false"
             colors="true"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false"
             syntaxCheck="false">
        <testsuites>
            <testsuite name="Application Test Suite">
                <directory>./tests/</directory>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist>
                <directory suffix=".php">application/</directory>
            </whitelist>
        </filter>
    </phpunit>

    3. 新建目录 tests

    运行

    php71 cmd unit
  • 相关阅读:
    Linux安全-通过修改/etc/hosts.deny拒绝远程ssh暴力破解
    JS-二进制的处理者:Blob
    JS-二进制数据缓冲区ArrayBuffer
    axio源码分析
    【js】appendChild()具有移动性
    js实现-二进制数据转换为blob
    实现点击下载到本地的功能
    memo、useMemo、useCallback
    memoization缓存优化
    什么是纯函数?
  • 原文地址:https://www.cnblogs.com/jiangxiaobo/p/11050168.html
Copyright © 2011-2022 走看看