acseo / behat-generator-bundle
此Bundle允许您为项目中的每个路由生成.feature文件。
Requires
- php: >=5.3.2
- symfony/symfony: >=2.7.0
This package is auto-updated.
Last update: 2024-08-29 04:09:10 UTC
README
介绍
此Bundle允许您为项目中的每个路由生成.feature文件。
安装
Behat
您不需要behat来生成测试,但需要运行它们。要在项目中添加behat,可以按照以下步骤进行: http://docs.behat.org/en/v3.0/cookbooks/1.symfony2_integration.html
$ composer require --dev behat/behat
$ composer require --dev behat/mink-extension
$ composer require --dev behat/mink-zombie-driver
然后您的behat.yml
文件应如下所示
# behat.yml
default:
extensions:
Behat\MinkExtension:
base_url: "https://:8000/"
sessions:
default:
zombie: ~
javascript:
zombie: ~
suites:
default:
contexts:
- FeatureContext: { container: "@service_container" }
您需要定义一个FeatureContext类
<?php
// features/bootstrap/FeatureContext.php
use Behat\MinkExtension\Context\MinkContext;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext
{
/**
* 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($container)
{
$this->container = $container;
}
/**
* Some forms do not have a Submit button just pass the ID
*
* @When I Submit the form with id :arg1
*/
public function iSubmitTheFormWithId($arg1)
{
// https://jsfiddle.net/v3rv47hL/
$jsTemplate = <<<EOT
"var forms = document.getElementsByTagName('form');var search = '%s';var x = null;if (forms.length == 1) {x = forms[0];} else { for (var i = 0; i < forms.length; i++) { if (forms[i].getAttribute('class') == search || forms[i].getAttribute('id') == search || forms[i].getAttribute('name') == search) { x = forms[i]; break; } } } if (x != null) x.submit();"
EOT;
$js = sprintf($jsTemplate, $arg1);
$node = $this->getSession()->getPage()->find('named', array('id', $arg1));
if($node) {
$this->getSession()->executeScript($js);
} else {
throw new Exception('No form with id : '.$arg1.' found on this page');
}
}
}
Zombie
我们将使用Zombie.js作为浏览器模拟器。为什么?因为它允许我们在JS测试后检查状态码。
安装文档可以在以下位置找到:http://mink.behat.org/en/latest/drivers/zombie.html
- 按照官方网站的说明安装node.js:https://node.org.cn/。
- 按照https://npmjs.net.cn/的说明安装npm(node包管理器)。
- 使用npm安装zombie.js
$ npm install -g zombie
BehatGeneratorBundle
将bundle包含到您的项目中
$ composer require acseo/behat-generator-bundle
在AppKernel.php中启用bundle
// app/AppKernel.php
// ...
public function registerBundles()
{
$bundles = array(
// ...
new ACSEO\Bundle\BehatGeneratorBundle\ACSEOBehatGeneratorBundle(),
// ...
);
就这样!现在命令php app/console acseo:automatic-test
已经准备好了。
使用方法
您可以使用命令app/console acseo:automatic-test
来生成feature文件。
如果您这样做,命令很可能检测到需要登录的保护路由。它将提示您许多将使用的信息,例如登录url、用户名和密码等。
如果您想避免提示,可以使用有用的信息调用命令
$ app/console acseo:automatic-test --access "main uri /login" --access "main loginField username " --access "main passwordField password" --access "main submitField Login" --access "main login test@test.com" --access "main password test"
自定义。
待定。
它是如何工作的?
- 第一步:获取所有路由并过滤,仅保留接受GET且没有参数的路由
- 第二步:生成一个简单的测试:获取URL并检查是否返回200状态码。
- 第三步:使用自定义事件来生成视图,检查它是否包含FormView参数。如果是,生成一个简单的测试:提交视图中的表单并检查是否返回200状态码。
API自动生成测试
当使用API时,通常不需要自定义测试。知道在常规条件下一切正常就足够了。在这种情况下,您可以使用Acseo为API自动生成的测试。它将为项目中的每个API资源在features/automatic/api/目录中创建一个behat功能测试。也就是说,创建资源、获取资源、获取所有资源、编辑一个资源、删除一个资源(如果未在API中打开操作,则某些步骤可能不存在)。所有这些测试都将在一个临时的SQLite数据库中运行,因此您的真实数据库不会受到影响。显然,如果一个资源没有创建新项的路由,则无法编写测试(控制台会警告您),您需要自己创建测试。
目前,它仅适用于使用NelmioApiDoc的API,我们使用它来获取编写测试所需的所有元素(路由、方法、要发送的参数)。
您还需要这些依赖项,如果尚未添加,请在 composer.json
中添加它们
- "behat/mink-extension": "~2.0"
- "behat/mink-browserkit-driver": "~1.1"
- "behatch/contexts": "dev-master"
- "fzaninotto/faker": "~1.6.0"
然后在 behat.yml
中添加我们特定的功能
default:
...
suites:
default:
contexts:
- ACSEO\Bundle\BehatGeneratorBundle\Context\ApiContext: { container: "@service_container" }
- ACSEO\Bundle\BehatGeneratorBundle\Context\ExtendedMinkContext
- ACSEO\Bundle\BehatGeneratorBundle\Context\ParallelContext: { container: "@service_container"
要生成这些测试,只需在您的终端中运行此命令:app/console acseo:automatic-api-test
您将看到所有生成的功能。
然后,如果一切正常
$ bin/behat
如果您需要认证,请将其添加到您的 config_test.yml
# app/config/config_test.yml
parameters:
ACSEOBehatGeneratorBundle:
entity_manager: doctrine.orm.entity_manager # Put your own entity manager
authentication:
user:
class: \AcmeBundle\Entity\User # Put your own class for user
attributes: # Enter value to fill out on user creation
username: user
email: user@test.com
plainPassword: test
enabled: true
route: # Define the route
url: /login_check
parameters: # And parameters to send
_username: user.username
_password: user.plainPassword
许可证
此软件包处于MIT许可证之下。请参阅软件包中的完整许可证
Resources/meta/LICENSE