Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save knmkr/a71bc18e7ecbf516dbdc to your computer and use it in GitHub Desktop.
Save knmkr/a71bc18e7ecbf516dbdc to your computer and use it in GitHub Desktop.
[postgresql] Unit testing in PostgreSQL with pgTAP extension + pg_prove

pgTAP に付随して公開されている pg_prove というツールでユニットテストの実行・計測ができる.

perl なので cpan からインストール. pg_prove コマンドが使えるようになる.

$ cpan TAP::Parser::SourceHandler::pgTAP
$ pg_prove --version
pg_prove 3.29

ユニットテストのスクリプトは .pg で保存して, pg_prove コマンドでスクリプトのあるディレクトリを指定して実行:

$ cat t/test_pass.pg
BEGIN;

SET search_path = public,tap,pg_catalog;

SELECT plan(1);

SELECT pass('This should pass!');

SELECT * FROM finish();
ROLLBACK;

$ pg_prove -d my_database t/
t/test_pass.pg .. ok
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.04 usr +  0.01 sys =  0.05 CPU)
Result: PASS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment