How to create and run an automatic unit test on a data class

This ‘How-to’ provides information on how to create and run an automatic unit test on a specific data class.

The following development process demonstrates how to create a simple script file to:

An example script file is provided at the end of this page to demonstrate how to create an 'AXUNIT' script file to test the 'AINSERT', 'AREAD' and 'AUPDATE' methods available in a specific data class ‘XLMBOOK’. The 'XLMBOOK' data class stores records of books in a Library Management System application (for example, a physical library where books can be borrowed, not a ‘programming language’ library).

An example trace file is also provided to demonstrate the results of the example script file.

To create a simple script file:

  1. Open your script editor and select your workspace.

  2. Create a new Sage X3 People source file within your project (workspace).

  3. Enter the following code:

############################################################# AXUNIT TEST SCRIPT## Data Class - MYCLASS ## Test Scope – Basic#############################################################Call TESTSUITEEndFunprog TESTSUITE()# Start the test suiteCall TESTSUITE_START("MYCLASS", "MYRECORD TEST") From AXUNITCall ADD_TESTCASE("MYCLASS_AINSERT","Insert a new record",2) From AXUNITCall ADD_TESTCASE("MYCLASS_AREAD","Read a record", 8) From AXUNITCall ADD_TESTCASE("MYCLASS_AUPDATE","Update a record", 3) From AXUNITLocal Clbfile RESULT_SUITERESULT_SUITE=func AXUNIT.RUN_TESTSUITE("MYCLASS", "MYRECORD TEST")End RESULT_SUITE
  4.  To remove an existing record from the data class, at the start of each test run, append the following code to your source file:
Subprog SETUPLocal Integer II=func CLEANUP("EXISTINGRECORD")ENDFUNCFunprog CLEANUP(NUMMBR)Value Char NUMMBRLocal Integer ILocal File MYTABLE [TABLE ALIAS]Trbegin [TABLE ALIAS]Delete [RECORDNAME] Where MYPROP1 = NUMMBRI+=adxdlrecCommitClose File [TABLE ALIAS]End I
  5.  To insert a new record into the data class, append the following code to your source file:
    Note: This code runs two 'AINSERT' test sub routines.
Subprog MYCLASS_AINSERTLocal Instance MYRECORD Using C_MYCLASSMYRECORD = NewInstance C_MYCLASS AllocGroup NullLocal Integer OK OK = fmet MYRECORD.AINIT()Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITMYRECORD.MYPROP1 = "EXISTINGRECORD"MYRECORD.MYPROP2 = "abcdefghij" MYRECORD.MYPROP3 = "nnnnnnnnnn" ...MYRECORD.MYPROP8 = [dd/mm/yyyy] OK = fmet MYRECORD.AINSERTCall CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITFreeGroup MYRECORDEnd
  6.  To read and test the property values in the new record, append the following code to your source file:
    Note: This code runs two 'AREAD' test sub routines.
Subprog MYCLASS_AREADLocal Instance MYRECORD Using C_MYCLASSMYRECORD = NewInstance C_MYCLASS AllocGroup nullOK= fmet MYRECORD.AREAD("EXISTINGRECORD")Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITCall LOG_LINE("Verify the values read") From AXUNITCall CHECK_EQUAL(MYRECORD.MYPROP1,"EXISTINGRECORD") From AXUNITCall CHECK_EQUAL(MYRECORD.MYPROP2,"abcdefghij") From AXUNITCall CHECK_EQUAL(MYRECORD.MYPROP3,"nnnnnnnnnn") From AXUNIT...FreeGroup MYRECORDEnd
  7.  To change property values in the new record, append the following code to your source file:
    Note: This code runs three 'AUPDATE' test sub routines.
Subprog MYCLASS_AUPDATELocal Instance MYRECORD Using C_MYCLASSMYRECORD = NewInstance C_MYCLASS AllocGroup NullOK= fmet MYRECORD.AREAD("EXISTINGRECORD")Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITMYRECORD.MYPROP2 = "Changed by AXUNIT"OK= fmet MYRECORD.AUPDATECall CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITCall LOG_LINE("Verify values changed") From AXUNITCall CHECK_EQUAL(MYRECORD.MYPROP2,"Changed by AXUNIT") From AXUNITFreeGroup MYRECORDEnd
  8.  To run the 'AXUNIT' script file and write the results of the test to a Trace file within Sage X3 People for review, type the following code
        in your script editor:

=> func QLFXL_CLASS.TESTSUITE

    Note: This will create the trace file 'QLFXL_CLASS_XNAME' where 'XNAME' is the name of the Sage X3 People user that ran the script.

Example script file

This example describes how to create an 'AXUNIT' script file to test the 'AINSERT', 'AREAD', and 'AUPDATE' methods available in the ‘XLMBOOK’ data class.

A script file will be created to:

Note: This script file has been created using the Eclipse IDE (Indigo release) editor.

  1. Open Eclipse IDE.

  2. Create a new Sage X3 People project (or use an existing Sage X3 People project).

    Note: The project must point to the Sage X3 People Endpoint (folder) where the script is to be created and ran.

  3.  Create a new Sage X3 People source file within the selected project. This example creates the source file 'QLFXL_BOOK' (the extension ‘src’ is automatically added). You now have an empty script file.

  4.  Enter the following code:

############################################################# AXUNIT TEST SCRIPT## Data Class - XLMBOOK ## Test Scope – Basic############################################################# Call TESTSUITEEndFunprog TESTSUITE()# Start the test suiteCall TESTSUITE_START("XLMBOOK", "XLMBOOK TEST") From AXUNITCall ADD_TESTCASE("XLMBOOK_AINSERT","Insert a new Library Book",2) From AXUNITCall ADD_TESTCASE("XLMBOOK_AREAD","Read a book", 8) From AXUNITCall ADD_TESTCASE("XLMBOOK_AUPDATE","Update a book", 3) From AXUNITLocal Clbfile RESULT_SUITERESULT_SUITE=func AXUNIT.RUN_TESTSUITE("XLMBOOK", "XLMBOOK TEST")End RESULT_SUITE

This code shows that you will be running three test sub routines:

  5.  Append the following code to your source file:

    Note: This code removes our test Book record (B20) from the database at the beginning of each test run.
Subprog SETUPLocal Integer II=func CLEANUP("B20")ENDFUNCFunprog CLEANUP(NUMMBR)Value Char NUMMBRLocal Integer ILocal File XLMBOOK [XLMBOOK]Trbegin [XLMBOOK]Delete [XLMBOOK] Where IDBOOK = NUMMBRI+=adxdlrecCommitClose File [XLMBOOK]End I
  6.  Append the following code to your source file:
    Note: This code tests that you can insert a new book record using the property values shown.
Subprog XLMBOOK_AINSERTLocal Instance MYBOOK Using C_XLMBOOKMYBOOK = NewInstance C_XLMBOOK AllocGroup NullLocal Integer OK OK = fmet MYBOOK.AINIT()Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITMYBOOK.IDBOOK = "B20"MYBOOK.TITLE = "Auto Generated Book" # A data typeMYBOOK.ISBN = "12345678910" # DCT data typeMYBOOK.BKPRICE = 20.00 # DCB data typeMYBOOK.BKGENRE = 4# M 13001 data type#BOOK.BKSTATUS = "2" # M 13002 data typeMYBOOK.DDC = "654"# A data typeMYBOOK.DATEPUR = [15/03/2013] # D data typeOK = fmet MYBOOK.AINSERTCall CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITFreeGroup MYBOOKEnd
  7.  Append the following code to your source file:
    Note: This code tests that you can read our book record and that the values you expect are the values that are found.
Subprog XLMBOOK_AREADLocal Instance MYBOOK Using C_XLMBOOKMYBOOK = NewInstance [MYB] AllocGroup NullOK= fmet MYBOOK.AREAD("B20")Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITCall LOG_LINE("Verify the values read") From AXUNITCall CHECK_EQUAL(MYBOOK.IDBOOK,"B20") From AXUNITCall CHECK_EQUAL(MYBOOK.TITLE,"Auto Generated Book") From AXUNITCall CHECK_EQUAL(MYBOOK.ISBN,"12345678910") From AXUNITCall CHECK_EQUAL(MYBOOK.BKPRICE,20.00 ) From AXUNITCall CHECK_EQUAL(MYBOOK.BKGENRE,4) From AXUNITCall CHECK_EQUAL(MYBOOK.BKSTATUS,1) From AXUNITFreeGroup MYBOOKEnd
  8.  Append the following code to your source file:
Subprog XLMBOOK_AUPDATELocal Instance MYBOOK Using C_XLMBOOKMYBOOK = NewInstance C_XLMBOOK AllocGroup NullOK= fmet MYBOOK.AREAD("B20")Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITMYBOOK.TITLE = "Changed by AXUNIT"OK= fmet MYBOOK.AUPDATECall CHECK_EQUAL(OK,[V]CST_AOK) From AXUNITCall LOG_LINE("Verify values changed") From AXUNITCall CHECK_EQUAL(MYBOOK.TITLE,"Changed by AXUNIT") From AXUNITFreeGroup MYBOOKEnd
  9.  To run the script, enter the following command into the Eclipse console panel:

=> func QLFXL_BOOK.TESTSUITE

  10.  The following trace file will be created in Sage X3 People:

QLFXL_BOOK_XJM

    Notes: The suffix 'XJM' is the name of the Sage X3 People user that ran the script.
                The user name was set when the Eclipse project was created.

Example trace file

2013-08-05T16:56:49.713: Start suite - QLFXL_BOOK - XLMBOOK - XLMBOOK TEST2013-08-05T16:56:49.718: Start test case - Insert a new Library Book1.1 - check equal - OK: 01.2 - check equal - OK: 0success=2, failure=0, elapsed=40ms2013-08-05T16:56:49.758: Start test case - Read a book2013-08-05T16:56:49.768: Verify the values read2.1 - check equal - OK: 02.2 - check equal - OK: 'B20'2.3 - check equal - OK: 'Auto Generated Book'2.4 - check equal - OK: '12345678910'2.5 - check equal - OK: 202.6 - check equal - OK: 42.7 - check equal - OK: 1success=7, failure=0, elapsed=13msMismatch number of assertions: expected 8 got 72013-08-05T16:56:49.772: Start test case - Update a book2013-08-05T16:56:49.795: Verify values changed3.1 - check equal - OK: 03.2 - check equal - OK: 03.3 - check equal - OK: 'Changed by AXUNIT'success=3, failure=0, elapsed=25ms