Unit Testing III - Our First Unit Test

A HelloWorld unit test to make sure everything works

In the previous article in this series, we looked at running unit tests from the command line. In this one, we'll create and run our first unit test.

MODX logo

Naming Conventions

We're going to create a number of tests in this series and a number of versions of our class files. To help keep them straight, I'll prefix the test files with T0_, T1_, T2_, T3_, etc. The class files will have matching numbers like this: user0.class.php, user1.class.php, etc. That way, I can put a reference in each article that lets you know which test file and which class file the article is about. Another reason for doing this is so that you can run all your unit tests at the same time without errors caused by re-declaring a User class.

MODX 3 had not been released when I wrote these articles. Some of the class files have been moved in that version, so some of the tests will have to have the paths to files corrected for that version. This shouldn't affect any of the unit tests since we're creating our own classes. It will, however affect the integration and acceptance tests we'll see much later in this series of articles.

The code for the tests and the classes will be available at my TestingProject GitHub repository. The numbering will also help you find the code if you want to cut and paste some of it.

This organization should make things more convenient for people who follow the articles, but you should know that it's not typically how you would develop a class. Usually, you would have multiple test files, named for what they do. Each test file might contain several different individual functions that each test something. But generally you'd only have one version of any class files (e.g., user.class.php). The various versions of the file would be handled by some version control system like Git.

Our First Unit Test

Our first test will run unit tests for a User class called user.class0.php (which we haven't created yet). In the _build directory, type this command:

codecept generate:test unit T0_Platform

In that command, "unit" is the name of the suite, and "User" is the name of the test. Now, in the unit suite (directory), you should see our test file: T0_PlatformTest.php. Notice that Codeception has added the suffix "Test" and the ".php" extension. It always does this.

The T0_PlatformTest File

Edit the T0_PlatformTest.php file. You should see this at the top: class T0_PlatformTest extends \Codeception\Test\Unit. This is a standard Codeception unit test class. It's not the name of the class we'll be testing, it's the name of the class that's going to be *doing* the testing.

You should also see empty _before() and _after() methods. These will be run automatically before and after each test. These are the equivalent to (actually, wrappers for) PhpUnit's setUp() and tearDown() methods. You'll also see an empty testSomeFunction() method. Important: The name of each method is arbitrary, but all test methods should start with the word "test." Change the name of that testSomeFunction() method to testWorking() and add this code inside it:


Save the file. Now, in the _build directory, run this command:

codecept run unit T0_PlatformTest

You should see this output at the top:

Time: 400 ms, Memory: 10.00MB

There was 1 failure:

1) T1_PlatformTest: Working
 Test  tests\unit\T1_PlatformTest.php:testWorking
Failed asserting that false is true.
#1  C:\xampp\htdocs\addons\assets\mycomponents\testingproject\_build\tests\unit\T1_PlatformTest.php:21
#2  T1_PlatformTest->testWorking

Tests: 1, Assertions: 1, Failures: 1.

The test failed (as it should, since false is not true), and the error occurred on line 21 of the T0_PlatformTest.php file.

Now, change the code in the test to the following and run the command again:


You should see a similar report, but with this at the end:

OK (1 test, 1 assertion)

If that all went as planned, you've written your first successful unit test. We'll make it more of a real test in the following article.

The code for this test, in its final form, is available here

Coming Up

In the next article, we'll expand our test to perform actual tests on a user class.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

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.)

Comments (0)

Please login to comment.