Test Framework
QFieldCloud uses Django’s built-in test framework with a custom test runner:- Test Runner:
QfcTestSuiteRunner(seedocker-app/qfieldcloud/testing.py) - Database: Separate test database created automatically
- Isolation: Each test runs in a transaction that’s rolled back
Setup Test Environment
Configure for Testing
Add the test override file to yourCOMPOSE_FILE in .env:
Rebuild Stack
Rebuild to install test dependencies fromrequirements_test.txt:
Running Tests
Run All Tests
--keepdb flag preserves the test database between runs for faster execution.
Run Specific Test Module
Test a specific module (e.g., permissions):Run Specific Test Case
Test a specific test class:Run Specific Test Method
Test a single test method:Test Structure
Tests are organized by Django app:Writing Tests
Basic Test Example
API Test Example
Testing with Files
Testing Permissions
Test Coverage
Generate Coverage Report
Run tests with coverage:View Coverage Report
Display coverage in terminal:Generate HTML Coverage Report
htmlcov/. You can view it by opening htmlcov/index.html in a browser.
Coverage Configuration
Coverage settings are indocker-app/.coveragerc.
Parallel Test Instance
You can run a test instance in parallel with your development instance.Create .env.test
Build Test Stack
Run Tests on Test Stack
Test Database
Test Database Configuration
The test database is automatically created with the name defined inPOSTGRES_DB_TEST environment variable.
In settings.py:218-221:
Access Test Database
Update your~/.pg_service.conf:
Best Practices
1. Use setUp() and tearDown()
Create fixtures in setUp(), clean up in tearDown():
2. Use --keepdb for Speed
Always use --keepdb to avoid recreating the test database:
3. Test One Thing at a Time
Each test method should test one specific behavior:4. Use Descriptive Test Names
Test names should describe what they test:5. Use Assertions Effectively
6. Mock External Services
Useunittest.mock for external dependencies:
Continuous Integration
QFieldCloud tests are run on every pull request. Ensure:- All tests pass locally before pushing
- New features include tests
- Test coverage doesn’t decrease
- Tests run in reasonable time
Next Steps
- Debugging - Learn to debug tests
- Contributing - Submit your changes
- Architecture - Understand the codebase