mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Adds internal cross-links to docs guides ## Testing - Prettier passes on the changed lines. - Docs build passes (`pnpm build --filter=docs`). - Docs link e2e run locally against the production build for every changed page across this PR. --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
100 lines
3.5 KiB
Plaintext
100 lines
3.5 KiB
Plaintext
---
|
|
id: 'local-development-testing-overview'
|
|
title: 'Testing Overview'
|
|
description: 'Learn how to develop and test database schemas, tables, functions, and Row Level Security (RLS) policies.'
|
|
---
|
|
|
|
Testing is a critical part of database development, especially when working with features like Row Level Security (RLS) policies. This guide provides a comprehensive approach to testing your Supabase database.
|
|
|
|
## Testing approaches
|
|
|
|
### Database unit testing with pgTAP
|
|
|
|
[pgTAP](https://pgtap.org) is a unit testing framework for Postgres. It tests database structure, Row Level Security (RLS) policies, functions and procedures, and data integrity.
|
|
|
|
For more information about writing and running pgTAP tests for an RLS policy, see [Test Row Level Security policies with pgTAP](/docs/guides/database/postgres/testing-row-level-security).
|
|
|
|
### Application-Level testing
|
|
|
|
Application-level tests check your policies end to end, through the same client your app uses.
|
|
|
|
For more information about testing RLS policies from TypeScript, see [Test the policy from your application](/docs/guides/database/postgres/testing-row-level-security#test-the-policy-from-your-application).
|
|
|
|
### Continuous integration testing
|
|
|
|
Set up automated database testing in your CI pipeline:
|
|
|
|
1. Create a GitHub Actions workflow `.github/workflows/db-tests.yml`:
|
|
|
|
```yaml
|
|
name: Database Tests
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
branches: [main]
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup Supabase CLI
|
|
uses: supabase/setup-cli@v1
|
|
|
|
- name: Start Supabase
|
|
run: supabase start
|
|
|
|
- name: Run Tests
|
|
run: supabase test db
|
|
```
|
|
|
|
## Best practices
|
|
|
|
1. **Test Data Setup**
|
|
- Use begin and rollback to ensure test isolation
|
|
- Create realistic test data that covers edge cases
|
|
- Use different user roles and permissions in tests
|
|
|
|
2. **RLS Policy Testing**
|
|
- Test Create, Read, Update, Delete operations
|
|
- Test with different user roles: anonymous and authenticated
|
|
- Test edge cases and potential security bypasses
|
|
- Always test negative cases: what users should not be able to do
|
|
|
|
3. **CI/CD Integration**
|
|
- Run tests automatically on every pull request
|
|
- Include database tests in deployment pipeline
|
|
- Keep test runs fast using transactions
|
|
|
|
## Real-World examples
|
|
|
|
For more complex, real-world examples of database testing, check out:
|
|
|
|
- [Database Tests Example Repository](https://github.com/usebasejump/basejump/tree/main/supabase/tests/database) - A production-grade example of testing RLS policies
|
|
- [RLS Guide and Best Practices](https://github.com/orgs/supabase/discussions/14576)
|
|
|
|
## Troubleshooting
|
|
|
|
Common issues and solutions:
|
|
|
|
1. **Test Failures Due to RLS**
|
|
- Ensure you've set the correct role `set local role authenticated;`
|
|
- Verify JWT claims are set `set local "request.jwt.claims"`
|
|
- Check policy definitions match your test assumptions
|
|
|
|
2. **CI Pipeline Issues**
|
|
- Verify [Supabase CLI](/docs/guides/local-development/cli/getting-started) is properly installed
|
|
- Ensure database migrations are run before tests
|
|
- Check for proper test isolation using transactions
|
|
|
|
## Additional resources
|
|
|
|
- [pgTAP Documentation](https://pgtap.org)
|
|
- [Supabase CLI Reference](/docs/reference/cli/supabase-test)
|
|
- [pgTAP Supabase reference](/docs/guides/database/extensions/pgtap?queryGroups=database-method&database-method=sql#testing-rls-policies)
|
|
- [Database testing reference](/docs/guides/database/testing)
|