Dynamics Online

Microsoft Business Apps and Microsoft Cloud

Setting up test data for unit Testing

We should make sure that the unit tests should be independent of data as much possible. For this purpose the Unit Test framework provided by David Pokluda also talks about two very important methods. setUp() and tearDown(). These are very important methods provided by all unit testing framework designed for any technology. setUp() deals with all those operations that needs to be performed in the beginning of all testing and tearDown() should be overridden clear all those things.
For example you want to write unit test for some calculateTotalcost() method written on some table TransactionDetails. You want to setup test data in the table. Following can be the code sample.

class MyTest extends SysTestCase
{
public void testMethod()
{
TransactionDetails transDetails;
;
transDetails.transId = ’01’;

this.assertsEqual(500,transDetails.calculateTotalcost());
}

void setUp()
{
TransactionDetails transDetails;
;
transDetails.transId = ’01’;
transDetails.Quantity = 2;
transDetails.Price = 250;
transDetails.insert();
}

void tearDown()
{
TransactionDetails transDetails;
;
transDetails.transId = ’01’;
transDetails.delete();
}
}

So, we are actually setting the test data in setup method and then calling assertEquals on that. Finally we are deleting the test record.

About fahahm