Unit Testing

Purpose
Unit Testing is a common technique in object oriented programming (OOP). The developer writes test cases for business logic elements. As in object oriented programming the application is built up from classes and objects, unit testing is performed on classes and objects. Test cases can be combined to test suites that cover a certain requirement. The big benefit from unit testing is that it can be automated using unit testing frameworks. Unit testing and testing frameworks are required for a test-driven development approach. Test driven development is common in agile software development like Extreme Programming. Where you first write the the test scenarios that must be accomplished, and afterwards write feature code that passes the test cases.



Unit Testing in Dynamics AX
Microsoft ships Dynamics AX 4 with a unit testing framework. The test framework allows developers to write unit tests almost like in JUnit and combine them in test suites. Especially for and ERP environment Microsoft has added multiple transaction isolation levels for the test framework. It is impossible to define transactions around a unit test so that a test run doesn't effect the original data. It is even possible to let the tests run in a new company account. Therefore the original data is never touched by a test case.

Implementation
To write a Unit Test first create a new test project. That's a special project type in Dynamics AX 4. In the projects window right click on the "shared" node and select "new" and "test project". The system creates a new test project with a test suite in it. A test suite is a group node for classes. Create a new class that extends the SysTestCase. /** Test a calculator class */ class TestCalculator extends SysTestCase { } The SysTestCase extends the SysTestAssert class. Thats where all the Assert methods are defined. Create a new test method that checks the basic operations of the Calculator class. A test method must start with the prefix "test". The test class doesn't compile at the moment because the feature code doesn't exist yet.



/** Check the basic operations */ public void testOperations {   Calculator c = new Calculator(3); ;   this.assertEquals(10,c.add(7));  // 3 + 7 = 10 this.assertEquals(8,c.sub(2));  // 10 - 2 = 8 this.assertEquals(4,c.div(2));  // 8 / 2 = 4 this.assertEquals(12,c.mul(3)); // 4 * 3 = 12 } Now its time to write feature code. Create a new class Calculator with methods for the basic arithmetic operations and a constructor that accepts a number as initial value. /** Calculator for basic Integer operations */ class Calculator { int value;

void new(int _value = 0) {   ;    value = _value; }

public int add(int x) { ;   value = value + x;    return x;  }

public int sub(int x) { ;   value = value - x;    return x;  }

public int mul(int x) { ;   value = value * x;    return x;  }

public int div(int x) { ;   value = value / x;    return x;  } } After developing the feature code the test case compiles and can be run the first time. Do this by right click and choose from the context menu "Add In" and "Run Test". Dynamics AX provides you with a lot of information and even code coverage information. The result can be written to the infolog, database or XML file.



Regression Testing
Regression tests are a common technique in quality assurance on a running project. While optimizing an application it is important to make sure not to destroy already working feature code. This can be done by combining the Version Control System with a test suite. In the settings dialog for the version control system you can append a test project. Modifications can only be checked in if the test project succeeds.



Examples
The Sudoku game use unit testing.