@@ -16,6 +16,7 @@ A comprehensive unit testing framework for the 4D platform with enhanced reporti
1616- [ CI/CD Integration] ( #cicd-integration )
1717- [ Framework Architecture] ( #framework-architecture )
1818- [ Best Practices] ( #best-practices )
19+ - [ Transaction Management] ( #transaction-management )
1920- [ License] ( #license )
2021
2122## Features
@@ -655,6 +656,98 @@ $t.assert.areEqual($t; "active"; $user.status; "Status check failed")
655656- Use setup/teardown for test data preparation
656657- Avoid shared state between tests
657658
659+ # # Transaction Management
660+
661+ The framework provides automatic transaction management for test isolation and manual transaction control for advanced scenarios.
662+
663+ # ## Automatic Transaction Management
664+
665+ By default, each test runs in its own transaction that is automatically rolled back after completion, ensuring :
666+ - **Test Isolation**: Tests cannot interfere with each other's data
667+ - **Clean Environment**: Database state is restored after each test
668+ - **No Side Effects**: Failed tests don't leave partial data
669+
670+ # ## Controlling Transaction Behavior
671+
672+ Use comment-based annotations to control transaction behavior :
673+
674+ ` ` ` 4d
675+ Function test_withTransactions($t : cs.Testing)
676+ // Automatic transactions enabled (default)
677+ // Test data changes will be rolled back
678+
679+ Function test_withoutTransactions($t : cs.Testing)
680+ // #transaction: false
681+ // Disables automatic transaction management
682+ ` ` `
683+
684+ # ## Manual Transaction Control
685+
686+ The Testing context provides methods for manual transaction management :
687+
688+ ` ` ` 4d
689+ Function test_manualTransactions($t : cs.Testing)
690+ // #transaction: false
691+
692+ // Start transaction manually
693+ $t.startTransaction()
694+
695+ // Check transaction status
696+ If ($t.inTransaction())
697+ // Perform database operations
698+ End if
699+
700+ // Validate or cancel transaction
701+ If ($success)
702+ $t.validateTransaction()
703+ Else
704+ $t.cancelTransaction()
705+ End if
706+
707+ Function test_transactionWrapper($t : cs.Testing)
708+ // #transaction: false
709+
710+ // Execute operation within transaction (auto-rollback)
711+ var $success : Boolean
712+ $success:=$t.withTransaction(Formula(
713+ // Database operations here
714+ // Will be rolled back automatically
715+ ))
716+
717+ // Execute operation with validation (persists data)
718+ $success:=$t.withTransactionValidate(Formula(
719+ // Database operations here
720+ // Will be validated if test succeeds
721+ ))
722+ ` ` `
723+
724+ # ## Transaction Control Comments
725+
726+ | Comment | Effect |
727+ | ------------------------ | ---------------------------------------- |
728+ | `// # transaction: false` | Disables automatic transactions |
729+ | No comment | Enables automatic transactions (default) |
730+
731+ # ## Transaction Management Methods
732+
733+ The `$t` (Testing) context provides these transaction management methods :
734+
735+ - ` $t.startTransaction()` - Begins a new transaction manually
736+ - ` $t.validateTransaction()` - Commits the current transaction
737+ - ` $t.cancelTransaction()` - Rolls back the current transaction
738+ - ` $t.inTransaction()` - Returns true if currently in a transaction
739+ - ` $t.withTransaction(formula)` - Executes formula in transaction with automatic rollback
740+ - ` $t.withTransactionValidate(formula)` - Executes formula in transaction with commit on success
741+
742+ # ## Best Practices
743+
744+ - Use automatic transactions (default) for most unit tests to ensure isolation
745+ - Disable transactions (`// # transaction: false`) only when testing transaction-related functionality
746+ - Use manual transaction control for integration tests that need to verify data persistence
747+ - Always check `$t.inTransaction()` before performing transaction operations
748+ - Use `withTransaction()` for operations that should always rollback
749+ - Use `withTransactionValidate()` for operations that should persist on test success
750+
658751# # License
659752
660753MIT License - see LICENSE file for details.
0 commit comments