单元如何测试PL/PgSQL?你使用哪些库/工具?
单元测试涵盖了多少百分比的代码,如何度量它?首先如何决定单元测试的模块?
您认为您在单元测试工具上投入的时间和精力是否得到了回报?
如果你不使用单元测试,你能解释为什么不?
发布于 2013-11-14 23:03:22
如果我有一个函数public.foo(bar text) returns text..。
我创建了另一个类似于这样的函数:
create or replace function test.foo() returns void as $$
begin
perform assert_equals('stuff', public.foo('thing'));
perform assert_null(public.foo(null));
...
end $$ language plpgsql;我有一些断言函数,如下所示。我故意使用与JUnit相同的名称和签名。
CREATE OR REPLACE FUNCTION assert_equals(expected text, actual text) RETURNS void AS $$
begin
if expected = actual or (expected is null and actual is null) then
--do nothing
else
raise exception 'Assertion Error. Expected <%> but was <%>', expected, actual;
end if;
end $$ LANGUAGE plpgsql;我还有一个函数来运行所有测试:
CREATE OR REPLACE FUNCTION test.run_all() RETURNS void AS $$
declare
skip constant name[] = '{run_all}';
test_schema_name constant name = 'test';
proc pg_catalog.pg_proc%rowtype;
started timestamptz;
begin
raise notice 'Time(m) Name';
for proc in select p.* from pg_catalog.pg_proc p join pg_catalog.pg_namespace n on pronamespace = n.oid where nspname = test_schema_name and not proname = any(skip) order by proname loop
started = clock_timestamp();
execute format('select %s.%s();', test_schema_name, proc.proname);
raise notice '% %.%()', to_char(clock_timestamp() - started, 'MI:SS:MS'), test_schema_name, proc.proname;
end loop;
end $$ LANGUAGE plpgsql;关于你的问题:
单元测试涵盖了多少百分比的代码,如何度量它?
不确定。我想我需要一个代码覆盖工具。
更新:看起来您可以使用https://github.com/kputnam/piggly检查代码覆盖率。
首先如何决定单元测试的模块?
最络合的。
您认为您在单元测试工具上投入的时间和精力是否得到了回报?
很高兴我使用了单元测试。
还请注意:
http://pgtap.org/
pgunit/
http://www.epictest.org/
https://stackoverflow.com/questions/19982373
复制相似问题