LifterLMS Unit Testing and CI for Add-ons: A Practical Setup

Set up LifterLMS unit testing for a custom add-on with the official lifterlms-tests framework: bootstrap, factories, a first test, testing against several LifterLMS and PHP versions, and a GitHub Actions workflow.

  • Add as a preferred source on Google
Illustration for “LifterLMS Unit Testing and CI for Add-ons: A Practical Setup”

If your business depends on a custom LifterLMS add-on, such as an enrollment rule, a gateway tweak or an integration with your CRM, every LifterLMS, WordPress and PHP update is a chance for it to break quietly. LifterLMS unit testing, run automatically on every change, is how you find out on a pull request instead of from a student.

This guide is for developers who maintain LifterLMS add-ons, in-house or for clients. It shows how to set up the official testing framework that LifterLMS itself uses, write a first meaningful test, run the suite against several versions, and wire it into GitHub Actions. Code and commands were checked against lifterlms/lifterlms-tests 4.5 and LifterLMS 10.3.

If you are still deciding whether your customisation should be a plugin at all, read when to build a custom LifterLMS plugin first.

The tools LifterLMS uses for testing

LifterLMS publishes its test tooling on Packagist so add-on developers can use the same setup as the core team:

  • lifterlms/lifterlms-tests is a testing framework built on PHPUnit and the WordPress core test suite. It provides an installer for the test environment, a bootstrap class that loads WordPress, LifterLMS and your add-on, a base test case with extra assertions, and factories for courses, memberships, orders, students and instructors. It also includes llms-env, a Docker-based local environment.
  • lifterlms/lifterlms-cs holds the LifterLMS coding standards for PHP_CodeSniffer, based on the WordPress Coding Standards.

The LifterLMS core plugin lists both as development dependencies in its composer.json, which is a good sign they will stay maintained.

Setting up LifterLMS unit testing in an add-on

1. Install the framework

composer require --dev lifterlms/lifterlms-tests

2. Add phpunit.xml.dist

Place it in the root of your add-on. The important parts are the bootstrap path and the test directory:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" verbose="true"
	convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true">
	<testsuites>
		<testsuite name="LLMSX Bonus Courses">
			<directory suffix=".php">tests/unit-tests</directory>
		</testsuite>
	</testsuites>
</phpunit>

3. Create tests/bootstrap.php

The bootstrap extends LLMS_Tests_Bootstrap and names your plugin's main file. By default it loads LifterLMS core before your add-on, so your code runs in a realistic environment.

<?php
/**
 * Test bootstrap for LLMSX Bonus Courses.
 */

require_once './vendor/lifterlms/lifterlms-tests/bootstrap.php';

class LLMSX_Bonus_Courses_Tests_Bootstrap extends LLMS_Tests_Bootstrap {

	public $file = __FILE__;

	public $suite_name = 'LLMSX Bonus Courses';

	public $plugin_main = 'llmsx-bonus-courses.php';

}

return new LLMSX_Bonus_Courses_Tests_Bootstrap();

4. Install the test environment

The installer downloads WordPress and its test library and creates a test database. It needs a MySQL server you can write to; use a throwaway database, because it is emptied on every run.

# Database name, user, password, host, WordPress version
./vendor/bin/llms-tests install llms_tests root password 127.0.0.1 latest

# Install LifterLMS into the test WordPress
./vendor/bin/llms-tests plugin lifterlms

# Run the suite
./vendor/bin/phpunit

The plugin command also accepts a version, which is how you test against an older LifterLMS release your customers still run. LifterLMS core wraps these commands in Composer scripts called tests-install and tests-run; copying that pattern into your add-on's composer.json saves typing.

Writing your first test

Here is a small add-on to test. When a student enrolls in a course that has a bonus course set in its meta, it enrolls them in the bonus course too:

<?php
/**
 * Plugin Name: LLMSX Bonus Courses
 */

defined( 'ABSPATH' ) || exit;

add_action( 'llms_user_enrolled_in_course', 'llmsx_enroll_bonus_course', 10, 2 );

function llmsx_enroll_bonus_course( $user_id, $course_id ) {
	$bonus_id = absint( get_post_meta( $course_id, '_llmsx_bonus_course', true ) );

	if ( $bonus_id && ! llms_is_user_enrolled( $user_id, $bonus_id ) ) {
		llms_enroll_student( $user_id, $bonus_id, 'llmsx_bonus_' . $course_id );
	}
}

And a test class for it, saved in tests/unit-tests/. Test classes extend LLMS_Unit_Test_Case, which gives you the LifterLMS factories on $this->factory:

<?php
/**
 * @group llmsx_bonus
 */
class LLMSX_Test_Bonus_Courses extends LLMS_Unit_Test_Case {

	public function test_enrolling_in_main_course_adds_bonus_course() {
		$main_id    = $this->factory->course->create();
		$bonus_id   = $this->factory->course->create();
		$student_id = $this->factory->student->create();

		update_post_meta( $main_id, '_llmsx_bonus_course', $bonus_id );
		llms_enroll_student( $student_id, $main_id );

		$this->assertTrue( llms_is_user_enrolled( $student_id, $bonus_id ) );
	}

	public function test_no_bonus_course_when_meta_is_missing() {
		$main_id    = $this->factory->course->create();
		$other_id   = $this->factory->course->create();
		$student_id = $this->factory->student->create();

		llms_enroll_student( $student_id, $main_id );

		$this->assertFalse( llms_is_user_enrolled( $student_id, $other_id ) );
	}
}

Each test runs inside a database transaction that is rolled back afterwards, so tests do not leak data into each other. Note that the test calls llms_enroll_student(), the same function the checkout uses, so the real llms_user_enrolled_in_course action fires and your hook runs exactly as it would in production.

What else the framework gives you

HelperWhat it is for
Factories for course, membership, order, student and instructorBuild realistic data quickly; the course factory can also generate sections, lessons and quizzes
llms_tests_mock_current_time() and llms_tests_reset_current_time()Test drip dates, access expiration and anything else that depends on "now"
mock_http_request()Short-circuit the next matching wp_remote_request(), essential for testing gateway and CRM integrations without real API calls
assertOutputContains() and related assertionsCheck template and shortcode output
WP_Error assertionsCheck error codes and messages from your validation code
mockGetRequest() and mockPostRequest()Simulate form submissions and query strings
LLMS_Unit_Test_Exception_Redirect and LLMS_Unit_Test_Exception_ExitTest code that redirects or exits, such as form handlers
LLMS_Unit_Test_Util::call_method()Call private and protected methods when you really need to

For REST endpoints, build a WP_REST_Request, dispatch it with rest_get_server()->dispatch(), and assert on the status and data. Set the current user with wp_set_current_user() first to test permissions for students, instructors and administrators separately.

What is worth testing in a LifterLMS add-on

  • Access decisions. Anything that enrolls, unenrolls, restricts or grants access. These bugs cost money or leak content.
  • Money. Price calculations, coupon rules, gateway responses and webhook handlers, with HTTP mocked.
  • Time. Drip, expiration, deadlines and reminder schedules, with the clock mocked.
  • Permissions. Every custom REST route and admin action, for each role that might call it.
  • Your use of LifterLMS hooks. If you rely on a filter's parameters, a test will catch it when they change.

You do not need to test LifterLMS itself. Test the behaviour your add-on adds, through the same public functions and hooks a real request would use. Our LifterLMS hooks and filters guide lists the ones add-ons rely on most.

Keeping the suite fast and readable

A slow suite stops being run. Mirror your plugin's structure in tests/unit-tests, one test class per class or feature, so failures point straight at the code. Create only the data each test needs: a course with no lessons is enough to test an enrollment hook. Use PHPUnit groups, like the @group llmsx_bonus tag above, to run one area with ./vendor/bin/phpunit --group llmsx_bonus while you work. When you fix a bug, write the failing test first, then the fix, so the same bug cannot return unnoticed after the next refactor.

Continuous integration with GitHub Actions

This workflow runs the suite on every push and pull request, against the oldest and newest PHP versions you support. LifterLMS 10.x requires PHP 7.4 or newer.

name: Tests

on: [ push, pull_request ]

jobs:
  phpunit:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: [ '7.4', '8.3' ]
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: password
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer
      - run: sudo apt-get update && sudo apt-get install -y subversion
      - run: composer install --no-interaction --prefer-dist
      - run: ./vendor/bin/llms-tests install llms_tests root password 127.0.0.1 latest
      - run: ./vendor/bin/llms-tests plugin lifterlms
      - run: ./vendor/bin/phpunit

Some practical additions once the basics work:

  • A LifterLMS version matrix. Add a matrix variable for the LifterLMS version and pass it to llms-tests plugin lifterlms, so you test the latest release and the oldest one you support.
  • A WordPress version matrix. Pass a specific version instead of latest to the install command.
  • Coding standards. Run PHP_CodeSniffer with the LifterLMS standard in a separate, faster job.
  • Paid add-on dependencies. If your code extends a paid LifterLMS add-on, install it in CI from a private package or a stored zip rather than committing it to a public repository.
  • A scheduled run. Add a weekly schedule trigger so a new LifterLMS release is tested even when nobody has pushed code.

The subversion step is there because the WordPress test library installer has traditionally used it; drop it if your runner already has it or the installer does not need it.

Common mistakes

  • Testing against a production database. The test installer empties its database. Always use a dedicated one.
  • Only testing the happy path. Test the student who is not enrolled, the expired plan and the failed payment.
  • Real HTTP calls in tests. They are slow, flaky and can hit live APIs. Mock them.
  • Pinning to one LifterLMS version forever. Your customers update; your CI should too.
  • No tests for permissions. A REST route that works is not the same as a route only the right people can call.

Frequently asked questions

Does LifterLMS have an official testing framework?

Yes. lifterlms/lifterlms-tests on Packagist is the framework used for LifterLMS core and add-ons. It builds on PHPUnit and the WordPress test suite.

Which PHPUnit version does it use?

Version 4.5 of the framework supports PHPUnit 7.5 or 9.5 and PHP 7.3 and newer, with Yoast PHPUnit Polyfills for compatibility.

Can I run the tests locally without installing MySQL?

Use the bundled llms-env tool, which generates a Docker Compose setup, or any local database server you already run.

Can I test code that extends a paid LifterLMS add-on?

Yes. Install the add-on into the test WordPress before running the suite, from a private package or a zip you are licensed to use, and load it in your bootstrap before your own plugin. Keep its files out of public repositories.

Should I write end-to-end browser tests too?

For checkout flows and complex front-end features, yes, a few. Keep most coverage in fast unit and integration tests and reserve browser tests for the paths that make money.

Next steps

Add the framework to one add-on this week and write tests for its single most important behaviour, usually the one that grants access or touches payments. Then switch on CI. If you have custom LifterLMS code with no tests and an update you are nervous about, our LifterLMS plugin development team can add a test suite and CI to existing add-ons, or build new ones with tests from day one. Tell us about your add-on.

Add LifterLMS Expert to your preferred sources

Add lifterlmsexpert.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

  • Add as a preferred source on Google
Free consultation

Prefer expert help over DIY?

Skip the trial and error. Get specialist LifterLMS help and ship faster.

Talk to a LifterLMS Expert Explore our services

Replies within one business day