2019-11-15 22:33:54 +01:00
|
|
|
' Gambas class file
|
|
|
|
|
|
|
|
''' The TestSuite class represents a suite of different tests to be run. The TestSuite contains
|
|
|
|
''' a part-whole hierarchy of objects that implement the ITest interface --
|
|
|
|
''' including TestCase objects and other TestSuite objects. Executing the Run method for the
|
|
|
|
''' TestSuite will execute all test cases that it contains. The TestSuite class also provides
|
2020-02-23 12:38:53 +01:00
|
|
|
''' methods for add all test cases contained in a TestModule object into the suite.
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Inherits ITest
|
|
|
|
|
|
|
|
Property Read Tests As ITest[]
|
|
|
|
Private $Tests As ITest[]
|
|
|
|
|
|
|
|
Public Sub _new()
|
|
|
|
|
|
|
|
$Tests = New ITest[]
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
'' Runs all tests contained within the collection and collects the result in the Track parameter.
|
|
|
|
Public Sub Run()
|
|
|
|
|
|
|
|
Dim oTest As ITestCase
|
2020-02-23 12:38:53 +01:00
|
|
|
Dim CurrentTestModule, LastTestModule As Class
|
2019-11-15 22:33:54 +01:00
|
|
|
Dim CurrentAction As String
|
|
|
|
|
|
|
|
Assert $Tests
|
|
|
|
|
|
|
|
For Each oTest In $Tests
|
2020-02-23 12:38:53 +01:00
|
|
|
CurrentTestModule = oTest.TestModule
|
|
|
|
If LastTestModule Then
|
|
|
|
If LastTestModule <> CurrentTestModule Then
|
|
|
|
CurrentAction = LastTestModule.Name & ":TearDownTestModule"
|
|
|
|
StopTestModule(LastTestModule)
|
|
|
|
CurrentAction = CurrentTestModule.Name & ":SetupTestModule"
|
|
|
|
StartTestModule(CurrentTestModule)
|
2019-11-15 22:33:54 +01:00
|
|
|
Endif
|
|
|
|
Else
|
2020-02-23 12:38:53 +01:00
|
|
|
'Assert.Note("-------------------- " & CurrentTestModule.Name)
|
|
|
|
CurrentAction = CurrentTestModule.Name & ":SetupTestModule"
|
|
|
|
StartTestModule(CurrentTestModule)
|
2019-11-15 22:33:54 +01:00
|
|
|
Endif
|
|
|
|
|
|
|
|
With Track
|
2020-02-23 12:38:53 +01:00
|
|
|
.TestModuleName = oTest.TestModule.Name
|
2019-11-15 22:33:54 +01:00
|
|
|
.TestName = oTest.Name
|
|
|
|
End With
|
|
|
|
|
2019-12-30 22:10:37 +01:00
|
|
|
oTest.Run()
|
2020-02-23 12:38:53 +01:00
|
|
|
LastTestModule = CurrentTestModule
|
2019-11-15 22:33:54 +01:00
|
|
|
Next
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
If LastTestModule Then
|
|
|
|
CurrentAction = LastTestModule.Name & ":TearDownTestModule"
|
|
|
|
StopTestModule(LastTestModule)
|
2019-11-15 22:33:54 +01:00
|
|
|
Endif
|
|
|
|
|
|
|
|
Catch
|
|
|
|
'Bail out
|
2020-01-04 14:48:51 +01:00
|
|
|
Print "Bail out! " & "Unittest Stopped with error \"" & Error.Text & "\" caused by " & CurrentAction & "."
|
2019-11-15 22:33:54 +01:00
|
|
|
Print "# ------- No success! -------"
|
|
|
|
Quit
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Private Sub StartTestModule(TestModule As Class)
|
2019-12-30 22:10:37 +01:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
If TestModule.Symbols.Exist("Setup") Then
|
|
|
|
Object.Call(TestModule, "Setup")
|
|
|
|
Endif
|
|
|
|
' Tracks tests in a TestModule, reset if TestModule changes
|
|
|
|
Track.TestModuleTestsCount = 0
|
2019-12-30 22:10:37 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Private Sub StopTestModule(TestModule As Class)
|
2019-12-30 22:10:37 +01:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Dim Plan As Integer
|
|
|
|
|
|
|
|
If TestModule.Symbols.Exist("TearDown") Then
|
|
|
|
Object.Call(TestModule, "TearDown")
|
|
|
|
Endif
|
|
|
|
|
|
|
|
If TestModule.Symbols.Exist("Plan") Then
|
|
|
|
Plan = Eval(TestModule.Name & ".Plan")
|
|
|
|
Endif
|
|
|
|
|
|
|
|
If Plan > 0 Then
|
|
|
|
Track.Plan += Plan
|
|
|
|
Else
|
|
|
|
Track.Plan += Track.TestModuleTestsCount
|
|
|
|
Endif
|
2019-12-30 22:10:37 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2019-11-15 22:33:54 +01:00
|
|
|
'' Add a object implementing ITest (either a TestCase or TestSuite) to the suite.
|
2020-02-24 14:08:40 +01:00
|
|
|
Private Sub AddTest(oTest As ITest)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Assert $Tests
|
|
|
|
$Tests.Add(oTest)
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
'' Add a TestCase to the suite.
|
2020-02-24 14:08:40 +01:00
|
|
|
Private Sub AddTestCase(oTestCase As ITestCase)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Assert $Tests
|
|
|
|
$Tests.Add(oTestCase, Object.Class(oTestCase).Name)
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
'' Create a new test case and add it to the suite.
|
2020-02-24 14:08:40 +01:00
|
|
|
Private Function AddNewTestCase(sName As String, TestModule As Class)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Dim test As TestCase
|
|
|
|
|
|
|
|
Assert sName <> Null
|
2020-02-23 12:38:53 +01:00
|
|
|
Assert TestModule
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
test = New TestCase(sName, TestModule)
|
2019-11-15 22:33:54 +01:00
|
|
|
Assert $Tests
|
|
|
|
$Tests.Add(test)
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Private Function GetTestsFromTestModule(TestModule As Class) As String[]
|
|
|
|
|
|
|
|
Dim sSymbol As String
|
|
|
|
Dim NoTestSymbols As String[] = ["Setup", "SetupEach", "Teardown", "TeardownEach", "Plan", "ThisIsAnUnitTestSelfTest"]
|
|
|
|
Dim asTests As New String[]
|
|
|
|
|
|
|
|
Assert TestModule
|
|
|
|
|
|
|
|
For Each sSymbol In TestModule.Symbols
|
|
|
|
If Not NoTestSymbols.Exist(sSymbol, gb.IgnoreCase) Then
|
|
|
|
asTests.Add(sSymbol)
|
|
|
|
Endif
|
|
|
|
Next
|
|
|
|
|
|
|
|
Return asTests
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
'' Create all test cases that are contained in the specified TestModule and add them to the suite.
|
|
|
|
Public Function AddAllTestCases(TestModule As Class, Optional NameProcedure As String)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Dim asTests As String[]
|
2019-11-15 22:33:54 +01:00
|
|
|
Dim i As Integer
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Assert TestModule
|
|
|
|
|
|
|
|
asTests = GetTestsFromTestModule(TestModule)
|
|
|
|
asTests.Sort
|
|
|
|
|
2020-02-23 22:31:28 +01:00
|
|
|
' Tests must not be empty
|
|
|
|
|
|
|
|
'Assert asTests.Count > 0 Error "Failed: No tests in " & TestModule.Name
|
|
|
|
If asTests.Count = 0 Then
|
|
|
|
Assert.BailOut("Error: No tests in " & TestModule.Name)
|
|
|
|
Endif
|
2020-02-23 12:38:53 +01:00
|
|
|
|
|
|
|
For i = 0 To asTests.Count - 1
|
|
|
|
If NameProcedure = Null Then
|
|
|
|
AddNewTestCase(CStr(asTests[i]), TestModule)
|
|
|
|
Else
|
|
|
|
' just one TestMethod was called, change plan, if plan exists in TestModule
|
|
|
|
If TestModule.Symbols.Exist("Plan", gb.IgnoreCase) Then
|
|
|
|
Object.SetProperty(TestModule, "Plan", 1)
|
2019-11-15 22:33:54 +01:00
|
|
|
Endif
|
2020-02-23 12:38:53 +01:00
|
|
|
If asTests[i] = NameProcedure Then
|
|
|
|
AddNewTestCase(CStr(asTests[i]), TestModule)
|
|
|
|
Endif
|
|
|
|
Endif
|
|
|
|
Next
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function Tests_Read() As ITest[]
|
|
|
|
|
|
|
|
Return $Tests
|
|
|
|
|
|
|
|
End
|