phpunit 基境

基境共享测试数据

有些时候每个测试方法中都创建了一个相同的对象,在一些场景下是重复劳动,为什么不能只创建一次然后供其他测试方法访问呢?这需要理解 PHPUnit 执行测试用例的工作流程。

我们没有办法在不同的测试方法中通过某成员属性来传递数据,因为每个测试方法的执行都是新建一个测试类对象,然后调用相应的测试方法

即测试的执行模式并不是

testObj = new ExampleTest(); 
testObj->testMethod1();
testObj->testMethod2();

而是

testObj1 = new ExampleTest(); 
testObj1->testMethod1(); 

testObj2 = new ExampleTest(); 
testObj2->testMethod2();

所以testMethod1()修改的属性状态无法传递给 testMethod2()使用。

PHPUnit则为我们提供了全面的hook接口:

public static function setUpBeforeClass()/tearDownAfterClass()    //测试类构建/解构时调用 
protected function setUp()/tearDown()                             //测试方法执行前/后调用 
protected function assertPreConditions()/assertPostConditions()   //断言前/后调用

当运行测试时,每个测试类大致就是如下的执行步骤

#测试类基境构建 
setUpBeforeClass 
#new一个测试类对象 
#第一个测试用例 
setUp 
assertPreConditions 
assertPostConditions 
tearDown 


#new一个测试类对象 
#第二个测试用例 
setUp 
assertPreConditions 
assertPostConditions 
tearDown 
... 


#测试类基境解构 
tearDownAfterClass

所以我们可以在测试类构建时使用setUpBeforeClass创建一个 App\Example 对象作为测试类的静态成员变量(tearDownAfterClass主要用于一些资源清理,比如关闭文件,数据库连接),然后让每一个测试方法用例使用它:

<?php
namespace Tests;

use App\Example;
use PHPUnit\Framework\TestCase as BaseTestCase;

class ExampleTest extends BaseTestCase
{
  // 类静态属性
  private static $example;

  public static function setUpBeforeClass()
  {
    self::$example = new Example();
  }

  public function testGetTrue()
  {
    // 类的静态属性更新
    self::$example->setMsg("hello big_cat");
    $result = self::$example->getTrue();
    $this->assertTrue($result);
  }

  public function testGetFalse()
  {
    $result = self::$example->getFalse();
    $this->assertFalse($result);
  }

  /**
   * 依赖 testGetTrue 执行完毕
   * @depends testGetTrue
   * @return [type] [description]
   */
  public function testGetMsg()
  {
    $result = self::$example->getMsg();
    $this->assertEquals($result, "hello big_cat");
  }
}

或者使用@depends注解来声明二者的执行顺序(依赖关系),并使用传递参数的方式来满足需求。



public function testMethod1()
{
  $this->assertTrue(true);
  return "hello";
}

/**
 * @depends testMethod1
 */
public function testMethod2($str)
{
  $this->assertEquals("hello", $str);
}


#执行模式大概如下
testObj1 = new Test;
$str = testObj1->testMethod1();

testObj2 = new Test;
testObj2->testMethod2($str);

理解测试执行的模式还是很有帮助的,其他高级特性请浏览官方文档

使用phpunit.xml编排测试套件

使用测试套件来管理测试,vi phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="./vendor/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false">
  <testsuites>
    <!--可以定义多个 suffix 用于指定待执行的测试类文件后缀-->
    <testsuite name="Tests">
      <directory suffix="Test.php">./test</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
      <!--可以定义多个 对./app下的业务代码做覆盖率统计-->
      <directory suffix=".php">./app</directory>
    </whitelist>
  </filter>
  <logging>
    <!--覆盖率报告生成类型和输出目录 lowUpperBound低覆盖率阈值 highLowerBound高覆盖率阈值-->
    <log type="coverage-html" target="./reports" lowUpperBound="35" highLowerBound="70"/>
  </logging>
</phpunit>

然后直接运phpunit行即可:

 

原文地址:http://www.manongjc.com/article/68236.html

发布了87 篇原创文章 · 获赞 58 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/benpaodelulu_guajian/article/details/90346830
今日推荐