# OnAfterTestRun (Codeunit) trigger
> Version: _Available or changed with runtime version 1.0._
Runs after a test of a test codeunit has been run.
> IMPORTANT
> The ../../properties/devenv-subtype-property must be set to TestRunner in the Codeunit.
## Syntax
```al
trigger OnAfterTestRun(CodeunitId: Integer; CodeunitName: Text; FunctionName: Text; Permissions: TestPermissions; Success: Boolean)
begin
    ...
end;
```
### Parameters
CodeunitId
Type: ../../methods-auto/integer/integer-data-type
Specifies the ID of the codeunit that has run.
CodeunitName
Type: ../../methods-auto/text/text-data-type
Specifies the name of the test codeunit that has run.
FunctionName
Type: ../../methods-auto/text/text-data-type
Specifies the name of the test that has run.
Permissions
Type: ../../methods-auto/testpermissions/testpermissions-option
Specifies 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:
 Disabled
 Restrictive
 NonRestrictive
 InheritFromTestCodunit - Specifies that a test the uses the TestPermissions property setting of the test codeunit to which it belongs.
Success
Type: ../../methods-auto/boolean/boolean-data-type
True indicates that the test run succeeded; otherwise, false indicates that the test run failed.
## Remarks
> NOTE
> This trigger is optional and not available on a test runner codeunit by default. To implement this trigger, you must manually add it.
A 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.
The OnAfterTestRun trigger suppresses the automatic display of the results message after the test codeunit runs.
> NOTE
> To return the error message for a failed test run, use the ../../methods-auto/debugger/debugger-getlasterrortext-method.
You 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.
The Permissions parameter, enables you can control how to handle applied permission sets, if any, after the test is run.
The FunctionName parameter is empty when the OnAfterTestRun trigger is called for the whole test codeunit.
The OnAfterTestRun trigger is run in its own database transaction.
## Example
The following OnAfterTestRun trigger code logs test results to a test reporting system. This example requires that you create a record variable named log.
```al
log.Init;
log.UnitId := CodeunitId;
log.Unit := CodeunitName;
log.Func := MethodName;
log.Before := Before;
log.After := CURRENTDATETIME;
if Success then
  log.Status := log.Status::Success
else begin
  log.Status := log.Status::Failure;
  if MethodName <> '' then
    log.Message := GETLASTERRORTEXT;
end;
log.Insert(true);
```
The GetLastErrorText returns the text that was contained in the last error message.
## Related information
Get Started with AL<../../devenv-get-started>
Developing Extensions<../../devenv-dev-overview>
../../properties/devenv-subtype-codeunit-property
devenv-onbeforetestrun-codeunit-trigger

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. https://jonas-paprotka.de/docs/dev-itpro/developer/triggers-auto/codeunit/devenv-onaftertestrun-codeunit-trigger/
