pgtap

pgtap : Unit testing for PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3200
pgtap
pgtap
1.3.4
LANG
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
Requires
plpgsql
See Also
plpgsql_check
plpgsql
pldbgapi
plprofiler
faker
unit
dbt2
plperl

missing pg17 el9, breaking perl deps

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
1.3.4
18
17
16
15
14
pgtapplpgsql
RPM
PGDG
1.3.4
18
17
16
15
14
pgtap_$v-
DEB
PGDG
1.3.4
18
17
16
15
14
postgresql-$v-pgtap-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
el8.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
el9.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
el9.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
el10.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
el10.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
d12.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
d12.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
d13.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
d13.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
u22.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
u22.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
u24.x86_64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
u24.aarch64
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PGDG 1.3.4
PackageVersionOSORGSIZEFile URL
pgtap_181.3.4el8.x86_64pgdg118.1 KiBpgtap_18-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_181.3.3el8.x86_64pgdg117.3 KiBpgtap_18-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_181.3.4el8.aarch64pgdg118.1 KiBpgtap_18-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_181.3.3el8.aarch64pgdg117.3 KiBpgtap_18-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_181.3.4el9.x86_64pgdg106.8 KiBpgtap_18-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_181.3.3el9.x86_64pgdg106.5 KiBpgtap_18-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_181.3.4el9.aarch64pgdg106.7 KiBpgtap_18-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_181.3.3el9.aarch64pgdg106.4 KiBpgtap_18-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_181.3.4el10.x86_64pgdg107.3 KiBpgtap_18-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_181.3.3el10.x86_64pgdg107.0 KiBpgtap_18-1.3.3-1PGDG.rhel10.noarch.rpm
pgtap_181.3.4el10.aarch64pgdg107.3 KiBpgtap_18-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_181.3.3el10.aarch64pgdg106.9 KiBpgtap_18-1.3.3-1PGDG.rhel10.noarch.rpm
postgresql-18-pgtap1.3.4d12.x86_64pgdg62.1 KiBpostgresql-18-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-18-pgtap1.3.4d12.aarch64pgdg62.1 KiBpostgresql-18-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-18-pgtap1.3.4d13.x86_64pgdg62.1 KiBpostgresql-18-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-18-pgtap1.3.4d13.aarch64pgdg62.1 KiBpostgresql-18-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-18-pgtap1.3.4u22.x86_64pgdg46.9 KiBpostgresql-18-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-18-pgtap1.3.4u22.aarch64pgdg46.9 KiBpostgresql-18-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-18-pgtap1.3.4u24.x86_64pgdg44.9 KiBpostgresql-18-pgtap_1.3.4-1.pgdg24.04+1_all.deb
postgresql-18-pgtap1.3.4u24.aarch64pgdg44.9 KiBpostgresql-18-pgtap_1.3.4-1.pgdg24.04+1_all.deb
PackageVersionOSORGSIZEFile URL
pgtap_171.3.4el8.x86_64pgdg118.1 KiBpgtap_17-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_171.3.3el8.x86_64pgdg117.3 KiBpgtap_17-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_171.3.4el8.aarch64pgdg118.1 KiBpgtap_17-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_171.3.4el9.x86_64pgdg106.8 KiBpgtap_17-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_171.3.3el9.x86_64pgdg106.5 KiBpgtap_17-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_171.3.4el9.aarch64pgdg106.7 KiBpgtap_17-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_171.3.3el9.aarch64pgdg106.5 KiBpgtap_17-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_171.3.4el10.x86_64pgdg107.3 KiBpgtap_17-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_171.3.3el10.x86_64pgdg107.0 KiBpgtap_17-1.3.3-1PGDG.rhel10.noarch.rpm
pgtap_171.3.4el10.aarch64pgdg107.3 KiBpgtap_17-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_171.3.3el10.aarch64pgdg106.9 KiBpgtap_17-1.3.3-1PGDG.rhel10.noarch.rpm
postgresql-17-pgtap1.3.4d12.x86_64pgdg62.1 KiBpostgresql-17-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-17-pgtap1.3.4d12.aarch64pgdg62.1 KiBpostgresql-17-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-17-pgtap1.3.4d13.x86_64pgdg62.1 KiBpostgresql-17-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-17-pgtap1.3.4d13.aarch64pgdg62.1 KiBpostgresql-17-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-17-pgtap1.3.4u22.x86_64pgdg46.9 KiBpostgresql-17-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-17-pgtap1.3.4u22.aarch64pgdg46.9 KiBpostgresql-17-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-17-pgtap1.3.4u24.x86_64pgdg44.9 KiBpostgresql-17-pgtap_1.3.4-1.pgdg24.04+1_all.deb
postgresql-17-pgtap1.3.4u24.aarch64pgdg44.9 KiBpostgresql-17-pgtap_1.3.4-1.pgdg24.04+1_all.deb
PackageVersionOSORGSIZEFile URL
pgtap_161.3.4el8.x86_64pgdg118.1 KiBpgtap_16-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_161.3.3el8.x86_64pgdg117.3 KiBpgtap_16-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_161.3.4el8.aarch64pgdg118.1 KiBpgtap_16-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_161.3.4el9.x86_64pgdg106.8 KiBpgtap_16-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_161.3.1el9.x86_64pgdg109.5 KiBpgtap_16-1.3.1-1PGDG.rhel9.x86_64.rpm
pgtap_161.3.4el9.aarch64pgdg106.8 KiBpgtap_16-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_161.3.3el9.aarch64pgdg106.5 KiBpgtap_16-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_161.3.1el9.aarch64pgdg109.3 KiBpgtap_16-1.3.1-1PGDG.rhel9.aarch64.rpm
pgtap_161.3.4el10.x86_64pgdg107.3 KiBpgtap_16-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_161.3.3el10.x86_64pgdg107.0 KiBpgtap_16-1.3.3-1PGDG.rhel10.noarch.rpm
pgtap_161.3.4el10.aarch64pgdg107.3 KiBpgtap_16-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_161.3.3el10.aarch64pgdg106.9 KiBpgtap_16-1.3.3-1PGDG.rhel10.noarch.rpm
postgresql-16-pgtap1.3.4d12.x86_64pgdg62.1 KiBpostgresql-16-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-16-pgtap1.3.4d12.aarch64pgdg62.1 KiBpostgresql-16-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-16-pgtap1.3.4d13.x86_64pgdg62.1 KiBpostgresql-16-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-16-pgtap1.3.4d13.aarch64pgdg62.1 KiBpostgresql-16-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-16-pgtap1.3.4u22.x86_64pgdg46.9 KiBpostgresql-16-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-16-pgtap1.3.4u22.aarch64pgdg46.9 KiBpostgresql-16-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-16-pgtap1.3.4u24.x86_64pgdg44.9 KiBpostgresql-16-pgtap_1.3.4-1.pgdg24.04+1_all.deb
postgresql-16-pgtap1.3.4u24.aarch64pgdg44.9 KiBpostgresql-16-pgtap_1.3.4-1.pgdg24.04+1_all.deb
PackageVersionOSORGSIZEFile URL
pgtap_151.3.4el8.x86_64pgdg118.1 KiBpgtap_15-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_151.3.3el8.x86_64pgdg117.3 KiBpgtap_15-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_151.2.0el8.x86_64pgdg113.9 KiBpgtap_15-1.2.0-1.rhel8.noarch.rpm
pgtap_151.3.4el8.aarch64pgdg118.1 KiBpgtap_15-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_151.3.4el9.x86_64pgdg106.8 KiBpgtap_15-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_151.3.1el9.x86_64pgdg109.5 KiBpgtap_15-1.3.1-1PGDG.rhel9.x86_64.rpm
pgtap_151.3.4el9.aarch64pgdg106.7 KiBpgtap_15-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_151.3.3el9.aarch64pgdg106.5 KiBpgtap_15-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_151.3.1el9.aarch64pgdg109.3 KiBpgtap_15-1.3.1-1PGDG.rhel9.aarch64.rpm
pgtap_151.3.4el10.x86_64pgdg107.3 KiBpgtap_15-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_151.3.3el10.x86_64pgdg107.0 KiBpgtap_15-1.3.3-1PGDG.rhel10.noarch.rpm
pgtap_151.3.4el10.aarch64pgdg107.3 KiBpgtap_15-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_151.3.3el10.aarch64pgdg106.9 KiBpgtap_15-1.3.3-1PGDG.rhel10.noarch.rpm
postgresql-15-pgtap1.3.4d12.x86_64pgdg62.1 KiBpostgresql-15-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-15-pgtap1.3.4d12.aarch64pgdg62.1 KiBpostgresql-15-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-15-pgtap1.3.4d13.x86_64pgdg62.1 KiBpostgresql-15-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-15-pgtap1.3.4d13.aarch64pgdg62.1 KiBpostgresql-15-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-15-pgtap1.3.4u22.x86_64pgdg46.9 KiBpostgresql-15-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-15-pgtap1.3.4u22.aarch64pgdg46.9 KiBpostgresql-15-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-15-pgtap1.3.4u24.x86_64pgdg44.9 KiBpostgresql-15-pgtap_1.3.4-1.pgdg24.04+1_all.deb
postgresql-15-pgtap1.3.4u24.aarch64pgdg44.9 KiBpostgresql-15-pgtap_1.3.4-1.pgdg24.04+1_all.deb
PackageVersionOSORGSIZEFile URL
pgtap_141.3.4el8.x86_64pgdg118.1 KiBpgtap_14-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_141.3.3el8.x86_64pgdg117.3 KiBpgtap_14-1.3.3-1PGDG.rhel8.noarch.rpm
pgtap_141.2.0el8.x86_64pgdg113.9 KiBpgtap_14-1.2.0-1.rhel8.noarch.rpm
pgtap_141.1.0el8.x86_64pgdg111.0 KiBpgtap_14-1.1.0-3.rhel8.noarch.rpm
pgtap_141.3.4el8.aarch64pgdg118.1 KiBpgtap_14-1.3.4-1PGDG.rhel8.noarch.rpm
pgtap_141.3.4el9.x86_64pgdg106.8 KiBpgtap_14-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_141.3.1el9.x86_64pgdg109.5 KiBpgtap_14-1.3.1-1PGDG.rhel9.x86_64.rpm
pgtap_141.3.4el9.aarch64pgdg106.7 KiBpgtap_14-1.3.4-1PGDG.rhel9.noarch.rpm
pgtap_141.3.3el9.aarch64pgdg106.5 KiBpgtap_14-1.3.3-1PGDG.rhel9.noarch.rpm
pgtap_141.3.1el9.aarch64pgdg109.3 KiBpgtap_14-1.3.1-1PGDG.rhel9.aarch64.rpm
pgtap_141.3.4el10.x86_64pgdg107.3 KiBpgtap_14-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_141.3.3el10.x86_64pgdg107.0 KiBpgtap_14-1.3.3-1PGDG.rhel10.noarch.rpm
pgtap_141.3.4el10.aarch64pgdg107.3 KiBpgtap_14-1.3.4-1PGDG.rhel10.noarch.rpm
pgtap_141.3.3el10.aarch64pgdg106.9 KiBpgtap_14-1.3.3-1PGDG.rhel10.noarch.rpm
postgresql-14-pgtap1.3.4d12.x86_64pgdg62.1 KiBpostgresql-14-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-14-pgtap1.3.4d12.aarch64pgdg62.1 KiBpostgresql-14-pgtap_1.3.4-1.pgdg12+1_all.deb
postgresql-14-pgtap1.3.4d13.x86_64pgdg62.1 KiBpostgresql-14-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-14-pgtap1.3.4d13.aarch64pgdg62.1 KiBpostgresql-14-pgtap_1.3.4-1.pgdg13+1_all.deb
postgresql-14-pgtap1.3.4u22.x86_64pgdg46.9 KiBpostgresql-14-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-14-pgtap1.3.4u22.aarch64pgdg46.9 KiBpostgresql-14-pgtap_1.3.4-1.pgdg22.04+1_all.deb
postgresql-14-pgtap1.3.4u24.x86_64pgdg44.9 KiBpostgresql-14-pgtap_1.3.4-1.pgdg24.04+1_all.deb
postgresql-14-pgtap1.3.4u24.aarch64pgdg44.9 KiBpostgresql-14-pgtap_1.3.4-1.pgdg24.04+1_all.deb

Source

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pgtap;		# install via package name, for the active PG version

pig install pgtap -v 18;   # install for PG 18
pig install pgtap -v 17;   # install for PG 17
pig install pgtap -v 16;   # install for PG 16
pig install pgtap -v 15;   # install for PG 15
pig install pgtap -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION pgtap CASCADE; -- requires plpgsql

Usage

pgtap: Unit testing for PostgreSQL

pgtap is a unit testing framework for PostgreSQL that produces TAP (Test Anything Protocol) output, providing hundreds of assertion functions for testing database objects and query results.

CREATE EXTENSION pgtap;

Test Structure

BEGIN;
SELECT plan(3);  -- declare how many tests to run

SELECT ok(1 = 1, 'one equals one');
SELECT is(1 + 1, 2, 'addition works');
SELECT isnt(1, 2, 'one is not two');

SELECT * FROM finish();
ROLLBACK;

Use no_plan() when the test count is not known in advance:

BEGIN;
SELECT * FROM no_plan();
-- ... tests ...
SELECT * FROM finish();
ROLLBACK;

Basic Assertions

SELECT ok(expression, description);           -- boolean test
SELECT is(got, expected, description);         -- equality test
SELECT isnt(got, unexpected, description);     -- inequality test
SELECT matches(value, regex, description);     -- regex match

Schema Testing

SELECT has_table('users');
SELECT has_table('myschema', 'users', 'users table exists');
SELECT has_column('users', 'email');
SELECT col_type_is('users', 'email', 'text');
SELECT col_not_null('users', 'id');
SELECT col_has_default('users', 'created_at');
SELECT has_function('calculate_total');
SELECT has_function('calculate_total', ARRAY['integer', 'numeric']);
SELECT has_index('users', 'users_email_idx');
SELECT has_pk('users');
SELECT has_fk('orders');

Error Testing

SELECT lives_ok('INSERT INTO t(id) VALUES (1)', 'insert succeeds');
SELECT throws_ok(
  'SELECT 1/0',
  '22012',          -- SQLSTATE for division by zero
  'division by zero'
);

Query Result Testing

-- Compare ordered result sets
SELECT results_eq(
  'SELECT * FROM active_users()',
  'SELECT * FROM users WHERE active',
  'active_users returns correct rows'
);

-- Compare unordered result sets
SELECT set_eq(
  'SELECT * FROM active_ids()',
  ARRAY[2, 3, 4, 5]
);

-- Check query returns no rows
SELECT is_empty('SELECT * FROM users WHERE id = -1');

-- Compare bag (multiset) results
SELECT bag_eq(
  'SELECT color FROM items',
  $$VALUES ('red'), ('blue'), ('red')$$
);

Running Tests with pg_prove

pg_prove -d mydb tests/*.sql
pg_prove -d mydb --ext .sql --recurse tests/

xUnit Style

CREATE FUNCTION test_my_feature() RETURNS SETOF text AS $$
  RETURN NEXT ok(1 = 1, 'basic check');
  RETURN NEXT is(my_func(1), 42, 'function works');
$$ LANGUAGE plpgsql;

SELECT * FROM runtests('test_my_feature');
Last updated on