Your first WordPress Codeception “cest” unit test



With WordPress Codeception, you can run unit tests, called Cest or Test tests, that use the format:

  • [test-name]Cest.php
  • [class-name]Test.php

The first step, as in any TDD cycle, is to create a failing test. This test is going to fail, because the feature doesn’t exist yet. Once the feature exists and works properly, the test will pass, and we’ll begin a new cycle.

When we run Codeception with the command:

  • bin/codecept run tests/unit/ShortcodeHelloWorldTest.php -vvv --html

The test fails.

We now add code to make the test pass. You can add this anywhere, for instance in a plugin.

  • bin/codecept run tests/unit/ShortcodeHelloWorldTest.php -vvv --html

The test passes!

Now it’s time to re-factor. That is, we modify the code to make it easier to read and understand, but we don’t add or change any functionality.
I’ve added namespaces and re-done the plugin in OOP style. The entire plugin code can be found here on Github.

Leave a Reply

Your email address will not be published. Required fields are marked *