在Symfony 4.x中,您可以使用Behat的上下文功能来定义不同的测试环境。下面是一个解决方案的示例代码:
kernel = $kernel;
}
/**
* @BeforeScenario
*/
public function setup()
{
// 从.env文件中加载环境变量
$dotenv = new Dotenv();
$dotenv->load($this->kernel->getProjectDir().'/.env');
// 获取当前测试环境
$this->env = $this->kernel->getEnvironment();
}
/**
* @Given /^I am in the "([^"]*)" environment$/
*/
public function iAmInTheEnvironment($environment)
{
if ($this->env !== $environment) {
throw new \RuntimeException("Current environment is not $environment");
}
}
}
Feature: Testing different environments
In order to test different environments
As a Behat user
I want to be able to switch between environments
Scenario: Testing production environment
Given I am in the "prod" environment
# Add your test steps here
Scenario: Testing development environment
Given I am in the "dev" environment
# Add your test steps here
这样,您就可以根据需要在不同的测试环境中运行Behat测试。请确保在.env文件中正确配置测试环境所需的环境变量。
上一篇:Behat容器不断退出