优化behat测试套件

Ben*_*der 2 selenium symfony behat mink

我有一个测试套件,它有20个feaure文件和100%MySQL CRUD操作正在执行.完成大约需要5分钟.如果我手动进行测试,最多需要7分钟.我需要知道的是,为了优化整个过程,我需要做些什么?

注意:Behat 3 不支持 ParallelRunnder ,因此现在超出范围!

如果您打算使用Behat 3,那么请帮我修改我的composer.json和behat.yml文件,因为当我自己运行并运行bin/behat时,我会收到如下错误:

`Behat\Symfony2Extension\Extension` extension file or class could not be located.
`Behat\MinkExtension\Extension` extension file or class could not be located. 
Unrecognized options "mink_driver" under "testwork.symfony2"
Unrecognized options "context, paths" under "testwork" 
Run Code Online (Sandbox Code Playgroud)

如下所示,我使用的是某些版本号,没有星号.

CURRENT composer.json:

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.5.4",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0",
    "sensio/framework-extra-bundle": "~3.0",
    "incenteev/composer-parameter-handler": "~2.0",
    "behat/behat": "2.5.3",
    "behat/behat-bundle": "1.0.0",
    "behat/symfony2-extension": "1.1.2",
    "behat/mink": "1.5.0",
    "behat/mink-extension": "~1.3",
    "behat/mink-selenium2-driver": "1.1.1",
    "behat/mink-goutte-driver": "1.0.9"
},
Run Code Online (Sandbox Code Playgroud)

CURRENT behat.yml:

default:
    context:
        class: FeatureContext
        parameters:
            output_path: %behat.paths.base%/build/behat/output/
            screen_shot_path: %behat.paths.base%/build/behat/screenshot/
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat\MinkExtension\Extension:
            base_url: 'http://symfony.local/app_test.php/'
            files_path: %behat.paths.base%/build/dummy/
            javascript_session: selenium2
            browser_name: firefox
            goutte: ~
            selenium2: ~
    paths:
        features: %behat.paths.base%/src
        bootstrap: %behat.paths.features%/Context
Run Code Online (Sandbox Code Playgroud)

编辑:

我有20个功能文件,每个都有一个方案.对于CRUD操作:

  • 我在每个场景中都运行了登录方法.查询针对DB运行.
  • 有些场景是INSERT,有些是UPDATE,有些是DELETE.
  • 我用的pdo_mysqldatabase_driver

我的FeatureContext文件的一部分:

namespace Site\CommonBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Process;

/**
 * Class FeatureContext
 *
 * Parent to other FeatureContext files. It is used to avoid duplicated codes and all the
 * shared commons are kept here.
 *
 * @package Site\CommonBundle\Features
 */
class FeatureContext extends MinkContext implements KernelAwareInterface
{
    protected $kernel;
    protected $screenShotPath;
    protected $outputPath;

    /**
     * Parameter $parameters comes from behat.yml file.
     * @param array $parameters
     */
    public function __construct(array $parameters)
    {
        $this->outputPath = $parameters['output_path'];
        $this->screenShotPath = $parameters['screen_shot_path'];
    }

    /**
     * Helps to use doctrine and entity manager.
     * @param KernelInterface $kernelInterface Interface for getting Kernel.
     */
    public function setKernel(KernelInterface $kernelInterface)
    {
        $this->kernel = $kernelInterface;
    }

    /**
     * Without this, PhantomJs will fail if responsive design is in use.
     * @BeforeStep
     */
    public function resizeWindow()
    {
        $this->getSession()->resizeWindow(1440, 900, 'current');
    }

    /**
     * Take screen-shot when step fails. Works only with Selenium2Driver.
     *
     * @AfterStep
     * @param $event Current event.
     * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
     */
    public function takeScreenshotAfterFailedStep($event)
    {
        if (4 === $event->getResult()) {
            $driver = $this->getSession()->getDriver();

            if (! ($driver instanceof Selenium2Driver)) {
                throw new UnsupportedDriverActionException(
                    'Taking screen-shots is not supported by %s, use Selenium2Driver instead.',
                    $driver
                );

                return;
            }

            if (! is_dir($this->screenShotPath)) {
                mkdir($this->screenShotPath, 0777, true);
            }

            $filename = sprintf(
                '%s_%s_%s.%s',
                $this->getMinkParameter('browser_name'),
                date('Ymd') . '-' . date('His'),
                uniqid('', true),
                'png'
            );

            file_put_contents($this->screenShotPath . '/' . $filename, $driver->getScreenshot());
        }
    }

    /**
     * @When /^I login as "([^"]*)"$/
     * @param $type User role type.
     */
    public function iLoginAs($type)
    {
        $container = $this->kernel->getContainer();
        $userData = $container->getParameter('dummy_user');

        $this->visit('/login');
        $this->fillField('username', $userData[$type]['username']);
        $this->fillField('password', $userData[$type]['password']);
        $this->pressButton('_submit');
    }
    .........
}
Run Code Online (Sandbox Code Playgroud)

Ian*_*hek 8

我不知道为什么我认为它需要20分钟才能运行,可能与功能数量相混淆.5分钟也不错.您可以做一些基本的事情来帮助加快速度.

  1. @BeforeStep你内心的逻辑- 你可能会把它移入@BeforeScenario甚至进入@BeforeFeature甚至进入@BeforeSuite- 没有必要经常这样做.
  2. 非常明显,但以防万一:Goutte驱动程序与其他驱动程序相比速度惊人,不利的是它不支持JS并且你无法截取屏幕截图.但我认为Symfony应用程序并不涉及很多JS来做CRUD操作.因此,仔细检查所有不涉及JS的内容并将其与Goutte一起使用.
  3. iLoginAs可能在每一步都使用.您可以通过手动创建用户会话并将cookie设置为标题来更快地更新它,即,您在步骤定义中使用与您的身份验证方法相同的逻辑,然后您只需执行$this->getSession()->getDriver()->setCookie(session_name(), session_id());- 在下一个请求中您的用户是已经过身份验证.
  4. browser_name: firefox- 上帝不...使用chrome,它必须更快.
  5. 使用HHVM运行Behat测试(应用程序仍使用旧的优秀PHP)将大大提高性能.安装它可能会很痛苦,特别是在Mac上.
  6. 最后,迁移到Behat 3,这根本不应该是痛苦的.他们从2.x中学到了很多知识并用它来改进它,包括性能方面.它在配置上也更灵活,允许多个上下文使事情更有条理,更清晰.


Ben*_*der 5

在实施上述所有建议后,我将在这里发布结果,以供其他人查看他们的效果,因此每次我介绍一个步骤时,我都会更新这篇文章。

原定时间:

Total time: 4 minutes  12.55 seconds
Run Code Online (Sandbox Code Playgroud)

实施步骤 1 后的时间:

Total time: 2 minutes  8.01 seconds
Run Code Online (Sandbox Code Playgroud)

注意:只有 @BeforeScenario 似乎可以工作,因为在 OOP 中不允许在静态方法中访问 $this 键,因为 @BeforeFeature 和 @BeforeSuite 要求 resizeWindow() 是静态的。如果可以的话,结果会快得多。

实施步骤 2 后的时间:

注意:请阅读下面的步骤 6。

实施步骤 3 后的时间:

注意:我是 Symfony2 用户,不幸的是未能实现这一点。

实施步骤 4 后的时间:

Total time: 1 minutes  54.11 seconds
Run Code Online (Sandbox Code Playgroud)

注意:Behat 3 用户 - 运行 seleniumjava -jar selenium-server-standalone-2.43.1.jar -Dwebdriver.chrome.driver="chromedriver"

实施步骤 6 后的时间:

Total time: 0 minutes  52.04 seconds
Run Code Online (Sandbox Code Playgroud)

注意:Symfony2 用户 - 如果您设置default_sessionsymfony2,与 goutte 和 selenium2 相比,它的出血速度很快。Goutte:1 分 20.03 秒,Selenium2:1 分 31.00 秒