ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

phpUnit中数据供给

2020-03-24 16:03:44  阅读:370  来源: 互联网

标签:function arr 供给 错误信息 value phpUnit php 数据 public


数据供给

索引数组

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase{

    public function one()
    {
        return [
            [1, 2, 3],
            [1, 1, 2],
            [1, 1, 1]
        ];
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }


}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

..F                                                                 3 / 3 (100%)

Time: 604 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set #2 (1, 1, 1)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 1.

D:\yanjing\workspace\study_test\php\MyClass.php:26

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

关联数组

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{

    public function one()
    {
        return [
            '第一项' => [1, 2, 3],
            '第一项' => [1, 1, 2],
            '第一项' => [1, 1, 1],
        ];
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 596 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set "第一项" (1, 1, 1)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 1.

D:\yanjing\workspace\study_test\php\MyClass.php:27

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

迭代器对象

定义一个迭代器

<?php
namespace Test\Iterator;

class RmbIterAtor implements \IterAtor
{
    private $arr = [
        'first' => [1, 2, 3],
        'second' => [1, 3, 4],
        'third' => [1, 1, 0],
    ];

    public function current()
    {
        return current($this->arr);
    }

    public function key()
    {
        return key($this->arr);
    }

    public function next()
    {
        return next($this->arr);
    }

    public function rewind()
    {
        return reset($this->arr);
    }

    public function valid()
    {
        return current($this->arr);
    }
}

测试

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;
use Test\iterator\RmbIterator;

class MyClassTest extends TestCase
{

    public function one()
    {
        return new RmbIterator;
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}

结果

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

..F                                                                 3 / 3 (100%)

Time: 757 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set "third" (1, 1, 0)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 0.

D:\yanjing\workspace\study_test\php\MyClass.php:24

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

数据供给优先级高于依赖

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{


    public function one()
    {
        return [
            'key1' => [1, 2, 3],
            'key2' =>[1, 1, 2],
            'key3' =>[1, 1, 0]
        ];
    }


    /**
     * @test
     */
    public function two()
    {
        $this->assertTrue(true);
        return [
            [100, 100, 200],
            [10, 10, 20],
            [1, 1, 0]
        ];
    }    

    /**
     * @test
     * @depends two
     * @dataProvider one
     */
    public function three($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

...F                                                                4 / 4 (100%)

Time: 604 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::three with data set "key3" (1, 1, 0)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 0.

D:\yanjing\workspace\study_test\php\MyClass.php:43

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.

标签:function,arr,供给,错误信息,value,phpUnit,php,数据,public
来源: https://www.cnblogs.com/zxcv123/p/12559482.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有