Skip to main content

pgTAP: Unit Testing

pgTAP is a unit testing extension for PostgreSQL.

Overview#

Let's cover some basic concepts:

  • Unit tests: allow you to test small parts of a system (like a database table!).
  • TAP: stands for Test Anything Protocol. It is an framework which aims to simplify the error reporting during testing.

Usage#

Enabling#

1. Go to the Database page.
2. Click on "Extensions" in the sidebar.
3. Search for "pgtap".
4. Click the toggle.

Managing tests#

It's a good practice to keep all your tests in a separate schema.

create schema tests;

Creating a test#

@TODO

  • Create a plan
  • We should come up with a recommendation on how to run the tests. Via a function? External scripts?
  • Eventually this can be done via our CLI

Running a test#

@TODO

Examples#

Let's look at a few different tests which could be helpful in your project.

Testing tables#

begin;
select plan( 1 );
select has_table( 'profiles' );
select * from finish();
rollback;

API:

Testing columns#

begin;
select plan( 1 );
select has_column( 'profiles', 'id' );
select col_is_pk( 'profiles', 'id' );
select * from finish();
rollback;

API:

Resources#