Behat 3.1 - 多个上下文,找不到第二个上下文类

Dar*_*s.V 2 symfony behat

我有这样的behat.yml

default:
  autoload:
   #'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
   #'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
   [ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
  formatters:
      progress: ~
  suites:
     app_features:
       #paths:
         #features: %paths.base%/tests/AppBundle/features
       paths: [ %paths.base%//tests/AppBundle/features ]
       contexts:
         #- tests\AppBundle\user_registration\UserRegistrationContext
         - UserRegistrationContext
         - UserLoginContext

  #suites:
   #   default:
    #      contexts:
     #         - FeatureContext:
      #            session:   '@session'
  extensions:
    Behat\Symfony2Extension: ~
Run Code Online (Sandbox Code Playgroud)

还有这个文件夹结构:

在此输入图像描述

使用UserRegistrationContext类可以,它不会抛出错误.然后我添加了UserLoginContext,它无法找到.

我在composer.json中使用"behat/behat":"^ 3.1".

behat.yml文件与'tests'目录处于同一级别.

UserLoginContext看起来像这样:

<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Session;


use AppBundle\Controller\UserController;


/**
 * Defines application features from the specific context.
 */
class UserLoginContext implements Context, SnippetAcceptingContext
{

    private $session;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
        // Choose a Mink driver. More about it in later chapters.
        $driver = new GoutteDriver();
        $this->session = new Session($driver);
    }

}
Run Code Online (Sandbox Code Playgroud)

为什么找不到UserLoginContext?

更新

我注意到如果我这样做 - 留下一个上下文,则找到UserLoginContext:

default:
  autoload:
   #'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
   '': %paths.base%/tests/AppBundle/features/user_login/bootstrap
   #[ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
  formatters:
      progress: ~
  suites:
     app_features:
       #paths:
         #features: %paths.base%/tests/AppBundle/features
       paths: [ %paths.base%//tests/AppBundle/features ]
       contexts:
         #- tests\AppBundle\user_registration\UserRegistrationContext
         #- UserRegistrationContext
         - UserLoginContext

  #suites:
   #   default:
    #      contexts:
     #         - FeatureContext:
      #            session:   '@session'
  extensions:
    Behat\Symfony2Extension: ~
Run Code Online (Sandbox Code Playgroud)

更新

我找到的临时解决方案 - 为每个上下文创建一个新的配置文件.但我想它不应该如何,但仍然会显示我是如何做到的:

user_login:
  autoload:
   '': %paths.base%/tests/AppBundle/features/user_login/bootstrap
  formatters:
      progress: ~
  suites:
     app_features:
       paths: [ %paths.base%//tests/AppBundle/features ]
       contexts:
         - UserLoginContext
  extensions:
    Behat\Symfony2Extension: ~
Run Code Online (Sandbox Code Playgroud)

然后使用--profile参数运行:

sudo vendor/behat/behat/bin/behat --profile user_login
Run Code Online (Sandbox Code Playgroud)

小问题是,当我想要运行所有测试时,我将不得不运行许多命令.还配置重复.还在等待更好的解决方案.

Dar*_*s.V 5

在这里,我注意到一些有用的小文本.

http://docs.behat.org/en/v3.0/guides/6.profiles.html

使用behat.yml自动加载只允许PSR-0.您也可以使用composer.json自动加载,这也将允许PSR-4

惭愧的是 - 他们真的需要写更多关于这个,因为新用户根本不会理解如何与composer.json一起使用它.

我做了什么:

  • 添加了UserLoginContext类的名称空间,与文件夹结构相同

    namespace Tests\AppBundle\features\user_login\bootstrap;

  • 将此命名空间添加到上下文附近的behat.yml:

.

# for each context class - new profile.

default:
  autoload:
   '': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
  formatters:
      progress: ~
  suites:
     app_features:
       #paths:
         #features: %paths.base%/tests/AppBundle/features
       paths: [ %paths.base%//tests/AppBundle/features ]
       contexts:

         - UserRegistrationContext
         - Tests\AppBundle\features\user_login\bootstrap\UserLoginContext

  #suites:
   #   default:
    #      contexts:
     #         - FeatureContext:
      #            session:   '@session'
  extensions:
    Behat\Symfony2Extension: ~
Run Code Online (Sandbox Code Playgroud)

那些没有在他们的composer.json中的人需要添加这个:

"autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
Run Code Online (Sandbox Code Playgroud)
  • composer dump-autoload -o

我很幸运,我有PSR-4的经验,可以猜测解决方案,我有时间思考和实验.