How to: Categorize Test Cases Using Attributes

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The unit test framework uses the attribute feature that is found in the X++ language. The purpose of an attribute is to represent or store metadata about types and methods. Use test attributes to do the following tasks:

  • Specify the target class or target table for your test

  • Specify which methods on the unit test class are the test methods

  • Specify which tests are the tests for the check in process and are integrated with a version control system

This topic provides a summary of the attributes and how they are used in code. For the complete steps, see Walkthrough: Testing a Class Using the Unit Test Framework. The following table describes the attributes that are predefined by the unit test framework.

Test attribute

Description

Where the attribute can be applied

SysTestMethodAttribute

Indicates that a method is a test method. A test method must be public, void, and take no parameter values.

Apply the attribute to a method.

SysTestCheckInTestAttribute

Indicates that the test is a check in test. A check in test is run when you check in code to a version control system to make sure that there is a good level of quality.

Apply the attribute to a method or class.

SysTestNonCheckInTestAttribute

Indicates the test is not a check in test. This is used when the class is marked with the attribute SysTestCheckInAttribute but it has one or more methods that should not be check in methods.

Apply the attribute to a method or class.

SysTestTargetAttribute(<name>,<type>)

Indicates the application object that is being tested by the test case. Examples of objects to test are class, table, and form.

This attribute takes two parameters:

  • Name - string value name of the code element to test.

  • Type - the type of the code element to test. If the type is not specified, the Unit Test framework treats it as a class.

Apply the attribute to a class.

SysTestInactiveTestAttribute

Indicates that the test is deactivated.

Apply the attribute to a method or class.

Examples

To specify the target class for your test

  • This example illustrates how to attach an attribute to a class to specify that it should be used for a test. This example attaches the SysTestTargetAttribute attribute to specify the class that is tested is the Employee class.
        [SysTestTargetAttribute(classStr(Employee), UtilElementType::Class)]
        class EmployeeTest extends SysTestCase
        {
        }
    

To specify which methods on the unit test class are test methods

  • The following code example illustrates how to attach an attribute to a method to indicate that it is a test method.
      [SysTestMethodAttribute]
      public void myTest()
      {
      }

To specify which tests are tests for check in and integrate with a version control system

  • You can run and classify the most critical test methods as check in tests. When you set up the version control system, you can specify which test project to run during check in. You can also specify whether to run all test methods, or only the test methods that are marked as check in tests. If all tests pass, the code will be submitted in the version control system. For more information about version control, see Version Control System.The following code example illustrates how to attach an attribute to a method to indicate that it is a check in test method.
      [SysTestCheckInTestAttribute]
      public void myCheckInTest()
      {
      }

See also

Unit Test Framework

Attributes on X++ Types and Methods

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.