Unit Test Module for Cerberus X


rcge.unit UML diagram

Here is my unit test module for Cerberus X. It consists of two classes: UnitTest and UnitTestSuite. The UnitTest class is inherited to override the GetName() and Run() methods. Inside the Run() method you can call any of the Assert* methods, and the results will be stored in the UnitTest object. The UnitTestSuite class aggregates the UnitTest class, calls their Run() methods, and prints results to the console.

The module is available for download on my Codeberg as part of Random Cruft Game Engine (RCGE) – my game framework which I’m currently translating to Cerberus X.

Example Source

Import rcge.unit

Class MyTestA Extends UnitTest

    Method GetName:String()
        Return "Integer test"
    End
    
    Method Run:Void()
        AssertEquals(1+1, 2, "One plus one is two")
        AssertEquals(1+2, 3, "One plus two is three")
    End

End

Class MyTestB Extends UnitTest

    Method GetName:String()
        Return "Null test"
    End
    
    Method Run:Void()
        AssertNull(Null, "Null is Null")
    End

End

Class MyTestC Extends UnitTest

    Method GetName:String()
        Return "Object test"
    End
    
    Method Run:Void()
        Local boxA:= BoxInt(10)
        Local boxB:= boxA
        AssertEquals(boxA, boxB, "A = B")
    End

End

Function Main:Int()
    Local testSuite:= New UnitTestSuite()
    testSuite.AddTest(New MyTestA())
    testSuite.AddTest(New MyTestB())
    testSuite.AddTest(New MyTestC())
    testSuite.Run()
    
    Return 0
End

Example Results

================================
        Unit Test Results
================================

Test #1: Integer test
PASS AssertEquals One plus one is two
PASS AssertEquals One plus two is three

Test #2: Null test
PASS AssertNull Null is Null

Test #3: Object test
PASS AssertEquals A = B

Tests passed: 3
Tests failed: 0