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 source file within your project (workspace).

  3. Enter the following code:

############################################################
# AXUNIT TEST SCRIPT                                       #
# Data Class - MYCLASS                                     #
# Test Scope – Basic                                       #
############################################################

Call TESTSUITE
End

Funprog TESTSUITE()
# Start the test suite
    Call TESTSUITE_START("MYCLASS", "MYRECORD TEST") From AXUNIT
    Call ADD_TESTCASE("MYCLASS_AINSERT","Insert a new record",2) From AXUNIT
    Call ADD_TESTCASE("MYCLASS_AREAD","Read a record", 8) From AXUNIT
    Call ADD_TESTCASE("MYCLASS_AUPDATE","Update a record", 3) From AXUNIT
    Local Clbfile RESULT_SUITE
    RESULT_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 SETUP
    Local Integer I
    I=func CLEANUP("EXISTINGRECORD")
ENDFUNC

Funprog CLEANUP(NUMMBR)
    Value Char NUMMBR
    Local Integer I
    Local File MYTABLE [TABLE ALIAS]
    Trbegin [TABLE ALIAS]
    Delete [RECORDNAME] Where MYPROP1 = NUMMBR
    I+=adxdlrec
    Commit
    Close 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_AINSERT
    Local Instance MYRECORD Using C_MYCLASS
    MYRECORD = NewInstance C_MYCLASS AllocGroup Null
    Local Integer OK 
    OK = fmet MYRECORD.AINIT()
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT

    MYRECORD.MYPROP1 = "EXISTINGRECORD"
    MYRECORD.MYPROP2 = "abcdefghij"               
    MYRECORD.MYPROP3 = "nnnnnnnnnn"               
    .
    .
    .
    MYRECORD.MYPROP8 = [dd/mm/yyyy]               
    OK = fmet MYRECORD.AINSERT
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    FreeGroup MYRECORD
End
  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_AREAD
    Local Instance MYRECORD Using C_MYCLASS
    MYRECORD = NewInstance C_MYCLASS AllocGroup null
    OK= fmet MYRECORD.AREAD("EXISTINGRECORD")
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    Call LOG_LINE("Verify the values read") From AXUNIT
    Call CHECK_EQUAL(MYRECORD.MYPROP1,"EXISTINGRECORD") From AXUNIT
    Call CHECK_EQUAL(MYRECORD.MYPROP2,"abcdefghij") From AXUNIT
    Call CHECK_EQUAL(MYRECORD.MYPROP3,"nnnnnnnnnn") From AXUNIT
    .
    .
    .
    FreeGroup MYRECORD
End
  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_AUPDATE
    Local Instance MYRECORD Using C_MYCLASS
    MYRECORD = NewInstance C_MYCLASS AllocGroup Null
    OK= fmet MYRECORD.AREAD("EXISTINGRECORD")
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    MYRECORD.MYPROP2 = "Changed by AXUNIT"
    OK= fmet MYRECORD.AUPDATE
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    Call LOG_LINE("Verify values changed") From AXUNIT
    Call CHECK_EQUAL(MYRECORD.MYPROP2,"Changed by AXUNIT") From AXUNIT
    FreeGroup MYRECORD
End
  8.  To run the 'AXUNIT' script file and write the results of the test to a Trace file within Sage X3 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 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 project (or use an existing Sage X3 project).

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

  3.  Create a new Sage X3 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 TESTSUITE
End

Funprog TESTSUITE()
# Start the test suite
    Call TESTSUITE_START("XLMBOOK", "XLMBOOK TEST") From AXUNIT
    Call ADD_TESTCASE("XLMBOOK_AINSERT","Insert a new Library Book",2) From AXUNIT
    Call ADD_TESTCASE("XLMBOOK_AREAD","Read a book", 8) From AXUNIT
    Call ADD_TESTCASE("XLMBOOK_AUPDATE","Update a book", 3) From AXUNIT
    Local Clbfile RESULT_SUITE
    RESULT_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 SETUP
    Local Integer I
    I=func CLEANUP("B20")
ENDFUNC

Funprog CLEANUP(NUMMBR)
    Value Char NUMMBR
    Local Integer I
    Local File XLMBOOK [XLMBOOK]
    Trbegin [XLMBOOK]
    Delete [XLMBOOK] Where IDBOOK = NUMMBR
    I+=adxdlrec
    Commit
    Close 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_AINSERT
    Local Instance MYBOOK Using C_XLMBOOK
    MYBOOK = NewInstance C_XLMBOOK AllocGroup Null
    Local Integer OK 
    OK = fmet MYBOOK.AINIT()
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT

    MYBOOK.IDBOOK = "B20"
    MYBOOK.TITLE = "Auto Generated Book"        # A        data type
    MYBOOK.ISBN = "12345678910"                 # DCT      data type
    MYBOOK.BKPRICE = 20.00                      # DCB      data type
    MYBOOK.BKGENRE = 4                          # M 13001  data type
    #BOOK.BKSTATUS = "2"                        # M 13002  data type
    MYBOOK.DDC = "654"                          # A        data type
    MYBOOK.DATEPUR = [15/03/2013]               # D        data type
    OK = fmet MYBOOK.AINSERT
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    FreeGroup MYBOOK
End
  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_AREAD
    Local Instance MYBOOK Using C_XLMBOOK
    MYBOOK = NewInstance [MYB] AllocGroup Null
    OK= fmet MYBOOK.AREAD("B20")
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    Call LOG_LINE("Verify the values read") From AXUNIT
    Call CHECK_EQUAL(MYBOOK.IDBOOK,"B20") From AXUNIT
    Call CHECK_EQUAL(MYBOOK.TITLE,"Auto Generated Book") From AXUNIT
    Call CHECK_EQUAL(MYBOOK.ISBN,"12345678910") From AXUNIT
    Call CHECK_EQUAL(MYBOOK.BKPRICE,20.00 ) From AXUNIT
    Call CHECK_EQUAL(MYBOOK.BKGENRE,4) From AXUNIT
    Call CHECK_EQUAL(MYBOOK.BKSTATUS,1) From AXUNIT
    FreeGroup MYBOOK
End
  8.  Append the following code to your source file:
Subprog XLMBOOK_AUPDATE
    Local Instance MYBOOK Using C_XLMBOOK
    MYBOOK = NewInstance C_XLMBOOK AllocGroup Null
    OK= fmet MYBOOK.AREAD("B20")
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    MYBOOK.TITLE = "Changed by AXUNIT"
    OK= fmet MYBOOK.AUPDATE
    Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
    Call LOG_LINE("Verify values changed") From AXUNIT
    Call CHECK_EQUAL(MYBOOK.TITLE,"Changed by AXUNIT") From AXUNIT
    FreeGroup MYBOOK
End
  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:

QLFXL_BOOK_XJM

    Notes: The suffix 'XJM' is the name of the Sage X3 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 TEST

2013-08-05T16:56:49.718: Start test case - Insert a new Library Book
1.1 - check equal - OK: 0
1.2 - check equal - OK: 0
success=2, failure=0, elapsed=40ms

2013-08-05T16:56:49.758: Start test case - Read a book
2013-08-05T16:56:49.768: Verify the values read
2.1 - check equal - OK: 0
2.2 - check equal - OK: 'B20'
2.3 - check equal - OK: 'Auto Generated Book'
2.4 - check equal - OK: '12345678910'
2.5 - check equal - OK: 20
2.6 - check equal - OK: 4
2.7 - check equal - OK: 1
success=7, failure=0, elapsed=13ms
Mismatch number of assertions: expected 8 got 7

2013-08-05T16:56:49.772: Start test case - Update a book
2013-08-05T16:56:49.795: Verify values changed
3.1 - check equal - OK: 0
3.2 - check equal - OK: 0
3.3 - check equal - OK: 'Changed by AXUNIT'
success=3, failure=0, elapsed=25ms