简体   繁体   English

如何在Symfony2中使用Beip与Liip \\ FunctionalTestBundle?

[英]How to Use Behat with Liip\FunctionalTestBundle in Symfony2?

I'm using Liip\\FunctionalTestBundle for Unit testing, it works very well. 我正在使用Liip\\FunctionalTestBundle进行单元测试,效果非常好。

AppCategoryControllerTest.php: AppCategoryControllerTest.php:

class AppCategoryControllerTest extends BoEditoAuthWebTestCase
{
    public function setUp()
    {
        parent::setUp();

        // It returns an array of class paths
        $this->loadFixtures($this->getAllDataFixtures());
    }
    //...
}

Now I would like to use my test fixtures with Behat. 现在我想用Behat测试装置。

How is that possible? 怎么可能?

FeatureContext.php: FeatureContext.php:

/**
 * @BeforeScenario @createSchema
 *
 * load my fixtures with Liip\FunctionalTestBundle\Test\WebTestCase
 */
public function createDatabase()
{
    // What can I do here?
}

As this is a PHPUnit test case, you can't use it directly in Behat. 由于这是一个PHPUnit测试用例,因此无法直接在Behat中使用它。 You'll need to replicate the behaviour in a Behat context. 您需要在Behat上下文中复制行为。

Have a look at the SymfonyDoctrineContext from Behat/CommonContexts. 看看Behat / CommonContexts的SymfonyDoctrineContext。 It should be a good place to start. 它应该是一个很好的起点。 The context is written for Behat 2 so you'll need to tweak it for Behat 3. 上下文是为Behat 2编写的,因此您需要为Behat 3进行调整。

I'm using Liip\\FunctionalTestBundle for Unit testing, it works very well. 我正在使用Liip \\ FunctionalTestBundle进行单元测试,效果非常好。

If you're using the FunctionalTestBundle, then you're not writing unit tests. 如果您正在使用FunctionalTestBundle,那么您不会编写单元测试。 Unit tests are isolated. 单元测试是孤立的。 Functional tests are type of an integration test (not isolated). 功能测试是集成测试的类型(未隔离)。 If you haven't noticed yet, this kind of testing is brittle and slow. 如果你还没有注意到,这种测试是脆弱的。 I recommend you to learn how to focus more of your efforts on true unit testing. 我建议您学习如何将更多精力集中在真正的单元测试上。

solution for the problem 解决问题的方法

// ApiContext.php
<?php

  declare(strict_types=1);

  use Behat\Behat\Context\Context;

  class ApiContext extends \Liip\FunctionalTestBundle\Test\WebTestCase implements Context
  {
    public function __construct($kernelDir)
    {
      parent::__construct();
      $_SERVER['KERNEL_DIR'] = $kernelDir;
    }

  /**
   * @BeforeScenario
   *
   */
  public function loadFixturesData(): void
  {
    $fixturesLocation = '@AppBundle/DataFixtures/ORM/hautelook_alice/';
    $this->loadFixtureFiles([
        $fixturesLocation . 'fixture1.yml',
        $fixturesLocation . 'fixture2.yml',
    ]);
}

} }

// behat.yml
....
  suites:
    default:
      contexts:
        - ApiContext:
            kernelDir: "app/"
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM