From 66ff21de8570e05611377197588633224093c5c2 Mon Sep 17 00:00:00 2001 From: Christof Thalhofer Date: Tue, 20 Sep 2016 23:49:28 +0200 Subject: [PATCH] Tests ok on Commandline --- .component | 2 +- .info | 439 ++++++++++++++++++- .list | 12 +- .project | 4 +- .src/TestMyself/GuTestAnything.class | 4 - .src/TestMyself/GuTestExample1.class | 8 +- .src/TestMyself/GuTestExample2.class | 45 +- .src/TestMyself/GuTestIntentionalError.class | 19 + .src/TestRunner/FmRunner.class | 18 +- .src/TestRunner/MRunner.module | 84 ++-- .src/TestSuite/TestCase.class | 6 +- .src/TestSuite/TestResult.class | 35 +- .src/TestSuite/TestSuite.class | 2 +- .startup | 4 +- 14 files changed, 590 insertions(+), 92 deletions(-) delete mode 100644 .src/TestMyself/GuTestAnything.class create mode 100644 .src/TestMyself/GuTestIntentionalError.class diff --git a/.component b/.component index 690ba1915..3d4c9ab30 100644 --- a/.component +++ b/.component @@ -1,5 +1,5 @@ [Component] Key=gb.deg.unit -Version=0.0.4 +Version=0.0.5 State=2 Needs=XML diff --git a/.info b/.info index c809687aa..6872af557 100644 --- a/.info +++ b/.info @@ -1,6 +1,3 @@ -#GuTestAnything -TestContainer -C #GuTestExample1 TestContainer C @@ -12,7 +9,7 @@ TeardownEach m -TestStringFailure +TestStringNull m @@ -23,14 +20,63 @@ m #GuTestExample2 TestContainer C -TestEmpty +SetupContainer m -TestString +TeardownContainer m +SetupEach +m + + +TeardownEach +m + + +TestFirstTest +m + + +TestThirdTest +m + + +TestSecondTest +m + + +#GuTestIntentionalError +TestContainer +C +TestError +m + + +#ITest + +C +Run +m + +(Result)TestResult;[(ShowDebug)b] +CountTestCases +m +i + +#ITestCase +ITest +C +Name +r +s + +Container +r +TestContainer + #MRunner C @@ -38,10 +84,15 @@ Main M -RunAllTests +RunTests M -[(ShowResultAsDebug)b] +(Result)TestResult;[(ContainerName)s(CaseName)s(ShowDebug)b] +'Run all tests, optional limited by Container or TestCaseName. TestResult contains . +PrintResult +M + +(res)TestResult; ShowTestRunnerForm M @@ -51,3 +102,375 @@ GetAllTestContainerNames M String[] +#TestCase +ITestCase +C +'The TestCase class is responsible for executing a specific test case. +'The test case to be executed is specified through the Name and TestContainer +'properties in the class. The Run method will call the appropriate Setup +'and TearDown methods for the test case as well as executing the test case +'method itself. +Name +r +s + +'Name of the test case +Container +r +TestContainer + +'Reference to the test container containing the test method to be executed. +_new +m + +(sName)s(oTestContainer)TestContainer; +'Initializes the TestCase. Used in lieu of a constructor. +Run +m + +(oTestResult)TestResult;[(ShowDebug)b] +CountTestCases +m +i + +#TestContainer + +C +Name +r +s + +CaseNames +p +String[] + +Case +p +ITestCase + +Result +p +TestResult + +Debug +p +b + +_new +m + +[(ShowDebug)b] +RunCase +m + +(oCase)ITestCase;(oTestResult)TestResult; +SetupEach +m + + +TearDownEach +m + + +SetupContainer +m + + +TearDownContainer +m + + +#TestError + +C +TestCase +p +ITestCase + +'Sets a reference to the test case where the error/failure was generated +ErrNumber +p +l + +'Set the error number (used by test errors, not failures) +Source +p +s + +Description +p +s + +#TestErrors + +C +'The TestErrors class is the collection class for TestError objects. +'It holds the test case failures and errors that have been collected +'by the TestResult object. +Items +r +TestError[] + +_new +m + + +'Initialize variables +Add +m +TestError +(oTestCase)ITestCase;(lNumber)l(sSource)s(sDescription)s +'Add a new error or failure to the error collection. +Count +r +l + +'Return number of TestError objects in the collection. +#TestParameter + +C +Name +p +s + +Value +p +v + +Parameters +p +TestParameters + +#TestParameters + +C +_new +m + + +AddParameter +m + +(oParameter)TestParameter; +Add +m +TestParameter +(sName)s(vValue)v +Count +m +l + +Item +m +TestParameter +(vIndex)v +#TestResult + +C +'The TestResult object collects the results from executing test cases. It is an +'instance of the Collecting Parameter pattern. When new failures or errors +'are added to the TestResult or if a test case is started or finished, the +'TestResult generates events to notify its event handlers about what has happened. +CountRunnedTests +r +i + +'Gets the number of run tests. +Failures +r +TestErrors + +'Returns a collection of failures +Errors +r +TestErrors + +'Returns a collection of errors +Parameters +p +TestParameters + +'Set and Returns parameter collection +:AfterStartTest +: + +(oTestCase)ITestCase; +'Events +:AfterEndTest +: + + +:AfterAddError +: + +(oError)TestError; +:AfterAddFailure +: + +(oError)TestError; +:AfterAddTrace +: + +(sMessage)s +_new +m + + +'Initialize Variables +WasSuccessful +r +b + +'Returns whether the entire test was successful or not. +StartTest +m + +(oTestCase)ITestCase; +'Informs the result that a test will be started. +EndTest +m + + +'Informs the result that a test is completed. +AddFailure +m + +(sDescription)s +'Adds a failure to the collection of failures. +AddError +m + +(lNumber)l(sSource)s(sDescription)s +'Adds a error to the collection of errors. +AddTrace +m + +(sMessage)s +'Add trace message +Assert +m + +(bCondition)b[(sMessage)s] +'Asserts that a condition is true. If it isn't it raises a failure with the given message. +'bCondition: condition to be asserted +'sMessage: optional message describing the asserted condition +AssertEqualsString +m + +(sExpected)s(sActual)s[(sMessage)s] +'Asserts that the expected string equals the actual string. +'sExpected: the expected value +'sActual: the actual value +'sMessage: optional message describing the asserted condition +AssertEqualsLong +m + +(lExpected)l(lActual)l[(sMessage)s] +'Asserts that the expected long value equals the actual long value. +'lExpected: the expected value +'lActual: the actual value +'sMessage: optional message describing the asserted condition +AssertEqualsFloat +m + +(dExpected)f(dActual)f(dDelta)f[(sMessage)s] +'Asserts that the expected Float value equals the actual Float value with delta precision. +'dExpected: the expected value +'dActual: the actual value +'dDelta: tolerated precision +'sMessage: optional message describing the asserted condition +AssertEqualsVariant +m + +(vExpected)v(vActual)v[(sMessage)s] +'Asserts that the expected variant equals the actual variant. +'vExpected: the expected value +'vActual: the actual value +'sMessage: optional message describing the asserted condition +AssertExists +m + +(oObject)o[(sMessage)s] +'Asserts that an object is not nothing +'oObject: object reference +'sMessage: the detail message to record if this assertion fails +AssertEqualsObject +m + +(oExpected)o(oActual)o[(sMessage)s] +'Asserts that the expected object equals the actual object. +'oExpected: expected object reference +'oActual: actual object reference +'sMessage: the detail message to record if this assertion fails +AssertNotEmpty +m + +(vVariant)v[(sMessage)s] +'Asserts that a variant is not empty +'vVariant: variant to evaluate +'sMessage: the detail message to record if this assertion fails +AssertEmpty +m + +(vVariant)v[(sMessage)s] +'Asserts that a variant is empty +'vVariant: variant to evaluate +'sMessage: the detail message to record if this assertion fails +AssertNotNull +m + +(vVariant)v[(sMessage)s] +'Asserts that a variant is not null +'vVariant: variant to evaluate +'sMessage: the detail message to record if this assertion fails +AssertError +m + +(ErrorNumber)l(ErrorMessage)s[(ErrorNumberExpected)l(Message)s] +'Asserts that an error was thrown +'Use it so: +'Try DosomethingThatThrowserror() +'Catch +'AssertError(Error.Code, Error.Text) +#TestSuite +ITest +C +'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 +'methods for add all test cases contained in a test container object into the suite. +_new +m + + +Run +m + +(oTestResult)TestResult;[(ShowDebug)b] +'Runs all tests contained within the collection and collects the result in the TestResult parameter. +CountTestCases +m +i + +'Number of test cases contained in the suite +AddTest +m + +(oTest)ITest; +'Add a object implementing ITest (either a TestCase or TestSuite) to the suite. +AddTestCase +m + +(oTestCase)ITestCase; +'Add a TestCase to the suite. +AddNewTestCase +m + +(sName)s(oTestContainer)TestContainer; +'Create a new test case and add it to the suite. +AddAllTestCases +m + +(oTestContainer)TestContainer; +'Create all test cases that are contained in the specified TestContainer and add them to the suite. diff --git a/.list b/.list index aa6fbf342..6a9d041f0 100644 --- a/.list +++ b/.list @@ -1,4 +1,14 @@ -GuTestAnything GuTestExample1 GuTestExample2 +GuTestIntentionalError +ITest +ITestCase MRunner! +TestCase +TestContainer +TestError +TestErrors +TestParameter +TestParameters +TestResult +TestSuite diff --git a/.project b/.project index bc2525618..4c06aa0a5 100644 --- a/.project +++ b/.project @@ -1,8 +1,8 @@ # Gambas Project File 3.0 # Compiled with Gambas 3.9.90 Title=gb.deg.unit -Startup=FmRunner -Version=0.0.4 +Startup=MRunner +Version=0.0.5 Component=gb.image Component=gb.qt4 Component=gb.form diff --git a/.src/TestMyself/GuTestAnything.class b/.src/TestMyself/GuTestAnything.class deleted file mode 100644 index 538ec4b2e..000000000 --- a/.src/TestMyself/GuTestAnything.class +++ /dev/null @@ -1,4 +0,0 @@ -' Gambas class file - -Inherits TestContainer -Export diff --git a/.src/TestMyself/GuTestExample1.class b/.src/TestMyself/GuTestExample1.class index ea7e9ab89..3a2929ba2 100644 --- a/.src/TestMyself/GuTestExample1.class +++ b/.src/TestMyself/GuTestExample1.class @@ -6,7 +6,7 @@ Export Public Sub SetupEach() If Me.Debug Then - Debug "I did my Setup" + Print "Setup Each" Endif End @@ -14,19 +14,21 @@ End Public Sub TeardownEach() If Me.Debug Then - Debug "I did my Teardown" + Print "Teardown Each" Endif End -Public Sub TestStringFailure() +Public Sub TestStringNull() + Print "GuTestExample1::Test First Test" Me.Result.AssertEmpty(Null, "Diskussion ohne Ergebnis") End Public Sub TestStringOk() + Print "GuTestExample1::Test Second Test" Me.Result.AssertEqualsString("ja", "ja", "Diskussion") End diff --git a/.src/TestMyself/GuTestExample2.class b/.src/TestMyself/GuTestExample2.class index 5baf4ac7c..33dd6cf6a 100644 --- a/.src/TestMyself/GuTestExample2.class +++ b/.src/TestMyself/GuTestExample2.class @@ -3,14 +3,55 @@ Inherits TestContainer Export -Public Sub TestEmpty() +Public Sub SetupContainer() + If Me.Debug Then + Print "Setup Container" + Endif + +End + +Public Sub TeardownContainer() + + If Me.Debug Then + Print "GuTestExample2::Teardown Container" + Endif + +End + +Public Sub SetupEach() + + If Me.Debug Then + Print "GuTestExample2::Setup Each" + Endif + +End + +Public Sub TeardownEach() + + If Me.Debug Then + Print "GuTestExample2::Teardown Each" + Endif + +End + +Public Sub TestFirstTest() + + Print "GuTestExample2::Test First Test" Me.Result.AssertEmpty("", "Empty") End -Public Sub TestString() +Public Sub TestThirdTest() + Print "GuTestExample2::Test Third Test" + Me.Result.AssertEqualsLong(200, 200) + +End + +Public Sub TestSecondTest() + + Print "GuTestExample2::Test Second Test" Me.Result.AddTrace("Tracemessage") End diff --git a/.src/TestMyself/GuTestIntentionalError.class b/.src/TestMyself/GuTestIntentionalError.class new file mode 100644 index 000000000..b3fe3ec8e --- /dev/null +++ b/.src/TestMyself/GuTestIntentionalError.class @@ -0,0 +1,19 @@ +' Gambas class file + +Inherits TestContainer +Export +Private $Result As TestResult + +Public Sub TestError() + + Dim a As Long + + Print "GuTestIntentionalError::TestError" + + Try a = 3 / 0 + + If Error Then + Me.Result.AssertError(Error.Code, Error.Text) + Endif + +End \ No newline at end of file diff --git a/.src/TestRunner/FmRunner.class b/.src/TestRunner/FmRunner.class index f0e2bf822..bd9f321bd 100644 --- a/.src/TestRunner/FmRunner.class +++ b/.src/TestRunner/FmRunner.class @@ -144,22 +144,6 @@ Sub RunTests() CaseName = CboTestCases.List[CboTestCases.Index] Endif - If ContainerName = Null Then - If CaseName = Null Then - For Each ContainerName In MRunner.GetAllTestContainerNames() - Container = Object.New(ContainerName) - Suite.AddAllTestCases(Container) - Next - Endif - Else - Container = Object.New(ContainerName) - If CaseName = Null Then - Suite.AddAllTestCases(Container) - Else - Suite.AddNewTestCase(CaseName, Container) - Endif - Endif - - Suite.Run($Result, True) + MRunner.RunTests($Result, ContainerName, CaseName) End diff --git a/.src/TestRunner/MRunner.module b/.src/TestRunner/MRunner.module index 4582b91e6..dce78fdc5 100644 --- a/.src/TestRunner/MRunner.module +++ b/.src/TestRunner/MRunner.module @@ -4,7 +4,11 @@ Export Public Sub Main() - RunAllTests(True) + Dim res As New TestResult + + RunTests(res, "GuTestIntentionalError", Null, True) + 'RunTests(res) + PrintResult(res) End @@ -17,56 +21,70 @@ Public Sub ShowTestRunnerForm() End -Public Sub RunAllTests(Optional ShowResultAsDebug As Boolean) +Public Sub PrintResult(Res As TestResult) 'Dim Container As TestContainer - Dim Suite As TestSuite - Dim Res As New TestResult + ' Dim Suite As TestSuite + ' Dim Res As New TestResult Dim Errs As TestErrors Dim Fails As TestErrors Dim Err As TestError Dim Fail As TestError - Dim C As Class - - Suite = New TestSuite - - For Each C In Classes - If Left(C.Name, 6) = "GuTest" Then - Suite.AddAllTestCases(Object.New(C.Name)) - Endif - Next - - Suite.Run(Res) + ' Dim C As Class 'TS.Run(Res) Errs = Res.Errors Fails = Res.Failures - If ShowResultAsDebug = True Then - Debug "Number of test cases:";; Suite.CountTestCases() + Print "------------------------------------------------- Test Results" + Print res.CountRunnedTests & " Test done" + Print "------------------------------------------------- " + If Errs.Count > 0 Then + For Each Err In Errs.Items + Print "Error in:";; Err.Source + Print "Error:";; Err.Description + Next + Else + Print "No Errors" + Endif - If Errs.Count > 0 Then - For Each Err In Errs.Items - Debug "Error in:";; Err.Source - Debug "Error:";; Err.Description - Next - Else - Debug "No Errors" - Endif + If Fails.Count > 0 Then + For Each Fail In Fails.Items + Print "Failure in:";; Fail.Source + Print "Failure:";; Fail.Description + Next + Else + Print "No Failures" + Endif + Print "------------------------------------------------- End" - If Fails.Count > 0 Then - For Each Fail In Fails.Items - Debug "Failure in:";; Fail.Source - Debug "Failure:";; Fail.Description +End + +'' Run all tests, optional limited by Container or TestCaseName. TestResult contains . + +Public Sub RunTests(Result As TestResult, Optional ContainerName As String, Optional CaseName As String, Optional ShowDebug As Boolean) + + Dim Container As TestContainer + Dim Suite As New TestSuite + + If ContainerName = Null Then + If CaseName = Null Then + For Each ContainerName In MRunner.GetAllTestContainerNames() + Container = Object.New(ContainerName) + Suite.AddAllTestCases(Container) Next - Else - Debug "No Failures" Endif Else - ' ------------------------------------------------- Show Result with form - + Container = Object.New(ContainerName) + If CaseName = Null Then + Suite.AddAllTestCases(Container) + Else + Suite.AddNewTestCase(CaseName, Container) + Endif Endif + Suite.Run(Result, ShowDebug) + End Public Function GetAllTestContainerNames() As String[] diff --git a/.src/TestSuite/TestCase.class b/.src/TestSuite/TestCase.class index a1f089bd7..d0d7feb40 100644 --- a/.src/TestSuite/TestCase.class +++ b/.src/TestSuite/TestCase.class @@ -48,7 +48,7 @@ Public Sub Run(oTestResult As TestResult, Optional ShowDebug As Boolean) 'On Error Resume Next '' Set up test fixture - $MyContainer.SetupEach + Try $MyContainer.SetupEach '' Run test '' Check for exceptions if Setup or Test method @@ -56,12 +56,12 @@ Public Sub Run(oTestResult As TestResult, Optional ShowDebug As Boolean) oTestResult.AddError(Error.Code, Error.Where & "::Setup", Error.Text) Error.Clear Else - $MyContainer.RunTestCase(Me, oTestResult) + $MyContainer.RunCase(Me, oTestResult) End If '' Tear down test fixture '' must ensure that test fixture is properly torn down if any failures have occurred - $MyContainer.TearDownEach + Try $MyContainer.TearDownEach If Error Then oTestResult.AddError(Error.Code, Error.Where & "::TearDown", error.Text) diff --git a/.src/TestSuite/TestResult.class b/.src/TestSuite/TestResult.class index 05772a079..c23bc7425 100644 --- a/.src/TestSuite/TestResult.class +++ b/.src/TestSuite/TestResult.class @@ -13,7 +13,7 @@ Export ''' the checking of test assertions. '' Gets the number of run tests. -Property Read RunTests As Integer +Property Read CountRunnedTests As Integer '' Returns a collection of failures Property Read Failures As TestErrors @@ -28,7 +28,7 @@ Property Parameters As TestParameters Private $colErrors As TestErrors Private $colFailures As TestErrors Private $colParameters As TestParameters -Private $iRunTests As Integer +Private $iCountRunnedTests As Integer Private $oCurrentTestCase As ITestCase '' Events @@ -43,7 +43,7 @@ Public Sub _new() $colErrors = New TestErrors $colFailures = New TestErrors - $iRunTests = 0 + $iCountRunnedTests = 0 End Sub @@ -62,7 +62,7 @@ End Sub Public Sub EndTest() $oCurrentTestCase = Null - $iRunTests = $iRunTests + 1 + $iCountRunnedTests = $iCountRunnedTests + 1 Raise AfterEndTest End Sub @@ -218,16 +218,21 @@ Public Sub AssertNotNull(vVariant As Variant, Optional sMessage As String) End Sub '' Asserts that an error was thrown -'' lError: error number to compare -Public Sub AssertEqualsError(oError As Error, Optional lError As Long, Optional sMessage As String) +'' Use it so: +'' +'' Try DosomethingThatThrowserror() +'' Catch +'' AssertError(Error.Code, Error.Text) - If (lError = 0 And oError.Number = 0) Then - AddFailure(sMessage & " - Expected Error did not occur.") - Else If (lError <> 0 And oError.Number <> lError) Then - AddFailure(sMessage & - " - Expected Error (" & lError & ") but Error (" & - oError.Number & ") was thrown instead. Description: " & - oError.Description) +Public Sub AssertError(ErrorNumber As Long, ErrorMessage As String, Optional ErrorNumberExpected As Long, Optional Message As String) + + If (ErrorNumberExpected = 0 And ErrorNumber = 0) Then + AddFailure(Message & " - Expected Error did not occur.") + Else If (ErrorNumberExpected <> 0 And ErrorNumber <> ErrorNumberExpected) Then + AddFailure(Message & + " - Expected Error (" & ErrorNumberExpected & ") but Error (" & + ErrorNumber & ") was thrown instead. Description: " & + ErrorMessage) End If Error.Clear @@ -246,9 +251,9 @@ Private Function WasSuccessful_Read() As Boolean End -Private Function RunTests_Read() As Integer +Private Function CountRunnedTests_Read() As Integer - Return $iRunTests + Return $iCountRunnedTests End diff --git a/.src/TestSuite/TestSuite.class b/.src/TestSuite/TestSuite.class index f14bcbe53..2240ef0ac 100644 --- a/.src/TestSuite/TestSuite.class +++ b/.src/TestSuite/TestSuite.class @@ -37,7 +37,7 @@ Public Sub Run(oTestResult As TestResult, Optional ShowDebug As Boolean) CurrentContainer.SetupContainer Endif - Try oTest.Run(oTestResult) + oTest.Run(oTestResult) If Error Then oTestResult.AddError(Error.Code, Error.Where, Error.Text) Try oTest.TestContainer.TearDownEach() diff --git a/.startup b/.startup index bf9e61747..8d7ba14da 100644 --- a/.startup +++ b/.startup @@ -1,8 +1,8 @@ -FmRunner +MRunner gb.deg.unit 0 0 -0.0.4 +0.0.5 gb.image gb.qt4