Search This Blog

Friday 19 October 2012

Unit Test Framework In Microsoft Dynamics AX 2012

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:



























Now I will show u the simple example on how to use the Unit Testing.

Create a simple class that contains add,subtract and multiply methods.

Now Create the Unit Test to Test this classes.The original class on which I have made these methods is Calculations.Now for unit test first we will override the setup method.



public class calculationtest extends systestcase
{
calculation obj;
}

pulbic void setup()
{

 obj=new calculation();

super();

}

public void testadd()
{
this.assertequal(3,obj.parmadd(1,2));
}
public void testmultiply()
{
this.assrtequal(9,obj.multiply(3,3));
}

Class Declaration:

[SysTestTargetAttribute('calculation',UtilElementType::Class]
public class calculationtest extends SysTestCase
{
calculation obj;
}

Methods:

[SysTestMethodAttribute]
public void testadd()
{
this.asserEquals()3,obj.add(1,2);
}

[SysTestMethodAttribute]
public void testsubtract()
{
this.assertEquals(1,obj.subtract(1,2));
}

[SysTestMethodAttribute]
public void testmultiply()
{
this.assertEquals(9,obj.multiply(3,3));
}

Running Test Cases:

Now right click the test class i,e is calculationtest and than go to add ins than click the run test tests as
you can see in the below image:

 
 
Now after running the unit tests you can see in the above image that the how much methods are runs how much are fail as you can see in the image.
 
 

 

You can also record the code coverage of the code just click on the parameters button as you can see in the above image.

A new screen will open.Now check the checkbox record code coverage and record number of records.as you can see in the below image.






















Now click on the Listener as you can see in the above image and select infolog.This is for when some exception occur than it will be show in the infolog.

Below is the screen for the infolog.

Now close the above form and than again run the unit test and than click the details button as you can see in the 3rd last image and a new form will open and where you can see the code coverage.

The recomended code coverage of the class should be 60%.

You can see the code coverage of my class in the below image:














You can also see the code coverage of the methods also.Just select your recently run testcase and than click the test button as you can see in the above image.

A new form will open which shows the code coverage of the methods as you can see in the below image.

 
Thanks
 
Muhammad Zahid.






2 comments:

  1. Hi,

    What does Listener type XML and Text file do? where does it save the files if selected?

    Thanks,
    Karthik

    ReplyDelete
  2. Why we need blank database for Unit testing? Can you explain me briefly.

    ReplyDelete