我有一个测试套件,它有20个feaure文件,正在执行100%的MySQL CRUD操作。完成大约需要5分钟。如果我手动进行测试,最多需要7分钟。我需要知道的是,为了优化整个过程,我需要做些什么?
注意:Behat 3的ParallelRunnder is not supported因此现在不在范围内!
如果您打算使用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"
如下所示,我使用的是某些版本的数字,没有星号。
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"
},
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
修改:
我有20个功能文件,每个都有一个方案。对于CRUD操作:
pdo_mysql
作为database_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');
}
.........
}
答案 0 :(得分:8)
我不知道为什么我认为它需要20分钟才能运行,可能与功能的数量相混淆。 5分钟也不错。你可以做一些基本的事情来加快速度。
@BeforeStep
内的逻辑 - 您可能会将其移至@BeforeScenario
或甚至移至@BeforeFeature
或甚至移至@BeforeSuite
- 无需执行此操作经常这样。iLoginAs
可能在每一步都使用。您可以通过手动创建用户会话并将cookie设置为标题来更快地更新它,即,您在身份验证方法中使用与步骤定义相同的逻辑,然后您只需执行$this->getSession()->getDriver()->setCookie(session_name(), session_id());
- 下一个请求您的用户已经过身份验证。browser_name: firefox
- 上帝不...使用chrome
,它必须更快。 答案 1 :(得分:3)
在实施上述所有建议之后,我会在此处发布结果,以了解它们是如何制定出来的,所以每次我介绍一步我都会更新此帖子。
原始时间:
Total time: 4 minutes 12.55 seconds
实施第1步后的时间:
Total time: 2 minutes 8.01 seconds
注意:只有@BeforeScenario似乎正在工作,因为在OOP中不允许在静态方法中访问$ this键,因为@BeforeFeature和@BeforeSuite要求resizeWindow()是静态的。如果可能的话,结果会快得多。
执行第2步后的时间:
注意:阅读下面的第6步。
实施第3步后的时间:
注意:我是Symfony2用户,不幸的是没有实现这一点。
执行第4步后的时间:
Total time: 1 minutes 54.11 seconds
注意:Behat 3个用户 - 使用java -jar selenium-server-standalone-2.43.1.jar -Dwebdriver.chrome.driver="chromedriver"
运行selenium
实施第6步后的时间:
Total time: 0 minutes 52.04 seconds
注意:Symfony2用户 - 如果将default_session
设置为symfony2
,与goutte和selenium2相比,它会快速流血。 Goutte:1分20.03秒,Selenium2:1分31.00秒