unit-testing - 您使用哪些工具/库对您的 PL/PgSQL 进行单元测试

标签 unit-testing postgresql postgresql-9.3

<分区>

如何对 PL/PgSQL 进行单元测试?您使用哪些库/工具?

单元测试覆盖了您的代码的百分比是多少?您如何衡量它?您如何决定首先对哪些模块进行单元测试?

您认为您在单元测试工具上投入的时间和精力是否得到了返回?

如果你不使用单元测试,你能解释为什么不吗?

最佳答案

如果我有一个函数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;

关于您的问题:

What percentage of your code is covered by unit tests and how do you measure it?

不确定。我想我需要一个代码覆盖工具。

更新:看起来您可以使用 https://github.com/kputnam/piggly 检查代码覆盖率

How do you decide which modules to unit test first?

complex那些。

Do you think the time and effort which you invested in your unit testing harness has paid off or not?

非常高兴我正在使用单元测试。

另请注意:

http://pgtap.org/
http://en.dklab.ru/lib/dklab_pgunit/
http://www.epictest.org/

关于unit-testing - 您使用哪些工具/库对您的 PL/PgSQL 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19982373/

相关文章:

java - 如何使用 Mockito @InjectMocks 将 HttpServletRequest 注入(inject) ContainerRequestFilter

java - 使用 PowerMockito 模拟私有(private)方法调用返回 null 而不是返回 List<String> : (Want not to execute private method)

sql - 如何针对大数据优化此查询

sql - 如何从不同的列中选择多个整数数据并将它们合并到数组整数的单列中

ruby-on-rails - 在 Rails 3.2 中修改 Postgres JSON 字段时出错

sql - PostgreSQL 9.3 - 比较两组数据而不重复第一组中的值

javascript - 除了在开发期间进行测试之外,在登台服务器上进行测试还有哪些好处?

java - 如何使用 TDD 处理无法访问的异​​常

postgresql - SELECT 在 PL/pgSQL 函数中引发异常

python - 使用 SQLAlchemy 创建表,但将索引的创建推迟到数据加载完成