12
votes

I am working on some Unit Tests for an API using Codeception. The idea is to make sure that each API call returns the expected response codes and a JSON object in a desired format.

The problem that I have is that I need to use different URLs depending on whether the server is localhost, the test server or the production one.

I can't use the values of $_SERVER['SERVER_NAME'] because the tests are not ran through the web browser.

Here http://codeception.com/docs/07-AdvancedUsage#Environments they explain that some environments can be set by modifying the configuration file. The documentation doesn't explain how to modify the configuration file to use it within your own unit tests.

Id like to set some environments like local, test, production and then, inside my unit test classes, to know what URLs to use. Each environment would have different URLs.

I've read the documentation but I can't find the way to do it.

Do you know any way to achieve what I need?

1
Agree, documentation is not clear on if the environment configuration is suite-specific, albeit since sub-configs are merged with the parent config (codeception.yml), it should be possible to configure environments in parent config as well. - Motin
Here is a tutorial I made to do this: wp-bdd.com/multiple-environments . Environments can be anything, different browsers, different versions, different data etc. - John Dee

1 Answers

14
votes

Make sure that your codeception version is at least 1.8, as environment is only supported from 1.8.

Below is our setup for codeception, specified in api.suite.yml, with PhpBrowser and REST modules enabled for BDD and API testings:

...
env:
    local:
        modules:
            config:
                PhpBrowser:
                    url: http://local.example.com/
                REST:
                    url: http://local.example.com/v1/
    integration:
        modules:
            config:
                PhpBrowser:
                    url: http://integration.example.com/
                REST:
                    url: http://integration.example.com/v1/
    staging:
        modules:
            config:
                PhpBrowser:
                    url: http://staging.example.com/
                REST:
                    url: http://staging.example.com/v1/

When running codecept command, you need to provide an --env option to specify which environment the tests should use.