plpgunit

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 :

A simple plpgunit test:

DROP FUNCTION IF EXISTS unit_tests.check_vacuum_analyze();

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

    IF NOT EXISTS(SELECT MAX(last_vacuum) FROM pg_stat_user_tables HAVING MAX(last_vacuum) > NOW() - interval '24 hours') THEN
        SELECT assert.fail('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 assert.fail('Analyze was not run since last 24 hours.') INTO message;
        RETURN message;
    END IF;

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

/*Use transaction if your test contains DML query.*/
--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;
Please Note
Visit Repository