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 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. 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 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. 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_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. 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_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. 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_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. 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:

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 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 will run three test sub routines:
  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 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 the book record, and that the values found are the ones you expect.
    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. 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 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