plpgunit started out of curiosity on why a unit testing framework cannot be simple and easy to use. Plpgunit does not require any additional dependencies and is ready to be used on your PostgreSQL Server database.

Visit Repository
How to Create a Unit Test?

A unit test is a function which must :

  • not have any arguments.
  • always return "test_result" data type.

A simple plpgunit test:

DROP FUNCTION IF EXISTS unit_tests.check_vacuum_analyze();

CREATE FUNCTION unit_tests.check_vacuum_analyze()
RETURNS test_result
    DECLARE message test_result;

    IF NOT EXISTS(SELECT MAX(last_vacuum) FROM pg_stat_user_tables HAVING MAX(last_vacuum) > NOW() - interval '24 hours') THEN
        SELECT'Vacuum was not run since last 24 hours.') INTO message;
        RETURN message;
    END IF;

    IF NOT EXISTS(SELECT MAX(last_analyze) FROM pg_stat_user_tables HAVING MAX(last_analyze) > NOW() - interval '24 hours') THEN
        SELECT'Analyze was not run since last 24 hours.') INTO message;
        RETURN message;
    END IF;

    SELECT assert.ok('End of test.') INTO message;  
    RETURN message; 
LANGUAGE plpgsql;

/*Use transaction if your test contains DML query.*/
SELECT * FROM unit_tests.begin();
Please Note
  • A unit test is nothing but a plpgsql function which returns "test_result" data type.
  • plpgunit test is as easy as writing a plpgsql function. Your job is to fail the test, we give you tools to do that!
  • When you create a Plpgunit test, it will not be tested, just only saved.
  • You can invoke a test by its name. I would only do that when I'm writing a test function. So, when I'm done writing a test, I would rather invoke all the tests to see what would the result be. This helps me concentrate on one thing at a time, and everything later.
  • You can invoke all tests with a simple three-line query, which is always the same, easy to remember.
Visit Repository