codeception

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


I see this sort of thing in Codeception YML files all the time:

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

How do I pass "URL" to Codeception from the command line? Or any other way!


Solution

  • It is documented at 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%"
    

    If you want to set it in command line, you can set it like this:

    URL=http://example.org codecept run
    

    or

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

    Examples above work with Bash, you may have todo something else if you use a different shell.