Using functions and variables with pg_prove scripts

6 views
Skip to first unread message

Matt DeLuco

unread,
Aug 8, 2025, 1:56:30 PMAug 8
to pgTAP Users
Hello,

I'm writing test scripts as illustrated in the docs for pg_prove (similar to the example under Test Scripts): https://pgtap.org/pg_prove.html

Is there a way to use functions and variables in this style of testing? Similar to xUnit tests?

I have a series of plpgsql functions to test, and as parameters these functions take id values created when inserting rows in various tables. The closest I've come is using a CTE, but it's pretty awkward and will rapidly become cumbersome when writing a lot of tests related to the same id.

Thanks!

Matt

David E. Wheeler

unread,
Aug 8, 2025, 4:24:43 PMAug 8
to Matt DeLuco, pgTAP Users
Hi Matt,

On Aug 8, 2025, at 13:56, 'Matt DeLuco' via pgTAP Users <pgtap...@googlegroups.com> wrote:

> Is there a way to use functions and variables in this style of testing? Similar to xUnit tests?

Yup: https://pgtap.org/documentation.html#whatasweetunit

> I have a series of plpgsql functions to test, and as parameters these functions take id values created when inserting rows in various tables. The closest I've come is using a CTE, but it's pretty awkward and will rapidly become cumbersome when writing a lot of tests related to the same id.

You can also create and call temporary functions that returns TAP lines:

https://pgtap.org/documentation.html#tapthatbatch

Best,

David

signature.asc

Matt DeLuco

unread,
Aug 11, 2025, 11:42:04 PMAug 11
to pgTAP Users
I had reviewed these areas of the documentation but (in the first example) I don't think it was clear to me how to run the tests.. Not sure - regardless, your direction led me to do_tap() which is what I was looking for - thanks David!

Matt

David E. Wheeler

unread,
Aug 12, 2025, 11:00:07 AMAug 12
to Matt DeLuco, pgTAP Users
On Aug 11, 2025, at 23:42, 'Matt DeLuco' via pgTAP Users <pgtap...@googlegroups.com> wrote:

> I had reviewed these areas of the documentation but (in the first example) I don't think it was clear to me how to run the tests.. Not sure - regardless, your direction led me to do_tap() which is what I was looking for - thanks David!

There is also `pg_prove --runtests`:

https://pgtap.org/pg_prove.html#xUnit-Test-Functions

Best,

David

signature.asc
Reply all
Reply to author
Forward
0 new messages