Codeception Gherkin,定义步骤实现路径

时间:2017-10-24 18:35:58

标签: php phpunit bdd codeception behat

我是php和codeception的新手,我想将Gherkin与Codeception一起使用,而且我已经设置了最低限度来使功能文件在Codeception中运行。我现在发现自己试图建立一个可扩展的结构并使用PageObject框架。我创建了一个 Steps 文件夹,我希望我的步骤实现保存在该文件夹中。默认情况下,运行codecept run some.feature会加载acceptance.suite.yml文件中定义的类。

动机:我希望能够将我的步骤实现保留在自己独立的文件夹中

鉴于我有一个accepted.suite.yml文件配置:

gherkin:
    contexts:
        default: 
            - AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: https://www.google.com/
            browser: chrome
        - \Helper\Acceptance

我的codeception.yml文件配置为:

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed

我在_support下面有步骤文件夹:

enter image description here

  

如何更改配置以允许我的步骤实现   从Steps文件夹调用?

1 个答案:

答案 0 :(得分:1)

在套件配置的gherkin:部分中,您需要列出在default:role:和/或tag:部分下组织的步骤类。官方文档中有一些配置示例:Gherkin options

下面是一个最近项目的示例(使用Codeception 2.5.6):

文件结构

/app/common
├── codeception.yml
├── tests
│   ├── acceptance.suite.yml
│   ├── _bootstrap.php
│   ├── _data
│   │   └── user.php
│   ├── _support
│   │   ├── AcceptanceTester.php
│   │   ├── Step
│   │   │   └── Acceptance
│   │   │       └── CuratorSteps.php

  

使用codecept generate:stepobject命令生成步骤对象时,步骤类的上述布局是默认布局,如下所示:

$ /app/vendor/bin/codecept -c /app/common generate:stepobject acceptance CuratorSteps

acceptance.suite.yml:

# acceptance.suite.yml
namespace: common\tests
suite_namespace: common\tests\acceptance
bootstrap: false
actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://example.com/
gherkin:
    contexts:
        default:
            - common\tests\AcceptanceTester
        role:
            curator:
                - common\tests\Step\Acceptance\CuratorSteps
  

文档中没有提及它,但是我注意到我必须列出步骤类的完整名称空间,否则我将得到“步骤定义...在上下文中找不到” 运行测试时出现错误,并且gherkin:steps codecept命令将不会返回步骤定义。

输出

$ /app/vendor/bin/codecept -vvv -c /app/common gherkin:steps acceptance
Steps from role:curator context:
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Step                                                               | Implementation                                                                           |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| I sign in as an admin                                              | common\tests\Step\Acceptance\CuratorSteps::iSignInAsAnAdmin                              |
| I should see a :arg1 button                                        | common\tests\Step\Acceptance\CuratorSteps::iShouldSeeAButton                             |
+--------------------------------------------------------------------+------------------------------------------------------------------------------------------+
Steps from default context:
+-------------------------------------+---------------------------------------------------------+
| Step                                | Implementation                                          |
+-------------------------------------+---------------------------------------------------------+
| I take a screenshot with name :arg1 | common\tests\AcceptanceTester::itakeAScreenshotWithName |
+-------------------------------------+---------------------------------------------------------+