PHP 5.3 的行为驱动开发(BDD)风格的测试工具

dev-master / 1.0.x-dev 2017-11-07 08:10 UTC

This package is auto-updated.

Last update: 2024-09-06 10:18:40 UTC


README

Build Status

dspec 是 PHP 5.3 的 BDD 风格测试工具。它有点不可靠。它的目标是像令人惊叹的 mocha 一样,但显然没有那么好。没有匹配器,如果你需要模拟和存根,可以使用 Hamcrest,如果你需要模拟和存根,可以使用 Mockery

更多信息,请参阅文档

安装

安装 dspec 的唯一文档化方法是使用 Composer

$ composer.phar require --dev davedevelopment/dspec:*

用法

创建 Spec 文件

<?php

// BowlingSpec.php

require_once 'Bowling.php';
require_once 'hamcrest/Hamcrest.php';

describe("Bowling", function() {

    beforeEach(function() {                                  // run before every sibling and descendant it() 
        $this->bowling = new Bowling;                        // PHP 5.4's closure binding allows the use of this
    }); 

    it("should score zero for gutter game", function() {
        for ($i = 0; $i < 20; $i++) {
            $this->bowling->hit(0);
        }

        assertThat($this->bowling->score, equalTo(0));      // hamcrest assertion
    });

});

运行它

$ vendor/bin/dspec BowlingSpec.php

更多示例,请查看 featuresspec 目录。

配置

有一些命令行选项,dspec --help 将显示这些选项。您还可以使用配置文件,通过在命令行中指定一个,或者通过在当前工作目录中有一个名为 dspec.ymldspec.yml.dist 的文件来使用。查看项目的 dspec.yml.dist 以获取配置的一些示例。

执行范围

各个方法中的闭包被绑定到上下文对象,该对象在整个测试运行期间被克隆,以尝试在适当的地方共享上下文,但允许每个示例都有那个共享上下文的干净版本。但这是一种最后的手段,正确地在 beforeEach 和 afterEach 钩子中进行设置和清理应该提供足够的隔离。

<?php

describe("Context", function() {

    $this->objA = new stdClass;

    it("has acccess to objA", function() {
        if (!isset($this->objA)) {
            throw new Exception("Could not access objA");
        }

        $this->objB = new stdClass;
    });

    it("does not have access to objB", function() {
        if (isset($this->objB)) {
            throw new Exception("Could access objB");
        }
    });
});

使用 $this 完全可选,在 PHP 5.3 中,您可以使用 let 和注入来传递变量

<?php

describe("test", function() {
    let("objA", function() {
        return new stdClass;
    });

    it("has acccess to objA", function($objA) {
        assertThat($objA, anInstanceOf("stdClass"));
    });
});

使用常规变量绑定与您的闭包是一个替代选项,但可能会有些混乱;

<?php

describe("Connection", function() {

    $db = new Connection();

    $dave = new User();
    $bex  = new User();

    beforeEach(function() use ($db, $dave, $bex) {
        $db->truncate('users');                
        $db->save($dave);
        $db->save($bex);
    });

    context("#findAll()", function() use ($db) {
        it("responds with all records", function() use ($db) {
            $users = $db->find('users');
            assertThat(count($users), equalTo(2));
        });
    });

});

全局函数

如果您不喜欢全局函数,您可以使用 DSpec 类的方法

<?php

use DSpec\DSpec as ds;

ds::describe("something", function() {
    ds::context("when this", function() {
        ds::it("does this", function() {

        });
    });
});

或者,DSpec 将测试文件包含在 DSpec\Context\SpecContext 的作用域内,因此您也可以使用 $this,但我认为这有点难看

<?php 

$this->describe("something", function() {
    $this->context("when this", function() {
        $this->it("does this", function() {

        });
    });
});

待办事项

  • 更多测试
  • 调试时记录日志会很好
  • 文档
  • 分析并改进内存消耗

贡献

查看上面的待办事项列表,我可能已经在做那些了。分支,写测试,写代码,重构,重复,提交拉取请求。

版权

版权所有 (c) 2012 Dave Marshall。有关更多信息,请参阅 LICENCE。