In the previous article, we tested the MODX
modUserCreate processor. In this one we'll get our first look at functional testing with Codeception.
This article assumes that you've installed and configured Codeception and PhpUnit. The test files and classes are available at GitHub here.
Functional testing is best explained in reference to acceptance testing (which we'll see in the following articles). In acceptance testing, the test platform acts as a kind of robot that launches a browser, travels to various pages of a web site, checks the content, fills and submits forms, clicks on links and buttons, and verifies the results. Functional testing does the same thing but with no browser.
Functional testing uses the platform modules provided by Codeception to do what might be called "pseudo-navigation". It mimics the actions that the browser and platform would take.
Codeception has modules for the Symfony2, Laravel5, Yii2, Zend Framework platforms and others. Unfortunately, there's no module for MODX, which is the platform we've been using, though there may be one by the time you read this.
Because there's no module for MODX, we'll have to imagine one (unless one exists). I considered writing a module for MODX, but after a couple dozen articles on software testing, I'm almost out of steam. Maybe later.
Imaginary MODX Module
Here are the most commonly used commands and tests in a platform module:
$I->amOnPage('/login'); $I->fillField('username', 'JoeTester'); $I->fillField('password', 'TesterPassword'); $I->click('LOGIN'); // or $I->submitForm('#login'); $I->see('Welcome, JoeTester!'); $I->seeElement('.notice'); $I->dontSeeElement('.error'); $I->seeLink('Logout');
In acceptance tests, the process above would launch a browser and actually perform the steps. In a functional platform module, we might use cURL to get the pages and write our own methods for each of the lines above. In MODX, we could speed that up by getting the page referenced by
amOnPage() directly from the database, integrate it with its template, and parse it with the MODX parser. We could do something similar with
$I->click(). We'd implement
fillField() by modifying the
$_GET array. We'd implement the
dontSee... with a string search or with an XPath parser.
Functional Tests Vs Acceptance Tests
Functional tests are more fragile and prone to error than acceptance tests. They're also more likely to generate false errors because of changes made to page code and CSS, and they're not very good at distinguishing visible from hidden elements. On the other hand, they are *much* faster.
Since Functional tests essentially test the same things as acceptance tests, which are much more reliable, many people skip functional testing and just create acceptance tests to make sure things work as they should.
In the following articles, we'll see acceptance testing in action.
In the next article, we'll take a look at acceptance testing in MODX with Codeception.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)