This is to
show how to write a unit test case in Ax 7 for a simple class.
I have created a
class to do simple arithmetic calculation called “MyCalculator” and created “MyCalculatorUnitTest”
unit test class. In this test class shown three ways of adding the test
methods.
1)
Adding [SysTestMethodAttribute] attribute
2)
Prefix the case with “test” in the method name
3)
Adding [SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)] attribute.
After creating
test cases it can be viewed in Test Explorer (Test > Windows> Test
Explorer) as shown below.
Main Class:
class MyCalculator
{
real
valueA,valueB;
public void setValueA(real _a)
{
valueA = _a;
}
public void setValueB(real _b)
{
valueB = _b;
}
public real sumOfValues(real _a = valueA,real _b = valueB)
{
return _a + _b;
}
public real differenceOfValues(real _a = valueA,real _b = valueB)
{
return _a - _b;
}
public real muliplicationOfValues(real _a = valueA,real _b = valueB)
{
return _a * _b;
}
public real divisionOfValues(real _a = valueA,real _b = valueB)
{
if (_b == 0)
{
return 0;
}
return _a / _b;
}
public real modulusOfValues(real _a = valueA,real _b = valueB)
{
if (_b == 0)
{
return 0;
}
return _a mod _b;
}
}
Unit Test Class:
class MyCalculatorUnitTest extends SysTestCase
{
[SysTestMethodAttribute]
public void summationTest()
{
MyCalculator calc = new MyCalculator();
calc.setValueA(10);
calc.setValueB(4);
this.assertEquals(14, calc.sumOfValues());
this.assertEquals(9, calc.sumOfValues(6,3));
}
[SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)]
public void subtractionTest()
{
MyCalculator calc = new MyCalculator();
calc.setValueA(10);
calc.setValueB(4);
this.assertEquals(6, calc.differenceOfValues());
this.assertEquals(3, calc.differenceOfValues(6,3));
}
public void testMuliplicationOfValues()
{
MyCalculator calc = new MyCalculator();
calc.setValueA(10);
calc.setValueB(4);
this.assertEquals(40, calc.muliplicationOfValues());
this.assertEquals(18, calc.muliplicationOfValues(6,3));
}
[SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)]
public void divisionTest()
{
MyCalculator calc = new MyCalculator();
calc.setValueA(10);
calc.setValueB(4);
this.assertEquals(2.5, calc.divisionOfValues());
this.assertEquals(2, calc.divisionOfValues(6,3));
}
public void testModulusOfValues()
{
MyCalculator calc = new MyCalculator();
calc.setValueA(10);
calc.setValueB(4);
this.assertEquals(2, calc.modulusOfValues());
this.assertEquals(0, calc.modulusOfValues(6,3));
}
}
You can find
ax wiki on unit test here.
No comments:
Post a Comment