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

114 lines
3.1 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()
'needed for debugging, to doubleclick on it
Dim Session As TapContext
Session = Assert.Session
With Session
Assert.Print(gb.Lf) ' better readability for humans
Assert.Note(Subst$(("&2 - ran: '&1' "), .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)
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
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))
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
Endif
Next
TestModuleNames.Sort
Return TestModuleNames
Catch
Assert.BailOut("Error in " & Error.Where & ": " & Error.Text)
Quit 1
End