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

时间:2016-04-13 12:25:17

标签: behat symfony

我有这样的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: ~

还有这个文件夹结构:

enter image description here

使用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);
    }

}

为什么找不到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: ~

更新

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

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: ~

然后使用--profile参数:

运行
sudo vendor/behat/behat/bin/behat --profile user_login

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

1 个答案:

答案 0 :(得分: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: ~

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

"autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
  • Ran

    composer dump-autoload -o

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