【问题标题】:Php unit testing pass config file from commandphp 单元测试通过命令传递配置文件
【发布时间】:2019-01-17 16:08:52
【问题描述】:

我正在使用单元测试来实现一些集成测试。我正在使用setUp() 方法来设置配置文件。我的配置文件包含一些 Api keys API secret 等。这很好用。

public function setUp()
{
    $this->_pipeline = new Pipline($this);
    $this->_pipeline->setConfig(\Api\Models\ConfigModel::createFromFile(__DIR__ . '/test/integration/assets/config'));
    $users = $this->_pipeline->users->listUsers();
    $this->_userId = $users->result->data[0]->user_id;

    parent::setUp(); 
}

这种方法效果很好。

问题 但我想从命令传递配置文件。目前我正在使用这个命令

php ./vendor/phpunit/phpunit/phpunit -c ./test/integration/phpunit.xml

有没有办法我可以通过类似这样的命令传递这个

php ./vendor/phpunit/phpunit/phpunit -c ./test/integration/phpunit.xml -c2 /test/integration/assets/config

提前致谢。

【问题讨论】:

  • 您可以从定义的环境变量中获取它,这些也可以在您的 phpunit.xml (<env name="foo" value="bar"/>) 中设置

标签: php unit-testing integration-testing


【解决方案1】:

您可以将名称作为参数传递给 php:

php ./vendor/phpunit/phpunit/phpunit -c ./test/integration/phpunit.xml -- /test/integration/assets/config

您可以通过$argc$argv 访问参数; $argv[0] 是脚本的名称,因此您的配置文件名将在$argv[1] 中。

例如

E:\>php -r "echo \"$argc\n\"; print_r($argv);" -- "hello world!"
2
Array
(
    [0] => -
    [1] => hello world!
)

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多