This directory contains example projects and configurations demonstrating various use cases for pgcov.
Directory: simple/
A minimal example showing basic pgcov usage:
- Single source file with a PL/pgSQL function
- Single test file with test cases
- Demonstrates co-location strategy
- Shows basic assertions and error handling
Use this to:
- Learn pgcov basics
- Understand file organization
- See minimal working example
Quick start:
cd simple/
pgcov run .Directory: ci-integration/
Production-ready CI/CD configurations:
- GitHub Actions workflow
- GitLab CI pipeline
- Coverage threshold enforcement
- Report generation and upload
- PR comments with coverage stats
Use this to:
- Integrate pgcov into CI/CD
- Set up automated coverage checks
- Configure coverage reporting services
- Enforce coverage standards
Quick start:
# Copy GitHub Actions config
cp ci-integration/github-actions.yml .github/workflows/coverage.yml
# Or copy GitLab CI config
cp ci-integration/gitlab-ci.yml .gitlab-ci.ymlexamples/
├── README.md # This file
├── simple/ # Basic usage example
│ ├── calculate.sql # Source file
│ ├── calculate_test.sql # Test file
│ └── README.md # Example documentation
└── ci-integration/ # CI/CD configurations
├── github-actions.yml # GitHub Actions workflow
├── gitlab-ci.yml # GitLab CI pipeline
└── README.md # CI setup guide
All examples follow this pattern:
-
Organize files: Co-locate test files with source files
myproject/ ├── feature.sql # Source └── feature_test.sql # Test (in same directory) -
Write tests: Use SQL assertions to verify behavior
DO $$ BEGIN ASSERT my_function(42) = expected_value; END $$;
-
Run tests: Execute with pgcov
pgcov run . -
Generate reports: Choose output format
pgcov report --format=lcov -o coverage.lcov
-- Source: math.sql
CREATE FUNCTION add(a INT, b INT) RETURNS INT AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
-- Test: math_test.sql
DO $$
BEGIN
ASSERT add(2, 3) = 5, 'Addition failed';
END $$;-- Source: log.sql
CREATE PROCEDURE log_event(msg TEXT) AS $$
BEGIN
RAISE NOTICE '%', msg;
END;
$$ LANGUAGE plpgsql;
-- Test: log_test.sql
CALL log_event('Test message');-- Test: user_test.sql
DO $$
BEGIN
-- Setup
CREATE TABLE users (id INT, name TEXT);
INSERT INTO users VALUES (1, 'Alice');
-- Test
ASSERT (SELECT COUNT(*) FROM users) = 1;
-- Teardown (automatic - temp database is destroyed)
END $$;DO $$
BEGIN
-- Test that function raises exception
BEGIN
PERFORM divide_by_zero();
RAISE EXCEPTION 'Should have raised error';
EXCEPTION
WHEN division_by_zero THEN
-- Expected
END;
END $$;# Use defaults
pgcov run .# All options
pgcov run ./... \
--host=localhost \
--port=5432 \
--user=postgres \
--password=secret \
--database=postgres \
--timeout=60s \
--parallel=4 \
--coverage-file=.pgcov/coverage.json \
--verbose# Set connection
export PGHOST=localhost
export PGPORT=5432
export PGUSER=postgres
export PGPASSWORD=secret
# Run with defaults
pgcov run .pgcov report --format=jsonOutput: Machine-readable coverage data
pgcov report --format=lcov -o coverage.lcovOutput: LCOV tracefile for genhtml, Codecov, Coveralls
pgcov report --format=lcov -o coverage.lcov
genhtml coverage.lcov -o coverage_html/
open coverage_html/index.htmlOutput: HTML visualization of coverage
- Use parallel execution:
--parallel=4for faster tests - Increase timeout for slow tests:
--timeout=120s - Run specific directories:
pgcov run ./critical/instead of./... - Use CI caching: Cache Go modules and build artifacts
- Skip verbose locally: Only use
--verbosefor debugging
- Co-locate tests and source: Same directory for discovery
- One test file per source file: Clear mapping
- Use descriptive test names:
feature_test.sqlnottest1.sql - Test edge cases: Cover error conditions and boundaries
- Run tests often: Integrate into development workflow
- Set coverage goals: Aim for 80%+ coverage
- Review coverage reports: Identify untested code paths
- Check test files end with
_test.sql - Ensure tests are in specified path
- Use
--verboseto see discovery process
- Verify PostgreSQL is running
- Check connection parameters
- Test with
psqlfirst
- Ensure source files are in same directory as tests
- Check that tests actually execute (use RAISE NOTICE)
- Use
--verboseto see instrumentation
- Increase
--timeoutvalue - Check for infinite loops in code
- Verify database isn't overloaded
- Try the
simple/example first - Adapt for your project structure
- Set up CI/CD with
ci-integration/examples - Explore coverage reports
- Iterate to improve coverage
Have a useful example? Contribute!
- Create directory with descriptive name
- Add source files, test files, README
- Test the example works
- Update this README with new example
- Submit pull request
- Documentation:
docs/ - Issues: GitHub Issues
- Discussions: GitHub Discussions