12

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?

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
woodgate
  • 183
  • 1
  • 8
  • 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 Apr 15 '14 at 09:05
  • Here is a tutorial I made to do this: https://wp-bdd.com/multiple-environments/ . Environments can be anything, different browsers, different versions, different data etc. – John Dee Mar 14 '18 at 20:10

1 Answers1

15

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.

hidro
  • 12,333
  • 6
  • 53
  • 53
  • 1
    just to be clear, this goes AFTER the whole tree starting with modules:... enabled:... config... – dwenaus May 28 '14 at 02:48
  • Is it possible to test against multiple environments in one test? – AlxVallejo Jul 19 '16 at 13:58
  • @AlxVallejo, yes add additional --env arguments, like this: `codecept run testSuiteName --env local --env integration --env staging`. But if you want specific test, from test case. Then: `codecept run pathToTestFileWithExtension.php:testMethodName --env local --env integration --env staging` – juslintek Jan 10 '20 at 15:40