SQLCheck turns SQL files into CI-grade tests with inline expectations. It scans SQL test source
files, extracts directives like {{ success(...) }} or {{ fail(...) }}, executes the compiled
SQL against a target database using SQLAlchemy, and reports per-test results with fast, parallel
execution.
- Directive-based expectations:
{{ success(...) }}and{{ fail(...) }}directives define expected behavior directly inside SQL test files. - Deterministic parse/compile stage: Directives are stripped to produce executable SQL plus
structured
sql_parsedstatement metadata. - Parallel execution: Run tests concurrently with a configurable worker pool (default: 5).
- CI-friendly outputs: Clear per-test failures, non-zero exit codes, and JSON/JUnit reports.
- Extensible assertions: Register custom functions via plugins.
uv tool install pysqlcheckSQLAlchemy requires a database-specific driver (dialect) package. Install the one for your database, for example:
# Snowflake
uv tool install pysqlcheck[snowflake]Common optional extras (mirrors popular SQLAlchemy dialects) include: databricks, mssql, duckdb, oracle...
If you need a different database dialect, install the SQLAlchemy driver for it directly. See https://docs.sqlalchemy.org/en/20/dialects/ for the full list and driver guidance.
Install the latest version directly from GitHub using uv:
uv tool install git+https://github.com/luisggc/sqlcheckTo install from a specific branch:
uv tool install git+https://github.com/luisggc/sqlcheck@branch-namegit clone <repo-url>
cd sqlcheck
uv sync
source .venv/bin/activateuv sync creates .venv by default and installs the sqlcheck entry point into it.
- Python 3.11+
- SQLAlchemy-compatible database connection
- Create a SQL test file (default pattern:
**/*.sql):
-- tests/example.sql
{{ success(name="basic insert") }}
CREATE TABLE t (id INT);
INSERT INTO t VALUES (1);
SELECT * FROM t;- Run sqlcheck with a database connection:
# Option 1: Set a default connection (no -c flag needed)
export SQLCHECK_CONN_DEFAULT="sqlite:///tmp/sqlcheck.db"
sqlcheck run tests/
# Option 2: Use a named connection
export SQLCHECK_CONN_DEV="sqlite:///tmp/sqlcheck.db"
sqlcheck run tests/ --connection dev
# Short flag works too
sqlcheck run tests/ -c dev
# Option 3: Pass a direct URL
sqlcheck run tests/ -c "sqlite:///tmp/sqlcheck.db"If any test fails, sqlcheck exits with a non-zero status code.
See Connection configuration for more options including YAML files.
Directives are un-commented blocks in the SQL source:
{{ success(name="my test", tags=["smoke"], timeout=30, retries=1) }}
{{ fail(match="'permission denied' in error_message") }}
{{ assess(match="stdout == 'ok' && rows.size() == 1") }}
{{ assess(match="status == 'fail' && 'type error' in error_message") }}
{{ assess(check="stdout.matches('^ok') && returncode == 0") }}success(...): Asserts the SQL executed without errors. Optionalmatchexpressions add further checks.fail(...): Asserts the SQL failed. Optionalmatchexpressions add further checks.assess(...): Evaluates a CEL (Common Expression Language) expression supplied via the requiredmatch(orcheck) argument. The expression must evaluate totrue.
CEL variables available to match:
status:"success"or"fail".success: Boolean success flag.returncode: Integer return code.error_code: String version of the return code.duration_s: Execution duration in seconds.elapsed_ms: Execution duration in milliseconds.stdout: Captured stdout.stderr: Captured stderr.error_message: Alias for stderr.rows: Query result rows as a list of lists.output: Nested object withstdout,stderr, androws.sql: Full SQL source (directives stripped).statements: List of parsed SQL statements.statement_count: Count of parsed SQL statements.
Common CEL expressions:
- Contains text:
stdout.contains("warning") - Regex match:
stdout.matches("^ok")ormatches(stdout, "^ok") - Comparisons:
returncode != 0,statement_count >= 1 - Row assertions:
rows.size() == 1,rows[0][0] > 0 - Status checks:
status == "success",success == true
If no directive is provided, sqlcheck defaults to success(). The name parameter is optional;
when omitted, the test name defaults to the file path.
SQL files are rendered with Jinja before directives are extracted, so you can use template
variables inside the SQL source (for example {{ schema }} or {{ limit }}). Supply values via
--vars (repeatable key=value pairs).
sqlcheck run tests/ --vars schema=public --vars limit=10Undefined variables raise a template error.
sqlcheck run TARGET [options]--pattern: Glob for discovery (default:**/*.sql).--workers: Parallel worker count (default: 5).--connection,-c: Connection name forSQLCHECK_CONN_<NAME>lookup.--vars,-v: Template variables inkey=valueformat (repeatable).--json: Write JSON report to path.--junit: Write JUnit XML report to path.--plan-dir: Write per-test plan JSON files to a directory.--plugin: Load custom expectation functions (repeatable).
SQLCheck resolves connection URIs in this order (first match wins):
- Default connection (when
-cis omitted) - Direct URL (if contains "://")
- Environment variables
- YAML configuration files
SQLCheck supports two environment variable prefixes:
SQLCHECK_CONN_{NAME}— SQLAlchemy URL for a named connectionDTK_CONN_{NAME}— Alternative prefix for compatibilitySQLCHECK_CONN_DEFAULT— Default connection used when-cis omittedDTK_CONN_DEFAULT— Alternative default connection
Connection names are normalized by converting to uppercase and replacing non-alphanumeric characters with underscores.
Example:
export SQLCHECK_CONN_DEFAULT="sqlite:///tmp/sqlcheck.db"
export SQLCHECK_CONN_SNOWFLAKE_PROD="snowflake://user:pass@account/db/schema"
# Uses default connection
sqlcheck run tests/
# Uses snowflake_prod connection
sqlcheck run tests/ --connection snowflake_prodFor better organization and to avoid exposing credentials in environment variables, you can use a YAML configuration file.
Default locations (checked in order):
~/.config/sqlcheck/connections.yaml~/.dtk/connections.yml
You can override the location using:
export SQLCHECK_CONNECTIONS_FILE="/path/to/your/connections.yaml"Example ~/.config/sqlcheck/connections.yaml:
dev:
drivername: postgresql
username: myuser
password: ${DB_PASSWORD} # Environment variables are expanded
host: localhost
port: 5432
database: testdb
snowflake_prod:
drivername: snowflake
username: my_user
password: ${SNOWFLAKE_PASSWORD}
host: my_account
database: ANALYTICS
schema: PUBLIC
query:
warehouse: COMPUTE_WH
role: ANALYST
# Simple URL format also works
local: "sqlite:///tmp/local.db"Usage:
# Uses connection defined in YAML
sqlcheck run tests/ --connection dev
# Environment variables in YAML are expanded
export DB_PASSWORD="secret123"
sqlcheck run tests/ --connection devYou can also pass a connection URL directly (useful for testing):
sqlcheck run tests/ --connection "sqlite:///tmp/test.db"- JSON: machine-readable summary of each test and its results.
- JUnit XML: CI-friendly test report format.
- Plan files: per-test JSON containing statement splits, directives, and metadata.
uv sync --extra devCreate a Python module with a register(registry) function:
# my_plugin.py
from sqlcheck.function_context import current_context
from sqlcheck.models import FunctionResult
def register(registry):
def assert_rows(min_rows=1, **kwargs):
context = current_context()
# Implement logic here based on stdout/stderr or engine-specific output
return FunctionResult(name="assert_rows", success=True)
registry.register("assert_rows", assert_rows)Run with:
sqlcheck run tests/ --plugin my_pluginpython -m unittest discover -s tests