alnutile/pickle

从 Gherkin 到 Dusk

v0.0.2 2017-05-24 19:21 UTC

This package is auto-updated.

Last update: 2024-09-06 09:42:18 UTC


README

Build Status

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

概述

将 Gherkin 文件转换为与 PHPUnit 和 Dusk 兼容的文件。

这将尝试通过 Gherkin 格式的文件轻松地与 Dusk 和 PHPUnit 一起工作。

如果您熟悉 Behat,则此工作流程可能类似。

介绍视频

介绍幻灯片

主题

初始化

在这个例子中,我已经编写了一个功能 1 文件,现在我想将其转换成我的第一个PHPUnit兼容的测试。

例如,我创建了一个文件 tests/features/profile.feature

Feature: Test Profile Page
  Can See and Edit my profile
  As a user of the system
  So I can manage my profile

  Scenario: Edit Profile
    Given I have a profile created
    And I am in edit mode
    Then I can change the first name
    And the last name
    And and save my settings
    Then when I view my profile it will have those new settings

我将在后面讨论的一个关键方面是如何使用一个文件来驱动两种类型的测试,集成和浏览器。以及 Gherkin 语法如何影响我命名类的方式,以便与功能的商业写作一致 2

注意:上述功能并没有真正总结出业务目标,它仍然相当关注于网络。

所以到现在为止,我可以输入,假设您像我下面所介绍的那样全局安装了 Pickle

pickle initialize tests/features/profile.feature 

或者对于一个浏览器测试

pickle initialize --context=browser tests/features/profile.feature

在这种情况下,让我们关注领域上下文,例如集成。

现在它将在 tests/Feature/ProfileTest.php 中为我创建一个测试

然后我可以开始处理这个文件,它看起来可能像这样

<?php

class ProfileTest extends TestCase {

    /**
      * @group editProfile
      */
    public function testGivenIHaveAProfileCreated() {
        $this->givenIHaveAProfileCreated();
        $this->andIamInEditMode();
        //etc etc
    }

    protected function andIamInEditMode() {
        $this->markTestIncomplete('Time to code');
    }

    protected function andIamInEditMode() {
        $this->markTestIncomplete('Time to code');
    }
    
    //and all the rest

}

运行

现在这只是锦上添花,您只需退回到基本操作,一切仍然可以正常工作。

pickle run tests/features/profile.feature 

或者直接回到使用PHPUnit

phpunit tests/Feature/ProfileTest.php

或者通过Pickle使用Dusk

pickle run --context=browser tests/features/profile.feature 

或者直接通过Dusk

php artisan dusk tests/Browser/ProfileTest.php

UI 示例

完成集成测试和所有准备工作后,我可以开始进行浏览器测试。

所以我运行了以下命令

pickle run --context=browser tests/features/profile.feature 

我得到了以下结果

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class ExampleTest extends DuskTestCase
{
    /**
     * @var Browser
     */
    protected $browser;

    /**
     * A basic browser test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->browse(function (Browser $browser) {
            $this->browser = $browser;
            $this->visitHome();
            $this->seeSomething();
            //etc...
            //etc...
        });
    }

    private function visitHome()
    {
        $this->browser->visit('/');
    }

    private function seeSomething()
    {
        $this->browser->assertSee('Laravel');
    }
}

通过将 $this->browser 设置为全局,我可以一次处理一个步骤或函数

  • 访问页面
  • 输入表单字段
  • 输入另一个表单字段
  • 提交
  • 查找结果

添加测试

例如,当您需要向功能文件添加新的场景,并且需要更新类文件时。

例如

Feature: Test Profile Page
  Can See and Edit my profile
  As a user of the system
  So I can manage my profile

  Scenario: Edit Profile
    Given I have a profile created
    And I am in edit mode
    Then I can change the first name
    And the last name
    And and save my settings
    Then when I view my profile it will have those new settings

  @new_scenario
  Scenario: View Profile
    Given I have a profile created
    And I am in view mode
    Then I can see the first name
    And the last name

我们现在需要添加更多内容,所以运行

pickle append tests/features/test_profile.feature

或者

pickle append --context=browser tests/features/test_profile.feature

这将添加新的场景和方法,确保不重复场景。

🤖🤖警告🤖🤖

这将不会更新现有的场景

例如,您修改了这个场景

  Scenario: Edit Profile
    Given I have a profile created
    And I am in edit mode
    Then I can change the first name
    And the last name
    And and save my settings
    Then when I view my profile it will have those new settings

  Scenario: Edit Profile
    Given I have a profile created
    And I am in edit mode
    Then I can change the first name
    And the last name
    And I can add my photo
    And and save my settings
    Then when I view my profile it will have those new settings

所以新的一行 And I can add my photo 将显示为一个方法

protected function andICanAddMyPhoto() {
   $this->markIncomplete("Time to Code");
 }

但是 它不会将其添加到 Scenario 步骤中,如初始化后所见

    public function testEditProfile()
    {
        $this->browse(function (Browser $browser) {
            $this->browser = $browser;
            $this->foo();
            $this->bar();
        });
    }

您只需将其添加到正确的顺序位置,在之前或之后即可

$this->foo();
$this->bar();
$this->andICanAddMyPhoto();

路线图

初始化 (完成)

使用 Gherkin 文件创建单元或浏览器测试的能力

待办事项

将工作移入 pickle 命令行文件,参见应用程序根目录。这只是简单的事情,因为它正在测试中工作

我会尽快处理这个

现在测试显示了它的工作情况,现在我需要将其添加到全局命令中

添加代码片段 (完成)

能够添加更多步骤和场景到现有的单元和浏览器测试中

因此,如果他们向功能中添加新的步骤或场景,pickle足够智能,可以附加场景(这里很简单),还可以根据需要将步骤添加到场景中。

待办事项

一切!我的意思是,我有一段代码来防止重复的方法。

从Gherkin运行(已完成)

从Gherkin测试运行,无论是领域测试还是UI测试,都会以Gherkin为基础的输出结果

例如

pickle run tests/features/profile.feature --domain

它会知道使用 tests/Feature/ProfileTest.php

并以Behat那样漂亮的Gherkin格式输出

待办事项

我将在这里跟踪这些问题

尽可能多/尽快在项目这里进行跟踪

安装

首先,您需要安装全局Composer工具以帮助整体了解更多信息

composer global require consolidation/cgr

然后确保将~/.composer/vendor/bin添加到您的$PATH`

您可能已经有这个了

例如,编辑您的.bash_profile`并添加

export PATH=~/.composer/vendor/bin:$PATH

然后输入

source ~/.bash_profile

现在如果您输入

cgr --help

您应该会得到一些类似这样的输出

The 'cgr' tool is a "safer" alternative to 'composer global require'.
Installing projects with cgr helps avoid dependency conflicts between
different tools.  Use 'cgr' wherever 'composer global require' is recommended.

.....
.....
.....

现在对于Pickle

cgr global require alnutile/pickle:dev-master

现在您应该能够在您的Mac上的任何位置运行

pickle help

并且由于这是一个忙碌的项目,所以需要经常升级

cgr global update alnutile/pickle

测试

$ composer test

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过me@alfrednutile.info而不是使用问题跟踪器发送电子邮件。

致谢

脚注

1 特性文件是用Gherkin编写的。您可以在这里看到一些示例

“特性”区域给出了特性的标题和目的

  • 正在构建什么
  • 为谁构建
  • 业务结果是什么

然后引出“场景”

2

您可以在这里了解更多关于BDD的信息

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。