{"title":"OnAfterTestRun (Codeunit) trigger","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/triggers-auto/codeunit/devenv-onaftertestrun-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-onaftertestrun-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-onaftertestrun-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 after a test of a test codeunit has been run.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":213,"chars":1119},{"level":3,"title":"Parameters","slug":"parameters","at":384,"chars":948},{"level":2,"title":"Remarks","slug":"remarks","at":1332,"chars":1192},{"level":2,"title":"Example","slug":"example","at":2524,"chars":585},{"level":2,"title":"Related information","slug":"related-information","at":3109,"chars":207}],"text":"> Version: _Available or changed with runtime version 1.0._\nRuns after a test of a test codeunit has been run.\n> IMPORTANT\n> The ../../properties/devenv-subtype-property must be set to TestRunner in the Codeunit.\n## Syntax\n```al\ntrigger OnAfterTestRun(CodeunitId: Integer; CodeunitName: Text; FunctionName: Text; Permissions: TestPermissions; Success: Boolean)\nbegin\n    ...\nend;\n```\n### Parameters\nCodeunitId\nType: ../../methods-auto/integer/integer-data-type\nSpecifies the ID of the codeunit that has run.\nCodeunitName\nType: ../../methods-auto/text/text-data-type\nSpecifies the name of the test codeunit that has run.\nFunctionName\nType: ../../methods-auto/text/text-data-type\nSpecifies the name of the test that has 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.\nSuccess\nType: ../../methods-auto/boolean/boolean-data-type\nTrue indicates that the test run succeeded; otherwise, false indicates that the test run failed.\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 one at a time in the codeunit. When implemented, the OnAfterTestRun trigger is called after each test has run and after all of the test codeunits have run.\nThe OnAfterTestRun trigger suppresses the automatic display of the results message after the test codeunit runs.\n> NOTE\n> To return the error message for a failed test run, use the ../../methods-auto/debugger/debugger-getlasterrortext-method.\nYou can use the OnAfterTestRun trigger to perform post-processing, such as logging, or to automate tests by integrating the test runner codeunit with a test management framework.\nThe Permissions parameter, enables you can control how to handle applied permission sets, if any, after the test is run.\nThe FunctionName parameter is empty when the OnAfterTestRun trigger is called for the whole test codeunit.\nThe OnAfterTestRun trigger is run in its own database transaction.\n## Example\nThe following OnAfterTestRun trigger code logs test results to a test reporting system. This example requires that you create a record variable named log.\n```al\nlog.Init;\nlog.UnitId := CodeunitId;\nlog.Unit := CodeunitName;\nlog.Func := MethodName;\nlog.Before := Before;\nlog.After := CURRENTDATETIME;\nif Success then\n  log.Status := log.Status::Success\nelse begin\n  log.Status := log.Status::Failure;\n  if MethodName <> '' then\n    log.Message := GETLASTERRORTEXT;\nend;\nlog.Insert(true);\n```\nThe GetLastErrorText returns the text that was contained in the last error message.\n## Related information\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>\n../../properties/devenv-subtype-codeunit-property\ndevenv-onbeforetestrun-codeunit-trigger"}