postgresql新增单元测试模块

src/test/下的各个模块的单元测试通过make check执行的时候,本质上是调用pg_regress(它包含一个完整的测试框架)程序运行用例。

Perl-based TAP(Test Anything Protocol) tests
====================

src/test/perl/ contains shared infrastructure that's used by Perl-based tests across the source tree, particularly tests in src/bin and src/test. It's used
to drive tests for backup and restore, replication, etc - anything that can't really be expressed using pg_regress or the isolation test framework.

The tests are invoked via perl's 'prove' command, wrapped in PostgreSQL makefiles to handle instance setup etc. See the $(prove_check) and
$(prove_installcheck) targets in Makefile.global. By default every test in the t/ subdirectory is run. Individual test(s) can be run instead by passing
something like PROVE_TESTS="t/001_testname.pl t/002_othertestname.pl" to make.

You should prefer to write tests using pg_regress in src/test/regress, or isolation tester specs in src/test/isolation, if possible. If not, check to
see if your new tests make sense under an existing tree in src/test, like src/test/ssl, or should be added to one of the suites for an existing utility.

示例如下:

PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/stage/lightdb-x/bin:$PATH" LD_LIBRARY_PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/stage/lightdb-x/lib:$LD_LIBRARY_PATH"  ../../../../src/test/regress/pg_regress --temp-instance=./tmp_check --inputdir=. --bindir=     --dbname=contrib_regression test_integerset
============== creating temporary instance            ==============
============== initializing database system           ==============
============== starting postmaster                    ==============
running on port 64472 with PID 20426
============== creating temporary tablespace          ==============
CREATE TABLESPACE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries        ==============
test test_integerset              ... ok         1945 ms
============== shutting down postmaster               ==============
============== removing temporary instance            ==============

=====================
 All 1 tests passed. 
=====================

默认情况下,因为没有开启TAP,所以如果测试用例里面启用了TAP测试,会提示TAP测试未开启,如下:

# src/test/modules/test_misc/Makefile

TAP_TESTS = 1

ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
else
subdir = src/test/modules/test_misc
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif

PGXS中的所有选项解析http://www.light-pg.com/docs/lightdb/current/extend-pgxs.html

make -j1 checkprep >>'/home/zjh/Sources/postgresql-13.3'/tmp_install/log/install.log 2>&1
TAP tests not enabled

如果启用了TAP,则会运行t/目录下的.pl用例,如下:

PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/lightdb-x/bin:$PATH" LD_LIBRARY_PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/lightdb-x/lib:$LD_LIBRARY_PATH"  ../../../../src/test/regress/pg_regress --temp-instance=./tmp_check --inputdir=. --bindir=     --dbname=contrib_regression test_pg_dump
============== removing existing temp instance        ==============
============== creating temporary instance            ==============
============== initializing database system           ==============
============== starting postmaster                    ==============
running on port 64472 with PID 20089
============== creating temporary tablespace          ==============
CREATE TABLESPACE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries        ==============
test test_pg_dump                 ... ok           35 ms
============== shutting down postmaster               ==============
============== removing temporary instance            ==============

=====================
 All 1 tests passed. 
=====================

rm -rf '/home/zjh/Sources/postgresql-13.3/src/test/modules/test_pg_dump'/tmp_check
/usr/bin/mkdir -p '/home/zjh/Sources/postgresql-13.3/src/test/modules/test_pg_dump'/tmp_check
cd . && TESTDIR='/home/zjh/Sources/postgresql-13.3/src/test/modules/test_pg_dump' PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/lightdb-x/bin:$PATH" LD_LIBRARY_PATH="/home/zjh/Sources/postgresql-13.3/tmp_install/home/zjh/lightdb-x/lib:$LD_LIBRARY_PATH"  LTPORT='65432' LT_REGRESS='/home/zjh/Sources/postgresql-13.3/src/test/modules/test_pg_dump/../../../../src/test/regress/pg_regress' /usr/bin/prove -I ../../../../src/test/perl/ -I .  t/*.pl
t/001_base.pl .. ok       
All tests successful.
Files=1, Tests=669,  5 wallclock secs ( 0.03 usr  0.00 sys +  0.86 cusr  0.79 csys =  1.68 CPU)
Result: PASS

tap是否启用在src/Makefile.global.in中大约445行判断,如下:

ifeq ($(enable_tap_tests),yes)

ifndef PGXS
define prove_installcheck
rm -rf '$(CURDIR)'/tmp_check
$(MKDIR_P) '$(CURDIR)'/tmp_check
cd $(srcdir) && \
   TESTDIR='$(CURDIR)' PATH="$(bindir):$$PATH" LTPORT='6$(DEF_PGPORT)' \
   top_builddir='$(CURDIR)/$(top_builddir)' \
   LT_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' \
   $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
endef
else # PGXS case
define prove_installcheck
rm -rf '$(CURDIR)'/tmp_check
$(MKDIR_P) '$(CURDIR)'/tmp_check
cd $(srcdir) && \
   TESTDIR='$(CURDIR)' PATH="$(bindir):$$PATH" PGPORT='6$(DEF_PGPORT)' \
   top_builddir='$(top_builddir)' \
   PG_REGRESS='$(top_builddir)/src/test/regress/pg_regress' \
   $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
endef
endif # PGXS

define prove_check
rm -rf '$(CURDIR)'/tmp_check
$(MKDIR_P) '$(CURDIR)'/tmp_check
cd $(srcdir) && \
   TESTDIR='$(CURDIR)' $(with_temp_install) LTPORT='6$(DEF_PGPORT)' \
   LT_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' \
   $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
endef

else
prove_installcheck = @echo "TAP tests not enabled"
prove_check = $(prove_installcheck)
endif

  全局运行的时候,make check不会跑TAP测试,make checkworld才会跑TAP测试。

  所以新增功能用例如果是纯粹sql类(如unsafe_tests其实放在regress下就可以,不用单独模块),放在src/test/regress下最简单,否则建议在src/test/modules下新增模块。如果跑访问内核或c实现、测试客户端或guc的,通常必须单独模块,用extension机制如test_shm_mq。

  附:perl单元测试http://cn.voidcc.com/question/p-svuvnjsw-bdr.html

posted @ 2023-05-30 23:48  zhjh256  阅读(104)  评论(0编辑  收藏  举报