-
Notifications
You must be signed in to change notification settings - Fork 23
Home
binodnp edited this page Aug 20, 2014
·
12 revisions
Welcome to plpgunit documentation.
A unit test is a function which :
- must be created under the schema "unit_tests".
- should not have any arguments.
- should always return "test_result" data type.
So, a plain Plpgunit test looks like this:
CREATE FUNCTION unit_tests.name_of_the_test()
RETURNS test_result
AS
$$
BEGIN
--Test body
END
$$
LANGUAGE plpgsql;
- A unit test is nothing but a plpgsql function created under the schema "unit_tests".
- Plpgunit test is as easy as writing a plpgsql function. Your job is to fail the test, we give you tools to do that!
- When you create a Plpgunit test, it will not be tested, just only saved.
- You can invoke a test by its name. I would only do that when I'm writing a test function. So, when I'm done writing a test, I would rather invoke all the tests to see what would the result be. This helps me concentrate on one thing at a time, and everything later.
- You can invoke all tests with a simple three-line query, which is always the same, easy to remember.
#How to Invoke All Tests However you could do that, but there is no need to call each test function manually. The following query automatically invokes all unit tests that have been already created:
BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
ROLLBACK TRANSACTION;
Remember, if none of your test(s) contain DML statements, there is no need to BEGIN and ROLLBACK transaction. So, this is fine:
SELECT * FROM unit_tests.begin();
##Functions
Function | Usage |
---|---|
assert.ok(text) | Should be placed at the end of a test function's body to propose that the test passed. |
assert.fail(text) | Fails the test. |
assert.pass(text) | Passes the test. |
assert.is_equal(IN have anyelement, IN want anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is not equal to the second. |
assert.are_equal(VARIADIC anyarray, OUT message text, OUT result boolean) | Fails the test if any item of the supplied arguments is not equal to others. Remember, you can pass any number of arguments here. |
assert.is_not_equal(IN already_have anyelement, IN dont_want anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is equal to the second argument. |
assert.are_not_equal(VARIADIC anyarray, OUT message text, OUT result boolean) | Fails the test if any item of the supplied arguments is equal to another. Remember, you can pass any number of arguments here. |
assert.is_null(IN anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is NOT NULL. |
assert.is_not_null(IN anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is NULL. |
assert.is_true(IN boolean, OUT message text, OUT result boolean) | Fails the test if the first argument is FALSE. |
assert.is_false(IN boolean, OUT message text, OUT result boolean) | Fails the test if the first argument is TRUE. |
assert.is_greater_than(IN x anyelement, IN y anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is not greater than the second argument. |
assert.is_less_than(IN x anyelement, IN y anyelement, OUT message text, OUT result boolean) | Fails the test if the first argument is not less than the second argument. |
assert.function_exists(function_name text, OUT message text, OUT result boolean) | Fails the test if the function does not exist in the current database. Make sure that the passed function name is a fully qualified relation name, and contains argument types. |