{"title":"OnBeforeTestRun (Codeunit) trigger","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/triggers-auto/codeunit/devenv-onbeforetestrun-codeunit-trigger/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/triggers-auto/codeunit/devenv-onbeforetestrun-codeunit-trigger, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/triggers-auto/codeunit/devenv-onbeforetestrun-codeunit-trigger","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Runs before a test of a test codeunit is run.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":208,"chars":931},{"level":3,"title":"Parameters","slug":"parameters","at":366,"chars":773},{"level":2,"title":"Return value","slug":"return-value","at":1139,"chars":141},{"level":2,"title":"Remarks","slug":"remarks","at":1280,"chars":930},{"level":2,"title":"Example","slug":"example","at":2210,"chars":363},{"level":2,"title":"Related information","slug":"related-information","at":2573,"chars":205}],"text":"> Version: _Available or changed with runtime version 1.0._\nRuns before a test of a test codeunit is run.\n> IMPORTANT\n> The ../../properties/devenv-subtype-property must be set to TestRunner in the Codeunit.\n## Syntax\n```al\ntrigger OnBeforeTestRun(CodeunitId: Integer; CodeunitName: Text; FunctionName: Text; Permissions: TestPermissions): Ok\nbegin\n    ...\nend;\n```\n### Parameters\nCodeunitId\nType: ../../methods-auto/integer/integer-data-type\nSpecifies the ID of the codeunit to be run.\nCodeunitName\nType: ../../methods-auto/text/text-data-type\nThe name of the test codeunit to be run.\nFunctionName\nType: ../../methods-auto/text/text-data-type\nSpecifies the name of the test to be run.\nPermissions\nType: ../../methods-auto/testpermissions/testpermissions-option\nSpecifies the permission set level to use on the test run. The value is retrieved from the TestPermissions property of the test codeunit or test that is run. The parameter accepts the following values:\n Disabled\n Restrictive\n NonRestrictive\n InheritFromTestCodunit - Specifies that a test the uses the TestPermissions property setting of the test codeunit to which it belongs.\n## Return value\nOk\nType: ../../methods-auto/boolean/boolean-data-type\ntrue if the test was run, otherwise, false. The default value is true.\n## Remarks\n> NOTE\n> This trigger is optional and not available on a test runner codeunit by default. To implement this trigger, you must manually add it.\nA test runner codeunit manages the execution of test codeunits that are run from its devenv-onrun-codeunit-trigger. When a test codeunit runs, it executes each test in the codeunit, one at a time. The OnBeforeTestRun trigger is called before the test codeunit, the OnRun , and each test .\nYou can use the OnBeforeTestRun triggers to perform preprocessing, such as general initialization and logging, or to automate tests by integrating the test runner codeunit with a test management framework.\nBy using the Permissions parameter, you can add code to control which permission sets, if any, to test.\nThe FunctionName parameter is empty when the OnBeforeTestRun trigger is called for the entire test codeunit.\nThe OnBeforeTestRun trigger is run in its own database transaction.\n## Example\nThe following OnBeforeTestRun trigger code initializes a logging variable and returns true to indicate that the test should execute.\n```al\ntrigger OnBeforeTestRun(CodeunitId: Integer, CodeunitName: Text, FunctionName: Text, Permissions: TestPermissions): Boolean\nvar\n    Before : DateTime;\nbegin\n    Before := CURRENTDATETIME;\n    Exit(true);\nend;\n```\n## Related information\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>\ndevenv-onaftertestrun-codeunit-trigger\n../../properties/devenv-testpermissions-property"}