gambas-source-code/comp/src/gb.test/.src/TestSuite/Test.class

124 lines
3.6 KiB
Text
Raw Normal View History

' Gambas class file
Export
Create Static
2020-04-07 16:48:46 +02:00
''' The static procedure Test.Main() starts test(s).
'' Runs all tests in all testcontainers and prints the result to the console.
2020-04-07 16:48:46 +02:00
'' With Test.Main(NameTestModule) the tests can be restricted to only those of a single test module.
'' With Test.Main(NameTestModule, NameProcedure) only a single test can be accomplished.
Public Sub Main(Optional Tests As String)
Assert.Reset() ' only if you run this Main multiple times per process, which you shouldn't
RunTests(Tests)
PrintSummary()
End
Private Sub PrintSummary()
With Assert.Session
Assert.Note(Subst$(("Test run '&1' &2"), .Summary.Description, IIf(.Summary.Success, "PASSED", "FAILED")))
If .TestsRun <> .Plan Then Assert.Note(Subst$(("Planned &1 tests but ran &2"), .Plan, .TestsRun))
If Not .Summary.Success Then ShowFailures(.Summary.Subtests, "")
End With
End
Private Sub ShowFailures(Tests As TestAssertion[], Prefix As String)
Dim hTest As TestAssertion
Dim sName As String
For Each hTest In Tests
sName = Prefix &/ hTest.Description
' Only show the deepest subtests that caused failures.
If Not hTest.Success And If Not hTest.Subtests.Count Then Assert.Note(Subst$(("FAILED &1"), sName))
ShowFailures(hTest.Subtests, sName)
Next
End
'
'' Run all tests, optional limited by Container or TestCaseName. Track contains .
Private Function RunTests(Tests As String)
Dim aTestCommands As TestCommand[]
Dim sTestModule As String
Dim TestModule As Class
Dim Suite As New TestSuite
aTestCommands = TestCommand.ParseCommands(Tests)
2020-04-10 13:52:56 +02:00
'FIXME: SingleTestModule, NameProcedure do not exist any more, they are replaced by aTestCommands
Dim NameProcedure, SingleTestModule As String
For Each sTestModule In GetAllTestModules(aTestCommands)
TestModule = Class.Load(sTestModule)
Suite.AddAllTestCases(TestModule, aTestCommands)
Next
Assert.Session.Summary.Description = Tests
Suite.Run()
If Not Assert.Finished Then Assert.Finish()
End
2020-04-10 13:52:56 +02:00
''
Function GetAllTestModules(Commands As TestCommand[]) As String[]
Dim TestClass As Class
Dim TestModuleNames As New String[]
Dim sNames As New String[]
Dim sName As String
Dim Command As TestCommand
If Exist(".../.test")
sNames = Split(File.Load(".../.test"), gb.Lf, Null, True)
Endif
Assert sNames
sNames.Sort
For Each sName In sNames
TestClass = Class.Load(sName)
If Not TestClass Then Error.Raise(Subst$(("Could not load test module '&1'"), sName))
'sName = Class.Stat(sName).Name
'Atention: Class.Stat(sName).Name does not work if included as component
'Then this creates the error:
2020-04-07 16:48:46 +02:00
'Bail out! Error in Test->GetAllTestModuleNames: Unknown symbol 'Stat' in class 'Class'
If TestModuleNames.Exist(sName) Then Continue
If Commands.Count = 0 Then
'Add every Testmodule
TestModuleNames.Add(sName)
Else
' Add only testmodules whose names exist in Commands
For Each Command In Commands
If Lower(Command.ModuleName) = Lower(sName) Then
TestModuleNames.Add(sName)
Endif
Next
' If Lower(sName) = Lower(SingleTestModule) Then
' TestModuleNames.Add(sName)
' Endif
Endif
Next
TestModuleNames.Sort
' Print "# These TestContainer will be executed:\n#\n# " & TestModuleNames.Join("\n# ") & "\n"
Return TestModuleNames
Catch
Assert.BailOut("Error in " & Error.Where & ": " & Error.Text)
Quit 1
End