- Open your AL project in Visual Studio Code.
- In Explorer, create a new file named My Test Library.al. In the Editor, create a new codeunit object:
codeunit 50151 "My Test Library"
{
}
- Add the following function to the codeunit:
procedure CreateTelevisionShow(var TelevisionShow: Record "Television Show")
var
LibraryUtility: Codeunit "Library - Utility";
begin
CLEAR(TelevisionShow);
TelevisionShow.Validate(Code, LibraryUtility
.GenerateRandomCode20(TelevisionShow.FieldNo(Code),
Database::"Television Show"));
TelevisionShow.VALIDATE(Name, TelevisionShow.Code);
TelevisionShow.MODIFY(TRUE);
end;
- Add the following function to the codeunit:
procedure CreateTelevisionShowCode(): Code[20]
var
TelevisionShow: Record "Television Show";
begin
CreateTelevisionShow(TelevisionShow);
exit(TelevisionShow.Code);
end;
- And finally, add the following function to the codeunit:
procedure DeleteTelevisionShow(TelevisionShowCode: Code[20])
var
TelevisionShow: Record "Television Show";
begin
TelevisionShow.Get(TelevisionShowCode);
TelevisionShow.Delete(true);
end;
- At this point, we've created a library of basic functions to handle the Television Shows application. Now, we have a set of functions that any test can use, and we're certain that any time a Television Show record is created or deleted, it will be done the same way.
You can also see that we created two different functions to create a new record. One requires you to pass in an actual TelevisionShow: Record variable, but with the other one, you can simply call it and use the return value to identify the new record that was created. Creating standards like this will provide you with more flexibility in writing your tests.