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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With