Skip to content
Announcing BigAnimal: Fully managed PostgreSQL in the Cloud
Postgres Build 2021: 30 Nov to 1 Dec
Contact usDocsPlans

How to use LCOV for analyzing the test coverage against PostgreSQL - Source code and Examples

Prabhat Sahu7/1/2021
Developers

LCOV is a graphical tool for GCC's coverage testing with gcov. It creates HTML pages containing the source code annotated with coverage information by collecting gcov data from multiple source files. LCOV supports “Lines coverage” and “Functions coverage” measurement. 

LCOV report looks as follows:

 

Steps to perform test coverage for any schedule:

 

1. Cleanup the source under “postgressql” directory.

cd /home/prabhat/PG/PGsrc/postgresql

make clean

make distclean

git clean -fdx

git reset HEAD --hard

2. Configure with "--enable-coverage" option and install.

./configure --with-zlib --enable-debug --enable-depend --prefix=$PWD/inst/ 
--enable-cassert --with-readline --with-openssl --enable-coverage CFLAGS="-g -O0"; make; make install;

3. Run the schedule (suppose, make check) From /src/test/regress directory.

cd src/test/regress

make check

 

4. Run the coverage under "/home/prabhat/PG/PGsrc/postgresql" directory

cd ../../../

make coverage-html

 

5. Open the index.html in the firefox browser under the "coverage" directory.

cd coverage/

firefox index.html

 

Steps to perform test coverage for newly added test cases:

1. Cleanup the source under “postgressql” directory.

cd /home/prabhat/PG/PGsrc/postgresql

make clean

make distclean

git clean -fdx

git reset HEAD --hard

 

2. Configure with "--enable-coverage" option and install the source.

./configure --with-zlib --enable-debug --enable-depend --prefix=$PWD/inst/ 
--enable-cassert --with-readline --with-openssl --enable-coverage CFLAGS="-g -O0"; make; make install;

 

3. Prepare the required test case files.

Example:

cd /home/prabhat/PG/PGsrc/postgresql/inst/bin



[prabhat@localhost bin]$ cat /tmp/test.sql

-- Test to demonstrate generate_series.

select x as "col1" from generate_series(1,5) x;

select x as "col1" from generate_series(1,5,2) x;

select x+5 as "col1" from generate_series(1,10,2) x;



[prabhat@localhost bin]$ ./psql postgres -a -f /tmp/test.sql > /tmp/test.out

[prabhat@localhost bin]$ cat /tmp/test.out 

-- Test to demonstrate generate_series.

select x as "col1" from generate_series(1,5) x;

 col1 

------

    1

    2

    3

    4

    5

(5 rows)



select x as "col1" from generate_series(1,5,2) x;

 col1 

------

    1

    3

    5

(3 rows)



select x+5 as "col1" from generate_series(1,10,2) x;

 col1 

------

    6

    8

   10

   12

   14

(5 rows)

 

4. Keep test case files in specific directory under src/test/regress/  (sql, expected

cd /home/prabhat/PG/PGsrc/postgresql/src/test/regress



mkdir sql/generate_series

mkdir expected/generate_series

mkdir results/generate_series



cp /tmp/test.sql sql/generate_series/.

cp /tmp/test.out expected/generate_series/.

 

5. Run the test or schedule under "/src/test/regress" directory.

cd /src/test/regress

./pg_regress --temp-instance=/tmp/data1 generate_series/test

Example:

[prabhat@localhost regress]$ ./pg_regress --temp-instance=/tmp/data1 generate_series/test

============== creating temporary instance            ==============

============== initializing database system           ==============

============== starting postmaster                    ==============

running on port 58080 with PID 110199

============== creating database "regression"         ==============

CREATE DATABASE

ALTER DATABASE

============== running regression test queries        ==============

test generate_series/test         ... ok           24 ms

============== shutting down postmaster               ==============

============== removing temporary instance            ==============



=====================

 All 1 tests passed. 

=====================

6. Run the coverage under "/home/prabhat/PG/PGsrc/postgresql" directory.

cd ../../../

make coverage-html

Example:

[prabhat@localhost postgresql]$ make coverage-html

/usr/bin/lcov --gcov-tool /usr/bin/gcov -q --no-external -c -i -d . -d . -o lcov_base.info

/usr/bin/lcov --gcov-tool /usr/bin/gcov -q --no-external -c -d . -d . -o lcov_test.info

rm -rf coverage

/usr/bin/genhtml -q --legend -o coverage --title='PostgreSQL 14devel' --num-spaces=4 --prefix='/home/prabhat/PG/PGsrc/postgresql' lcov_base.info lcov_test.info

touch coverage-html-stamp

7. Open the “index.html” in the firefox browser under the "coverage" directory.

cd coverage/

firefox index.html