10up/wp-codeception

Name: wp-codeception

Owner: 10up

Description: Integrates Codeception framework into WordPress and allows run tests using WP CLI command.

Created: 2015-03-28 12:50:44.0

Updated: 2017-11-20 06:05:21.0

Pushed: 2017-11-08 19:06:22.0

Homepage:

Size: 187

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

WP Codeception

This is a WordPress Plugin which integrates with the Codeception PHP testing framework and allows you to write and run Codeception tests for WordPress via WP CLI.

We're working towards supporting all of Codeceptions commands. If you find one we haven't included yet, please submit a Pull Request!

Installation

Download the latest version and extract, or clone the repository with Git into a new directory wp-content/plugins/wp-codeception in your WordPress install.

Install required node modules and composer dependencies

We'll run our commands from within VVV because WP CLI, Node, and Composer are already installed for us there.

grant up
grant ssh
do apt-get install openjdk-7-jre-headless
 /srv/www/yoursite/htdocs/wp-content/plugins/wp-codeception
mposer install
 plugin activate wp-codeception

Afterwards you'll have a new vendor directory within your plugins/wp-codeception directory which contains all the code libraries we're dependant on.

Install as a composer dependency

There is an alternative way to install this plugin. You can add it as a composer dependency for your project. To do it, run following command in your terminal:

mposer require 10up/wp-codeception

This command will install the plugin and all its dependencies for your project. Please, pay attention that if you already use composer/installers dependency in your project, then wp-codeception will be installed into <PROJECT_DIR>/wp-content/plugins/wp-codeception/ folder. It happens, because wp-codeception has wordpress-plugin type and will be processed by composer/installers accordingly (read its documentation for more details).

If you want to add it as a dependency to your plugin or theme, you will need to update your composer.json file and tell it where to install wp-codeception. You can achieve it by providing installer-paths instructions like in the snippet below.


...,
"extra": {
    "installer-paths": {
        "vendor/{$name}/": ["type:wordpress-plugin"]
    }
},
...

Now composer/installers will know to install wordpress plugins into vendor folder. The final step which you need to do is to update your autoload section and add wp-codeception.php file to the autoload files list.


...,
"autoload": {
    "psr-X": {
        ...
    },
    "files": [
        ...,
        "vendor/wp-codeception/wp-codeception.php"
    ]
},
...

Install the test suite

See the Codeception bootstrapping documentation for further information.

u'll create the test suite in your own plugin or theme directory..
 /srv/www/yoursite/htdocs/wp-content/{your plugin or theme directory}
 codeception bootstrap

Afterwards you'll have a new tests directory within your plugin or theme directory. This is your new test suite, and where you'll write all your tests.

Writing Tests

You can write tests using any of the three Codeception testing frameworks: Acceptance, Functional and Unit testing. If you look within the new tests directory you'll see three config files; one for each test framework (acceptance.suite.yml, functional.suite.yml, unit.suite.yml). Edit these files as you wish.

Generate your first test
u should be in the plugin or theme directory where you ran the bootstrap
 codeception generate-(cept|cest) (acceptance|functional|unit) MyTestName

ample
 codeception generate-cept acceptance LoginTest

Afterwards you'll have a new file in your plugin or theme directory tests/acceptance/LoginTest.php, where you can write your first test. Remember, any Codeception test will work here! For example, you could run any of the acceptance test examples mentioned in the Codeception documentation. Likewise, the same goes for Functional and Unit tests.

Example: Writing a Login Acceptance Test
p

ake sure you've added your site URL to acceptance.suite.yml
see http://codeception.com/docs/03-AcceptanceTests#PHP-Browser
 new AcceptanceTester( $scenario );
wantTo( 'Ensure WordPress Login Works' );

et's start on the login page
amOnPage( wp_login_url() );

opulate the login form's user id field
fillField( 'input#user_login', 'YourUsername' );

opupate the login form's password field
fillField( 'input#user_pass', 'YourPassword' );

ubmit the login form
click( 'Log In' );

alidate the successful loading of the Dashboard
see( 'Dashboard' );
Running Your Tests

Now you've written some tests, it's time to run them! But first..

Selenium

If you've created any browser automation/acceptance tests you'll need to turn Selenium on, and likewise, you'll want to stop Selenium after you're through running tests.

u can run these commands from anywhere in your WordPress install
 selenium start

op Selenium when you're through
 selenium stop
Run

You'll use the run command to execute your tests from within your plugin or theme directory (where you ran the bootstrap). We've implemented most of the Codeception 'run' command arguments, but if you find one we've missed please submit a Pull Request!

u should be in the plugin or theme directory where you ran the bootstrap
 codeception run
Example: Running our Login Test
u should be in the plugin or theme directory where you ran the bootstrap
t's display verbose output
 codeception run -vvv

ception PHP Testing Framework v2.0.11
red by PHPUnit 4.5.1 by Sebastian Bergmann and contributors.

building AcceptanceTester...

ptance-production Tests (1) ---------------------------------
les: WebDriver, WordPress, AcceptanceHelper
-------------------------------------------------------------
re WordPress Login Works (LoginTest)
ario:
am on page "http://site.com/wp-login.php"
fill field "input#user_login","YourUsername"
fill field "input#user_pass","YourPassword"
click "Login"
see "Dashboard"
SED

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.