Thursday, July 5, 2018

UNIT TEST CASE FRAMEWORK in Dynamics AX

Attributes:

•SysTestMethodAttribute (Applies: Method) - 
 Indicates that a method is a unit test.
•SysTestCheckInTestAttribute (Applies: Method or class) - 
 Indicates the test is a check-in unit test. 
        A check-in test is run when checking in code 
        to a version control system to ensure a proper
        level of quality.
•SysTestNonCheckInTestAttribute (Applies: Method) - 
 Indicates the test is not a check-in test.
•SysTestTargetAttribute(<name>,<type>)(Applies: class) - 
 Indicates the application object that is being 
        tested by the test case. Examples of tested objects
        are class, table, and form

Code:
Class1:
[SysTestTargetAttribute('Bid Management System', 
                   UtilElementType::Class)]
class BMSTest extends SysTestCase
{
ClsBMS  clsBMS; //Class for master bid management system
}
Override setup method
public void setUp()
{    
    clsBMS = new ClsBMS("name");
    //Create an master bid management system instance to 
      use in test cases
    super();
}
[SysTestMethodAttribute]
void testBidName()
{    
    this.assertEquals("your name", clsBMS.name()); 
    //Verify that the bid supplier name is set correctly
}
Class2:
class ClsBMS         
{
    Name name;
}
Public void new(Name _name)
{
    name = _name;
}
public Name name()
{
    // Return the expected string.
    return name;
}
Compile both class
Run a test case
/AOT/Class/BMSTest/ Right click (Add-ins) and click run tests

Refer: 
https://community.dynamics.com/ax/b/goshoom/archive/2015/09/07/unit-testing-in-ax-basics
Reference content:
http://codingchamp.blogspot.com/2012/10/unit-test-framework-in-microsoft.html
The Unit Test framework is tightly integrated into the MorphX IDE of 
Microsoft Dynamics AX. This integration is very important 
to test-driven development (TDD) because a unit test can be created alongside 
the feature code it is testing.

What is a Unit Test:

A unit test is code that verifies that feature code has been implemented correctly. If you adhere to the principles of TDD, it is best for the developer who is writing the feature code to first write the unit tests. This puts emphasis on how feature code is consumed and creates a more user friendly application programming interface (API). A unit test, in the context of the Unit Test framework, includes test cases, how test cases are staged with data, and the organization of test cases. A test case is a class that extends the SysTestCase Class class. You can add test methods to test each requirement of the feature code. You should execute test cases when code is changed. 

Some Important Points:

Every test case class should extend  SysTestCase Class.

For testing purpose you can write multiple methods for single actual method in code.

For Classes their should only one class for testing a single class.

Name the test class same as the class followed by test in the end.

Name of the method in the test class should also contains test  in the start.

We write Unit Test for classes and tables only.

Always Testing will be done on Blank Database.

The attribute that must be placed on the unit test case class is:

[SysTestTargetAttribute('SysDictTable',UtilElementType::Class)]

The attribute that must be  placed above the method of the test case is:

[SysTestMethodAttribute]

Methods for Setting Data:

Two types of methods are use for setting up data.These are:

1. Setup Method.

2 Setup Test Case Method.

1.Setup Method:

Setup Method runs before every Test method.For some times we usaually want fresh data so we write code in setup method.It works like this:First setup method executed than test method executed than tear down method executed than again setup method executed with fresh data than second test method executed and so on.

2.Setup Test Case Method:

Setup Test case method runs only one time when test class is executed.In case of setup test case method it executed only one time and than tear down method for setup test case method will execute in the end.

note:Due to some issues we usually dont use i.e override setup test case method we only use setup method.

Tear Down Method:

Tear Down method is basically use to flush out data.We normally dont override this method.It manages Automatically.

A test method will typically contain several assertions that are required to hold true,for the test to be successful.

Note: The use of the assertEquals method.This method,and other assert methods,are available as part of the SysTestCase framework.These methods also have an optional string parameter called _message,to specify the message that would go into the infolog.If the assertion fails.The following is list of available methods:


Sample XPO:
https://1drv.ms/f/s!Asi_SJ2dXQ3BqFIfQV_E3dK6VxHm

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...