pgtap
pgtap
pgtap : Unit testing for PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3200 | pgtap
|
pgtap
|
1.3.4 |
LANG
|
PostgreSQL
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--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
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG
|
1.3.4 |
18
17
16
15
14
|
pgtap |
plpgsql |
| 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 / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
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
|
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall 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 14Create this extension with:
CREATE EXTENSION pgtap CASCADE; -- requires plpgsqlUsage
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 matchSchema 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