如何从命令行将环境变量传递给 Codeception YML 文件?

How do I pass environment variable to Codeception YML file from command line?

我经常在 Codeception YML 文件中看到这种东西:

modules:
    enabled:
        - PhpBrowser:
            url: '%URL%'

如何从命令行将 "URL" 传递给 Codeception?或者任何其他方式!

记录在 https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters

Modules can be dynamically configured from environment variables. Parameter storage should be specified in the global codeception.yml configuration inside the params section. Parameters can be loaded from environment vars, from yaml (Symfony format), .env (Laravel format), ini, or php files.

Use the params section of the global configuration file codeception.yml to specify how to load them. You can specify several sources for parameters to be loaded from.

Example: load parameters from the environment:

params:
    - env # load params from environment vars

Example: load parameters from YAML file (Symfony):

params:
    - app/config/parameters.yml

Example: load parameters from php file (Yii)

params:
    - config/params.php

Example: load parameters from .env files (Laravel):

params:
    - .env
    - .env.testing

Once loaded, parameter variables can be used as module configuration values. Use a variable name wrapped with % as a placeholder and it will be replaced by its value.

Let’s say we want to specify credentials for a cloud testing service. We have loaded SAUCE_USER and SAUCE_KEY variables from environment, and now we are passing their values into config of WebDriver:

modules:
   enabled:
      - WebDriver:
         url: http://example.com
         host: '%SAUCE_USER%:%SAUCE_KEY%@ondemand.saucelabs.com'

Parameters are also useful to provide connection credentials for the Db module (taken from Laravel’s .env files):

modules:
    enabled:
        - Db:
            dsn: "mysql:host=%DB_HOST%;dbname=%DB_DATABASE%"
            user: "%DB_USERNAME%"
            password: "%DB_PASSWORD%"

如果想在命令行中设置,可以这样设置:

URL=http://example.org codecept run

export URL=http://example.org
codecept run

以上示例适用于 Bash,如果您使用不同的 shell。

,您可能需要做其他事情