Creating and Running an Automatic Unit Test on a Data Class

This document provides information on how to create and run an automatic unit test on a specific data class.

Principles

The process described below demonstrates how to create a simple script file to:

To do so:

  1. Open your script editor and select your workspace.
  2. Create a new Sage X3 source file within your project.
  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. At the start of each test run, append the following code to your source file to remove an existing record from the data class:
    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. Append the following code to your source file to insert a new record in the data class.
    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. Append the following code to your source file to read and test the property values in the new record.
    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. Append the following code to your source file to change property values in the new record.
    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. Type the following code to run the "AXUNIT" script file and write the results of the test in a trace file within Sage X3 for review in your script editor:
    => func QLFXL_CLASS.TESTSUITE
    Note: This code creates trace file "QLFXL_CLASS_XNAME" where "XNAME" is the name of the Sage X3 user who ran the script.

Example script file

This example describes the creation of an "AXUNIT" script file that tests the "AINSERT", "AREAD", and "AUPDATE" methods available in the "XLMBOOK" data class. The "XLMBOOK" data class stores records of books in a Library Management System application.

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

This script will allow you to:

  • Automatically create a new book record.
  • Change some of the values in this record.
  • Attempt to delete the record.
  • Write the results of the test in a Sage X3 trace file for review.

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 has to be created and run.
  3. Create a new Sage X3 source file within the selected project. In this example, create source file "QLFXL_BOOK". Extension ".src" is automatically added.
    You now have an empty script.
  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 will run three test sub routines:
    • "XLMBOOK_AINSERT" with two tests
    • "XLMBOOK_AREAD" with two tests
    • "XLMBOOK_AUPDATE" with three tests
  5. Append the following code to your source file.
    Note: This code removes 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 the book record, and that the values found are the ones you expect.
    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. Enter the following command in the Eclipse console panel to run the script:
    => func QLFXL_BOOK.TESTSUITE
    Trace file "QLFXL_BOOK_XJM" is created in Sage X3, with "XJM" being 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