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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章