Setting up wp-browser on MAMP for Mac to test a plugin
This is the documentation for version 3 of the project. The current version is version 4 and the documentation can be found here.
Setting up wp-browser with MAMP on Mac to test a plugin
- A Mac machine
- A working installation of MAMP.
- You should be able to create sites and visit them from your browser without issues.
- Composer installed and working on your terminal
PATH, you should be able to run
composer --versionat the terminal and see the version correctly.
The version of MAMP used in this tutorial is the free, non PRO, one. MAMP PRO provides more features, but the setup instructions should remain valid.
Install and configure MAMP
This walk-through starts after MAMP has been installed and is correctly running on the host machine; you can download MAMP from the site and follow the installation instructions.
In the context of this guide I'm assuming the "Document Root" directory is the default one, in the
If your document root lies elsewhere, replace the
/Applications/MAMP/htdocs path with the actual directory in each command.
Creating the databases and installing WordPress
Go to the
http://localhost/phpMyAdmin/ page and create two new databases:
wordpressis the database you will use for WordPress
testsis the database you will use for the tests
Unzip the the WordPress files into the
/Applications/MAMP/htdocs and head over to
http://localhost to install WordPress.
The database credentials for the installation are:
- Database name:
- Database user:
- Database password:
- Database host:
admin as administrator user name and
password as password for the administrator user.
Make sure you can visit the WordPress installation at
http://localhost and that you can correctly access the administration area at
Scaffolding the project folder
I'm assuming the scope of the development is to test the
The first step is to create the bare minimum code required to make the plugin show up among the available WordPress plugins.
Create the main plugin file in the WordPress installation plugins directory, in the
The plugin should now show up, activate and deactivate correctly, among the plugins listed in the WordPress installation at
Open a terminal window and navigate to the plugin directory and initialize the Composer project:
Composer will ask some questions to initialize the project, for the sake of this small guide the answers are not relevant.
Here is the
composer.json file generated by the above answers:
lucatume/wp-browser as a development dependency:
Composer installs any dependency binary file, an executable file, in the project
To check Codeception is correctly installed run this command:
Since wp-browser requires Codeception, there is no need to require Codeception explicitly as a development dependency.
Setting up wp-browser
For those that might get lost while trying to set up wp-browser for the first time the VVV context provides an excellent base to understand the process.
wp-browser needs to know:
- Where the WordPress installation files are located: they will be loaded in integration and "WordPress unit" tests.
- How to connect to the WordPress site "normal" database: this is the database that stores the data of the site I would see when visiting the local installation URL at
- How to connect to the database dedicated to the integration and "WordPress unit" tests: this database will be used to install WordPress during integration and "WordPress unit" tests.
Any test suite using a database should never run on a database containing data of any value; this means that your first step should be to backup the site database.
You can create a backup of the current site database contents using phpMyAdmin, at
http://localhost/phpMyAdmin/, under the "Export" tab:
At any moment you can re-import the site database dump using, again, phpMyAdmin, under the "Import" tab:
Bootstrapping and configuring wp-browser
After the backup is done it's time to bootstrap
wp-browser using its interactive mode:
The initialization guide will ask a number of questions.
In the screenshots below are the answers I used to configure
Below a complete list of each answer:
- I acknowledge wp-browser should run on development servers...
- Would you like to set up the suites interactively now?
- How would you like the acceptance suite to be called?
- How would you like the functional suite to be called?
- How would you like the WordPress unit and integration suite to be called?
- How would you like to call the env configuration file?
- What is the path of the WordPress root directory?
- What is the path, relative to WordPress root URL, of the admin area of the test site?
- What is the name of the test database used by the test site?
- What is the host of the test database used by the test site?
- What is the user of the test database used by the test site?
- What is the password of the test database used by the test site?
- What is the table prefix of the test database used by the test site?
- What is the name of the test database WPLoader should use?
- What is the host of the test database WPLoader should use?
- What is the user of the test database WPLoader should use?
- What is the password of the test database WPLoader should use?
- What is the table prefix of the test database WPLoader should use?
- What is the URL the test site?
- What is the email of the test site WordPress administrator?
- What is the title of the test site?
My Plugin Test
- What is the login of the administrator user of the test site?
- What is the password of the administrator user of the test site?
- Are you testing a plugin, a theme or a combination of both (both)?
- What is the folder/plugin.php name of the plugin?
- Does your project needs additional plugins to be activated to work?
Codeception will build the suites for the first time and should be ready to go.
Setting up the starting database fixture
A "fixture", in testing terms, is a minimal, starting environment shared by all tests.
In BDD it's the
Background any scenario will share.
In the case of a plugin the minimal, starting environment is the following:
- A fresh WordPress installation empty of any content.
- WordPress using its default theme.
- The only active plugin is the one you're testing, in this example:
You should set up this fixture "manually", using the site administration UI at
The following command will empty the site, backup any content you care about first!
When you're done setting up the initial database fixture, export it using the "Export" tab of phpMyAdmin, at
http://localhost/phpMyAdmin/ and move the file to the
There is one last step left to complete the setup.
Using the tests database in acceptance and functional tests
Acceptance and functional tests will act as users, navigating to the site pages and making requests as a user would.
This means that WordPress will load, and with it its
wp-config.php file, to handle the requests made by the tests.
During the setup phase I've specified the database to be used for
functional tests as
tests but, looking at the contents of the
/Applications/MAMP/htdocs/wp-config.php file, the
DB_NAME constant is set to
What we'll do now means:
- If the request is a normal one, use the
- If the request comes from a test, use the
In your IDE/text-editor of choice edit the
/Applications/MAMP/htdocs/wp-config.php and replace the line defining the
DB_NAME constant like this:
Here's the copy-and-paste friendly version:
If you look at the
tests/functional.suite.yml files, respectively the
functional suite configuration files, you will see these entries in the
WPBrowser module configuration:
This means that, with each HTTP request done during tests, the module will send the two headers.
Those headers are read, on the WordPress side, using the
Before starting to write tests, take a moment to run each suite separately and make sure all is set up correctly.
If you run into issues, there's a chance you forgot something along the way, please take the time to read this tutorial a second time before opening an issue.
You have created 4 suites, each suite has at least one example test to make sure all works.
Run each suite and make sure all tests succeed, from within the box run:
You're now run to customize the suites to your liking or start writing tests, run
vendor/bin/codecept to see a list of the available commands.