I'm working on AccUnit (unit test framework) again and would like to implement some kind of code coverage check.
Does anyone have an idea how to check something like this without having to make (big) changes in the source code?
Simplified example:
Unit test:
This test only tests with odd numbers as input. => CalcWithEvenParam is never called.
It would be great if I could show in the test framework that only 50% of the code was run.
Currently, the only option I can think of is to include a counter in the code.
or
But this is not really usable.
I also have the ability to insert code into the procedure from AccUnit and remove it after testing.
However, identifying where to insert a log line will not be so easy.
I'll probably have to cancel my wish, but maybe someone has a good idea.
Does anyone have an idea how to check something like this without having to make (big) changes in the source code?
Simplified example:
Code:
Public Function ProcedureToTest(ByVal X As Long) As Long
If X Mod 2 = 1 Then
ProcedureToTest = CalcWithOddParam(X)
Else
ProcedureToTest = CalcWithEvenParam(X)
End If
End Function
Private Function CalcWithEvenParam(ByVal X As Long) As Long
CalcWithEvenParam = X / 2
End Function
Private Function CalcWithOddParam(ByVal X As Long) As Long
CalcWithOddParam = (X + 1) / 2
End Function
Unit test:
Code:
'AccUnit:Row(1, 1)
'AccUnit:Row(5, 3)
'AccUnit:Row(123, 62)
Public Sub Test_ProcedureToTest(ByVal X As Long, ByVal Expected As Long)
Dim Actual As Long
Actual = ProcedureToTest(X)
Assert.That Actual, Iz.EqualTo(Expected)
End Sub
This test only tests with odd numbers as input. => CalcWithEvenParam is never called.
It would be great if I could show in the test framework that only 50% of the code was run.
Currently, the only option I can think of is to include a counter in the code.
Code:
Public Function ProcedureToTest(ByVal X As Long) As Long
If X Mod 2 = 1 Then
#If UnitTest = 1 Then
TestSuite.CodeCoverage "ProcedureToTest", 1
#End If
ProcedureToTest = CalcWithOddParam(X)
Else
#If UnitTest = 1 Then
TestSuite.CodeCoverage "ProcedureToTest", 2
#End If
ProcedureToTest = CalcWithEvenParam(X)
End If
End Function
Code:
Public Function CodeCoverageLog(ByVal ProcName As String, ByVal Pos As Long) As Object
#If RunUnitTest = 1 Then
TestSuite.CodeCoverage ProcName, Pos
#End If
End Function
Public Function ProcedureToTest(ByVal X As Long) As Long
If X Mod 2 = 1 Then
CodeCoverageLog "ProcedureToTest", 1
ProcedureToTest = CalcWithOddParam(X)
Else
CodeCoverageLog "ProcedureToTest", 2
ProcedureToTest = CalcWithEvenParam(X)
End If
CodeCoverageLog "ProcedureToTest", 3
End Function
I also have the ability to insert code into the procedure from AccUnit and remove it after testing.
However, identifying where to insert a log line will not be so easy.
I'll probably have to cancel my wish, but maybe someone has a good idea.
Last edited: