PG_PROVE(1)
π Name π
Okay bestie, welcome to PG_PROVE(1) from the User Contributed Perl Documentation collection. This is the manual for pg_prove β a command-line tool for running and harnessing pgTAP tests. Itβs giving test-runner main character energy, and weβre gonna cover every single flag. π
π Usage π
pg_prove tests/
pg_prove --dbname template1 test*.sql
pg_prove -d testdb --runtests
πΈ Description πΈ
pg_prove is a command-line application to run one or more pgTAP (https://pgtap.org/) tests in a PostgreSQL database. The output of the tests is harvested and processed by TAP::Harness in order to summarize the results of the test. β¨
pg_prove relies on psql (https://www.postgresql.org/docs/current/app-psql.html) to run the tests, and therefor it supports all of the environment variables (https://www.postgresql.org/docs/current/libpq-envars.html), as well as the password file (https://www.postgresql.org/docs/current/libpq-pgpass.html) and service file (https://www.postgresql.org/docs/current/libpq-pgservice.html) read by psql.
Tests can be written and run in one of two ways, as SQL scripts or as xUnit-style database functions. Both are valid, both are iconic, letβs go. π¦
π§ͺ Test Scripts π§ͺ
pgTAP test scripts should consist of a series of SQL statements that output TAP. Hereβs a simple example that assumes that the pgTAP functions have been installed in the database:
-- Start transaction and plan the tests.
BEGIN;
SELECT plan(1);
-- Run the tests.
SELECT pass( 'My test passed, w00t!' );
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;
Now run the tests by passing the list of SQL script names or the name of a test directory to pg_prove. Hereβs what it looks like when the pgTAP tests are run with pg_prove:
% pg_prove -U postgres tests/
tests/coltap.....ok
tests/hastap.....ok
tests/moretap....ok
tests/pg73.......ok
tests/pktap......ok
All tests successful.
Files=5, Tests=216, 1 wallclock secs ( 0.06 usr 0.02 sys + 0.08 cusr 0.07 csys = 0.23 CPU)
Result: PASS
π§ xUnit Test Functions π§
pgTAP test functions should return a set of text, and then simply return the values returned by pgTAP functions, like so:
CREATE OR REPLACE FUNCTION setup_insert(
) RETURNS SETOF TEXT AS $$
RETURN NEXT is( MAX(nick), NULL, 'Should have no users') FROM users;
INSERT INTO users (nick) VALUES ('theory');
$$ LANGUAGE plpgsql;
Create OR REPLACE FUNCTION test_user(
) RETURNS SETOF TEXT AS $$
SELECT is( nick, 'theory', 'Should have nick') FROM users;
END;
$$ LANGUAGE sql;
Once you have these functions defined in your database, you can run them with pg_prove by using the --runtests option.
% pg_prove --dbname mydb --runtests
runtests()....ok
All tests successful.
Files=1, Tests=32, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.01 cusr 0.00 csys = 0.04 CPU)
Result: PASS
Be sure to pass the --schema option if your test functions are all in one schema, and the --match option if they have names that donβt start with test. For example, if you have all of your test functions in the test schema and ending with test, run the tests like so:
pg_prove --dbname mydb --schema test --match 'test$'
β¨ Options β¨
-b,--psql-binPSQL β Location of thepsqlclient.-d,--dbnameDBNAME β Database to which to connect.-U,--usernameUSERNAME β User with which to connect.-h,--hostHOST β Host to which to connect.-p,--portPORT β Port to which to connect.-P,--psetOPTION=VALUE β Setpsqlkey/value printing option.-S,--setVAR=VALUE β Set variables forpsqlsession.-R,--runtestsβ Run xUnit test usingruntests().-s,--schemaSCHEMA β Schema in which to find xUnit tests.-x,--matchREGEX β Regular expression to find xUnit tests.--extβ Set the extension for tests (default.pg)-r,--recurseβ Recursively descend into directories.--ignore-exitβ Ignore exit status from test scripts.--trapβ Trap Ctrl+C and print summary on interrupt.--harnessβ Define test harness to use.-j,--jobsN β Run N test jobs in parallel (try 9.)--rcRCFILE β Process options from rcfile--norcβ Donβt process default.proverc--stateOPTION=VALUE β Set persistent state options.-v,--verboseβ Print all test lines.-f,--failuresβ Show failed tests.-o,--commentsβ Show comments and diagnostics.--directivesβ Only show results with TODO or SKIP directives.-q,--quietβ Suppress some test output while running tests.-Q,--QUIETβ Only print summary results.--parseβ Show full list of TAP parse errors, if any.--normalizeβ Normalize TAP output in verbose output-D,--dryβ Dry run. Show test that would have run.--mergeβ Merge test scriptsβ STDERR and STDOUT.-t,--timerβ Print elapsed time after each test.-c,--colorβ Colored test output (default).--nocolorβ Do not color test output.--shuffleβ Run the tests in random order.--reverseβ Run the tests in reverse order.-a,--archiveFILENAME β Store the resulting TAP in an archive file.--formatterβ Result formatter to use.--countβ Show X/Y test count when not verbose (default)--nocountβ Disable the X/Y test count.-H,--helpβ Print a usage statement and exit.-?β Print a usage statement and exit.-m,--manβ Print the complete documentation and exit.-V,--versionβ Print the version number and exit.
π Options Details π
ποΈ Database Options ποΈ
π -b, --psql-bin
pg_prove --psql-bin /usr/local/pgsql/bin/psql
pg_prove -b /usr/local/bin/psql
Path to the psql program, which will be used to actually run the tests. Defaults to psql, which should work well when it is in your path. π»
ποΈ -d, --dbname
pg_prove --dbname try
pg_prove -d postgres
The name of database to which to connect. Defaults to the value of the $PGDATABASE environment variable or to the system username.
π§βπ» -U, --username
pg_prove --username foo
pg_prove -U postgres
PostgreSQL user name to connect as. Defaults to the value of the $PGUSER environment variable or to the operating system name of the user running the application.
π₯οΈ -h, --host
pg_prove --host pg.example.com
pg_prove -h dev.local
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix-domain socket. Defaults to the value of the $PGHOST environment variable or localhost.
π’ -p, --port
pg_prove --port 1234
pg_prove -p 666
Specifies the TCP port or the local Unix-domain socket file extension on which the server is listening for connections. Defaults to the value of the $PGPORT environment variable or, if not set, to the port specified at compile time, usually 5432.
π¨οΈ -P, --pset
pg_prove --pset tuples_only=0
pg_prove -P null=[NULL]
Specifies printing options in the style of \pset in the psql program. See https://www.postgresql.org/docs/current/static/app-psql.html for details on the supported options.
π§· -S, --set
pg_prove --set MY_CONTRACT=321
pg_prove -S TEST_SEARCH_PATH=test,public
Sets local variables for psql in the style of \set in the psql program. See https://www.postgresql.org/docs/current/static/app-psql.html for details on the supported options.
π --runtests
pg_prove --runtests
pg_prove -r
Donβt run any test scripts, but just use the runtests() pgTAP function to run xUnit tests. This ends up looking like a single test script has been run, when in fact no test scripts have been run. Instead, pg_prove tells psql to run something like:
psql --command 'SELECT * FROM runtests()'
You should use this option when youβve written your tests in xUnit style, where theyβre all defined in test functions already loaded in the database. π«Ά
ποΈ -s, --schema
pg_prove --schema test
pg_prove -s mytest
Used with --runtests, and, in fact, implicitly forces --runtests to be true. This option can be used to specify the name of a schema in which to find xUnit functions to run. Basically, it tells psql to run something like:
psql --command "SELECT * FROM runtests('test'::name)"
π -x, --match
pg_prove --match 'test$'
pg_prove -x _test_
Used with --runtests, and, in fact, implicitly forces --runtests to be true. This option can be used to specify a POSIX regular expression that will be used to search for xUnit functions to run. Basically, it tells psql to run something like:
psql --command "SELECT * FROM runtests('_test_'::text)"
This will run any visible functions with the string _test_ in their names. This can be especially useful if you just want to run a single test in a given schema. For example, this:
pg_prove --schema testing --match '^test_widgets$'
Will have psql execute the runtests() function like so:
SELECT * FROM runtests('testing'::name, '^test_widgets$'::text);
ποΈ Behavioral Options ποΈ
π --ext
pg_prove --ext .sql tests/
Set the extension for test files (default .pg). May be specified multiple times if you have test scripts with multiple extensions, though all must be pgTAP tests:
pg_prove --ext .sql --ext .pg --ext .pgt
If you want to mix pgTAP tests with other TAP-emitting tests, like Perl tests, use prove instead, where --ext identifies any test file, and --pgtap-option suffix= lets you specify one or more extensions for pgTAP tests.
prove --source Perl \
--ext .t --ext .pg \
--source pgTAP --pgtap-option suffix=.pg
π -r, --recurse
pg_prove --recurse tests/
pg_prove --recurse sql/
Recursively descend into directories when searching for tests. Be sure to specify --ext if your tests do not end in .pg. Not relevant with --runtests.
π --ignore-exit
pg_prove --ignore-exit
--ignore-exitβ Ignore exit status from test scripts. Normally if a script triggers a database exception,psqlwill exit with an error code and, even if all tests passed, the test will be considered a failure. Use--ignore-exitto ignore such situations (at your own peril). π--trapβ Trap Ctrl+C and print a summary on interrupt. π
pg_prove --trap
--harnessβ Specify a subclass ofTAP::Harnessto use for the test harness. Defaults toTAP::Harness(unless--archiveis specified, in which case it usesTAP::Harness::Archive). π
pg_prove --harness TAP::Harness::Color
-j,-jobsβ Run N test jobs in parallel (try 9.) β‘--rcβ Process options from the specified configuration file. πΈ
pg_prove --rc pg_prove.rc
If --rc is not specified and ./.proverc or ~/.proverc exist, they will be read and the options they contain processed before the command line options. Options in configuration files are specified in the same way as command line options:
# .proverc
--state=hot,fast,save
-j9
Under Windows and VMS the option file is named _proverc rather than .proverc and is sought only in the current directory.
Due to how options are loaded you cannot use .proverc for pg_prove-specific options, only prove options. However, pg_prove does support all of the usual libpq Environment Variables (https://www.postgresql.org/docs/current/static/libpq-envars.html). π«Ά
--norcβ Do not process./.provercor~/.proverc. π ββοΈ--stateβ You can askpg_proveto remember the state of previous test runs and select and/or order the tests to be run based on that saved state. π§ β¨
The --state switch requires an argument which must be a comma separated list of one or more of the following options.
lastβ Run the same tests as the last time the state was saved. This makes it possible, for example, to recreate the ordering of a shuffled test. π
# Run all tests in random order
pg_prove --state save --shuffle
# Run them again in the same order
pg_prove --state last
failedβ Run only the tests that failed on the last run. π
# Run all tests
pg_prove --state save
# Run failures
pg_prove --state failed
If you also specify the save option newly passing tests will be excluded from subsequent runs.
# Repeat until no more failures
pg_prove --state failed,save
passedβ Run only the passed tests from last time. Useful to make sure that no new problems have been introduced. βallβ Run all tests in normal order. Multiple options may be specified, so to run all tests with the failures from last time first:
pg_prove --state failed,all,save
hotβ Run the tests that most recently failed first. The last failure time of each test is stored. Thehotoption causes tests to be run in most-recent-failure order. π₯
pg_prove --state hot,save
Tests that have never failed will not be selected. To run all tests with the most recently failed first use
pg_prove --state hot,all,save
This combination of options may also be specified thus
pg_prove --state adrian
todoβ Run any tests with todos. πslowβ Run the tests in slowest to fastest order. This is useful in conjunction with the-jparallel testing switch to ensure that your slowest tests start running first. π’
pg_prove --state slow -j9
fastβ Run test tests in fastest to slowest order. πnewβ Run the tests in newest to oldest order based on the modification times of the test scripts. πoldβ Run the tests in oldest to newest order. ποΈfreshβ Run those test scripts that have been modified since the last test run. π§saveβ Save the state on exit. The state is stored in a file called.pg_prove(_pg_proveon Windows and VMS) in the current directory. πΎ
The --state switch may be used more than once.
pg_prove --state hot --state all,save
π Display Options π
-v,--verboseβ Display standard output of test scripts while running them. This behavior can also be triggered by setting the$TEST_VERBOSEenvironment variable to a true value. π
pg_prove --verbose
pg_prove -v
-f,--failuresβ Show failed tests. π
pg_prove --failures
pg_prove -f
-o,--commentsβ Show comments, such as diagnostics output bydiag(). Enabled by default. Use--no-commentsto disable. π¬--directivesβ Only show results with TODO or SKIP directives. π¦
pg_prove --directives
-q,--quietβ Suppress some test output while running tests. π€«
pg_prove --quiet
pg_prove -q
-Q,--QUIETβ Only print summary results. π
pg_prove --QUIET
pg_prove -Q
--parseβ Enables the display of any TAP parsing errors as tests run. Useful for debugging new TAP emitters. π
pg_prove --parse
--normalizeβ Normalize TAP output in verbose output. Errors in the harnessed TAP corrected by the parser will be corrected. π§Ό
pg_prove --normalize
--dry,-Dβ Dry run. Just outputs a list of the tests that would have been run. π΅
pg_prove --dry tests/
pg_prove -D
--mergeβ Merge test scripts'STDERRwith theirSTDOUT. Not really relevant to pgTAP tests, which only print toSTDERRwhen an exception is thrown. π-t,--timerβ Print elapsed time after each test file. β±οΈ
pg_prove --timer
pg_prove -t
-c,--colorβ Display test results in color. Colored test output is the default, but if output is not to a terminal, color is disabled. π
Requires Term::ANSIColor on Unix-like platforms and Win32::Console on Windows. If the necessary module is not installed colored output will not be available.
pg_prove --color
pg_prove -c
--nocolorβ Do not display test results in color. π«π--shuffleβ Test scripts are normally run in alphabetical order. Use--reverseto run them in random order. Not relevant when used with--runtests. π²
pg_prove --shuffle tests/
--reverseβ Test scripts are normally run in alphabetical order. Use--reverseto run them in reverse order. Not relevant when used with--runtests. β©οΈ
pg_prove --reverse tests/
-a,--archiveβ Send the TAP output to a TAP archive file as well as to the normal output destination. The archive formats supported are.tarand.tar.gz. π¦
pg_prove --archive tap.tar.gz
pg_prove -a test_output.tar
-f,--formatterβ The name of the class to use to format output. The default isTAP::Formatter::Console, orTAP::Formatter::Fileif the output isn't a TTY. π¨
pg_prove --formatter TAP::Formatter::File
pg_prove -f TAP::Formatter::Console
--countβ Show the X/Y test count as tests run when not verbose (default). π’
pg_prove --count
--nocountβ Disable the display of the X/Y test count as tests run. π
pg_prove --nocount
π Metadata Options π
-H,-?,--helpβ Outputs a brief description of the options supported bypg_proveand exits. π
pg_prove --help
pg_prove -H
-m,--manβ Outputs this documentation and exits. π
pg_prove --man
pg_prove -m
-V,--versionβ Outputs the program name and version and exits. π·οΈ
pg_prove --version
pg_prove -V
π Author π
David E. Wheeler <dwheeler@cpan.org>
Β© Copyright Β©
Copyright (c) 2008-2025 David E. Wheeler. Some Rights Reserved.
Sign-off deets, bestie: this iconic page is from perl v5.42.3, dated 2025-03-30, and it lives as PG_PROVE(1). Mwah πβ¨
π¦ where this page comes from
- nixos 26.05: perl5.42.3-TAP-Parser-SourceHandler-pgTAP 3.36
- nixpkgs unstable: perl5.42.3-TAP-Parser-SourceHandler-pgTAP 3.36
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms