{"title":"Create test runner codeunits","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-testrunner-codeunits/","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/devenv-testrunner-codeunits, 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/devenv-testrunner-codeunits","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":"Learn how to create test runner codeunits in AL to manage the execution of test codeunits and integrate with test management or test reporting frameworks.","date":"2026-08-24","meta":{"topic":"how-to"},"outline":[{"level":1,"title":"Create test runner codeunits","slug":"create-test-runner-codeunits","at":0,"chars":2818},{"level":2,"title":"Example","slug":"example","at":1642,"chars":1127},{"level":2,"title":"Related information","slug":"related-information","at":2769,"chars":49}],"text":"# Create test runner codeunits\nYou can create test runner codeunits to manage the execution of test codeunits and to integrate with test management or test reporting frameworks. By integrating with a test management framework, you can automate your tests and enable them to run unattended.\nTo create a test runner codeunit, create a codeunit and set the properties/devenv-subtype-codeunit-property to TestRunner.\nTo specify what changes in the database you want to roll back after the tests in the test runner codeunit execute, set the properties/devenv-testisolation-property.\nTest runner codeunits include the following triggers:\n triggers-auto/codeunit/devenv-onrun-codeunit-trigger\n triggers-auto/codeunit/devenv-onbeforetestrun-codeunit-trigger\n triggers-auto/codeunit/devenv-onaftertestrun-codeunit-trigger\nIn the OnRun trigger, you'll enter the code to run the codeunits. It runs when you execute the codeunit and before the test methods run. You can use the OnBeforeTestRun and the OnAfterTestRun triggers to perform preprocessing and postprocessing, such as initialization or logging test results. If you implement the OnBeforeTestRun trigger, then it executes before each test method executes. If you implement the OnAfterTestRun trigger, then it executes after each test method executes and also suppresses the automatic display of the results message.\n> WARNING\n> The OnBeforeTestRun and OnAfterTestRun triggers always run in their own transactions, regardless of the value of the properties/devenv-testisolation-property, the value of the TransactionModel Property<properties/devenv-properties>, or the outcome of a test method.\n## Example\nThis sample codeunit runs three test codeunits in the automated application test libraries.\n```al\ncodeunit 50101 TestRunnerCodeunit\n{\n    Subtype = TestRunner;\n\n    trigger OnRun()\n    begin\n        Codeunit.Run(Codeunit::\"ERM Vendor Statistics\");\n        Codeunit.Run(Codeunit::\"ERM Sales Quotes\");\n        Codeunit.Run(Codeunit::\"ERM Dimension\");\n    end;\n}\n```\nYou may want to define your test suite in a table and then write code in the test runner codeunit to iterate through the items in the table. And then run each test codeunit. In that case, you can make use of the following example.\n```al\ncodeunit 50102 EnabledTestRunnerCodeunit\n{\n    Subtype = TestRunner;\n\n    trigger OnRun()\n    var\n        EnabledTestCodeunit: Record \"CAL Test Enabled Codeunit\";\n        AllObj: Record AllObj;\n    begin\n        if EnabledTestCodeunit.FindSet() then\n            repeat\n                if AllObj.Get(AllObj.\"Object Type\"::Codeunit, EnabledTestCodeunit.\"Test Codeunit ID\") then\n                    Codeunit.Run(EnabledTestCodeunit.\"Test Codeunit ID\");\n            until EnabledTestCodeunit.Next() = 0;\n    end;\n}\n```\n## Related information\ndevenv-testing-application"}