How to Use Behat with Liip\FunctionalTestBundle in Symfony2?

I'm using LiipFunctionalTestBundle for Unit testing, it works very well.

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.

How is that possible?

FeatureContext.php:

/**
 * @BeforeScenario @createSchema
 *
 * load my fixtures with LiipFunctionalTestBundleTestWebTestCase
 */
public function createDatabase()
{
    // What can I do here?
}

As this is a PHPUnit test case, you can't use it directly in Behat. You'll need to replicate the behaviour in a Behat context.

Have a look at the SymfonyDoctrineContext from Behat/CommonContexts. 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.

I'm using LiipFunctionalTestBundle for Unit testing, it works very well.

If you're using the FunctionalTestBundle, then you're not writing unit tests. 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 BehatBehatContextContext;

  class ApiContext extends LiipFunctionalTestBundleTestWebTestCase 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/"
....
链接地址: http://www.djcxy.com/p/31086.html

上一篇: 使用repositoryItemTextEdit Devexpress xtragrid列设置掩码

下一篇: 如何在Symfony2中使用Behat和Liip \ FunctionalTestBundle?