ThinkPHP5与单元测试PHPUnit使用详解

2020-02-23 16:02:15王冬梅

FirstTest为测试类FirstTest继承于 PHPUnitFrameworkTestCase测试方法testTure(),测试方法必须为public权限,一般以test开头,或者你也可以选择给其加注释@test来表在测试方法内,类似于 assertEquals() 这样的断言方法用来对实际值与预期值的匹配做出来以此判断方法是否正确

命令行执行:
tests目录下 执行  >phpunit FirstTest     命令 测试文件命名

测试项目内方法 

tp5项目下的控制器在  F:wampwampwwwtp5applicationindexcontroller 文件夹下 写一个简单的方法

在tests内写一个IndexTest.php

<?php
 
namespace Apptests;
require_once __DIR__ . '/../vendor/autoload.php';
 
use PHPUnitFrameworkTestCase;
use appindexcontrollerIndex;
 
 
class IndexTest extends TestCase
{
  public function testSum()
  {
    $obj = new Index;
    $this->assertEquals(6, $obj->index(2,3));
 
  }
 
}

执行后的结果  成功!

如果我在index.php,和IndexTest.php都故意写错 

返回结果  会有错误位置

其他用法

其他用法请参考官网:PHPUnit中国官网 

相关文章 大家在看