学习phpunit

Posted Follow U Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习phpunit相关的知识,希望对你有一定的参考价值。

1、我的疑问:什么是测试套件

测试套件,是一套业务逻辑验证时,所需要的测试用例,包含测试逻辑、测试预期、测试结果。

一个套件里面,由多个测试类集合,一个测试类中可以有多个测试实例,因此可以测试多个测试类的多个逻辑

2、开始看phpnuit

phpunit 的五个基础类和接口

(1)class TestSuite 实现了(3)Test接口
(2)abstract class TestCase 实现了Test接口,且继承了(4)Assert类
(5)class TestResult :case运行完了之后,这个类可以收集我们测试结果,并执行这些测试结果、(收集覆盖率、收集成功了几个,错误了几个,错误信息,之类的,括号的话不负责,哈哈。)
再学习一下,这些类的特点:
Test:所有测试类必须要实现的接口,里面只定义了一个
public function run(TestResult $result = null);一个接口的方法。
 
TestCase:它的源码,如何构造一个TestCase中,举例是
* <?php
* class MathTest extends PHPUnitFrameworkTestCase
* {
* public $value1;
* public $value2;
*
* protected function setUp()
* {
* $this->value1 = 2;
* $this->value2 = 3;
* }
* }

从类名看,这是一个单元测试(测方法的)的基类(暂时不学,所以不看了)

TestSuite:
/**
     * Constructs a new TestSuite:
     *
     *   - PHPUnitFrameworkTestSuite() constructs an empty TestSuite.
     *
     *   - PHPUnitFrameworkTestSuite(ReflectionClass) constructs a
     *     TestSuite from the given class.
     *
     *   - PHPUnitFrameworkTestSuite(ReflectionClass, String)
     *     constructs a TestSuite from the given class with the given
     *     name.
     *
     *   - PHPUnitFrameworkTestSuite(String) either constructs a
     *     TestSuite from the given class (if the passed string is the
     *     name of an existing class) or constructs an empty TestSuite
     *     with the given name.
*/

从注释看,传入的参数是个类,那么,测试的对象很可能是接口之类的或者一套被测方法,而接口测试其实是一串测试方法的测试(所以,这个是接口测试要学的吧)


Assert:前面说了,是一串检查期望值的静态方法,可以直接通过类名调用他的方法。
 
TestResult:对测试结果的处理(还没咋研究,处理了啥)

以上是关于学习phpunit的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5:模型上的fresh()在PHPUnit中不起作用

phpunit在Windows下安装

初识 PHPunit stub 模拟返回数据

学习phpunit

Laravel PHPUnit:如何测试 url 前缀

VS Code 不显示供应商目录中 PHPUnit\Framework 类的自动完成建议