10. Tests, migrations, and delivery
Objectives
- write scenario-focused module tests
- design forward-only schema migrations
- build and inspect the distributable artifact
Mental model
Production readiness is evidence: repeatable tests, a rehearsed upgrade, explicit permissions, a restorable backup, and an installable artifact from CI.
Guided practice
- Start with ModuleTestCase activation.
- Test behavior, permissions, states, and multi-company boundaries.
- Put schema/data migrations in register with existence checks.
- Upgrade a copy containing previous-version data.
- Build the wheel, install it in a clean environment, and run tests there.
Minimal example
from trytond.modules.company.tests import CompanyTestMixin
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
class LibraryTestCase(CompanyTestMixin, ModuleTestCase):
module = 'library'
@with_transaction()
def test_book_requires_title(self):
pool = self.pool
Book = pool.get('library.book')
book = Book()
self.assertFalse(book.title)
# Typical verification commands
python -m unittest trytond.modules.library.tests.test_module
python -m build
python -m twine check dist/*
How to read the example
A test named only test_create is weak unless it states the business outcome. Include a regression test for every fixed defect and test the installed wheel, not only the source checkout.
Exercise
Create a pre-upgrade fixture, rename a field safely, migrate its values, and reconcile record counts before and after.
Run the exercise first with a minimal valid case, then add an invalid case and turn both into repeatable tests.
Verifiable result
Fresh install and upgrade both pass; the wheel contains XML, CSV, translations, and views; rollback is a database restore rather than a guessed reverse migration.
Common mistakes
- testing only happy paths
- using production as the first migration run
- publishing without inspecting wheel contents
- considering compilation proof of business compatibility
Ready-to-advance criteria
- I can explain the concepts without looking at the code.
- Valid and invalid cases have tests.
- I tested with a non-administrative user.
- I know how to upgrade and restore the training database.
Version and references
Series 8.0 requires at least Python 3.10 according to official metadata. The official server declares its project with pyproject.toml; new modules should use modern packaging and verify wheel contents. Do not confuse this historical minimum with the Python version certified by your organization.
This edition pins examples and dependencies to series 8.0. Check the official 8.0 tutorial, server API, and source branch before moving a pattern to production.