How to success writing Test Code: next steps
Jalmaraz
667
Automating testing in product is mandatory to do, and wise to do it well. Is very important to increase the amount of tests with new features and maintain this test suite. To get these, once you are in the way, you can find some problems:
A-Start them. The first test will cost. This is a problem, but the real problem to fix is if the second and the third cost too much work too. We can fix this by making good libraries.
B-Dependent tests.
C-Random errors
Libraries
A test suite in a new app, must have these components: testing codeunits and libraries.
Libraries are non-type test codeunits:
codeunit 69000 "Library Warehouse Assistant"
{
Subtype=Normal;
Every app must have at least its library codeunit to ease the making of the fixture of the test. What is the fixture of a test? Is the “Done” section (remember Done-Then-When) of the test code. If you are creating a new discount and testing it, your test library or a standard library, must contain functions to create a customer, create a sales doc, and in your own test library a new function to create your new discount setting it in a simple way. The libraries cause in your tests these consequences:
.Your first app function test will cost more than you expect.
.Your next tests, and new app error tests, are very easy to do. So, a good library is worth inversion of time.
Dependent tests
If you run a test in combination with others, and then this test fails, we have a dependent test. In my case a lot of time was involved with the Random test libraries. We know the real random doesn´t exists, and this causes that when we are creating a random code for a master table if do not care in the next function, very often the “random” function will give us the same code and cause a duplicate key error that we did not expect:
So, to get a new expert table key Instead “RandText” from Library Random:
Item."No." := LibraryRandom.RandText(20);
Always use this from Library Utiliy:
Item."No." := LibraryUtility.GenerateRandomCode20(Item.FieldNo("No."),Item.RecordId.TableNo);
Why? Because GenerateRandomCode20, search a random code that do not exists yet in the table field (The function of standard Microsoft test library):
procedure GenerateRandomCode20(FieldNo: Integer; TableNo: Integer): Code[20]
var
RecRef: RecordRef;
FieldRef: FieldRef;
begin
// Create a random and unique 20 code for the any code field.
RecRef.Open(TableNo, false, CompanyName);
Clear(FieldRef);
FieldRef := RecRef.Field(FieldNo);
repeat
FieldRef.SetRange(PadStr(GenerateGUID, FieldRef.Length, '0'));
until RecRef.IsEmpty;
exit(FieldRef.GetFilter);
end;
Random errors
When sometimes a test work and other times doesn´t. Possible causes:
. The Company already has data for your own app. Be sure that this is not happening, using a clean Cronus demo company.
. Centralize decimal rounding method. The error could be caused by a little difference of decimals that sometimes raises, but other times don´t.
So, these simple tricks can avoid many problems in your test code.
*This post is locked for comments