PG_PROVE(1)

πŸ“¦ version 3.36, in nixos 26.05, nixpkgs unstable

πŸ’– 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-bin PSQL β€” Location of the psql client.
  • -d, --dbname DBNAME β€” Database to which to connect.
  • -U, --username USERNAME β€” User with which to connect.
  • -h, --host HOST β€” Host to which to connect.
  • -p, --port PORT β€” Port to which to connect.
  • -P, --pset OPTION=VALUE β€” Set psql key/value printing option.
  • -S, --set VAR=VALUE β€” Set variables for psql session.
  • -R, --runtests β€” Run xUnit test using runtests().
  • -s, --schema SCHEMA β€” Schema in which to find xUnit tests.
  • -x, --match REGEX β€” 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, --jobs N β€” Run N test jobs in parallel (try 9.)
  • --rc RCFILE β€” Process options from rcfile
  • --norc β€” Don’t process default .proverc
  • --state OPTION=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, --archive FILENAME β€” 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, psql will exit with an error code and, even if all tests passed, the test will be considered a failure. Use --ignore-exit to ignore such situations (at your own peril). 😭
  • --trap β€” Trap Ctrl+C and print a summary on interrupt. πŸ’…
pg_prove --trap
  • --harness β€” Specify a subclass of TAP::Harness to use for the test harness. Defaults to TAP::Harness (unless --archive is specified, in which case it uses TAP::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 ./.proverc or ~/.proverc. πŸ™…β€β™€οΈ
  • --state β€” You can ask pg_prove to 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. The hot option 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 -j parallel 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_prove on 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_VERBOSE environment 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 by diag(). Enabled by default. Use --no-comments to 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' STDERR with their STDOUT. Not really relevant to pgTAP tests, which only print to STDERR when 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 --reverse to 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 --reverse to 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 .tar and .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 is TAP::Formatter::Console, or TAP::Formatter::File if 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 by pg_prove and 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 (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