1

I've been going through a VB6 application called SimplyVBUnit. Now the great thing is that the person who wrote this has released its source and I have been attempting to work out how they actually accomplished the unit testing functionality for VB6 but so far I have struggled to understand how the code works.

Basically what I am hoping somebody can explain to me is how one create a unit testing framework for VB6 given (to the best of my knowledge) it doesn't implement anything similar to the "Compiler Services" or reflection provided by .NET?

Without these features I can't get my head around how one could invoke a method/object/whatever dynamically at runtime and observe the result. Any chance someone could provide some input?

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • You say that the source has been released - so why don't you look through it to find out? Note that reflection is likely to be much more important to a unit testing framework than dynamic compilation. – Jon Skeet Jun 21 '11 at 06:09
  • Please reread question. I have tried reading the source but as you can imagine it is rather complicated and I have been unable to understand how it works. Obviously if I don't get a reasonable answer here I will continue to try and work it out myself but thus far, no luck! – Maxim Gershkovich Jun 21 '11 at 06:12
  • @Maxim: I think it would be a more interesting question if you made it broader, along the lines of "How do you start getting into a code base which confuses you?" Explain what you've already tried, how far you've got, etc. Have you built the code and got it working in a debugger? Have you hit a break point while executing a unit test, and looked at the stack trace? Which *bit* of the code is confusing you? – Jon Skeet Jun 21 '11 at 06:18
  • Browsing through the code, it looks like InterfaceInfoFromObject is the most important starting point for reflection... see MemberQuery.cls. – Jon Skeet Jun 21 '11 at 06:22
  • @Maxim: Adding to what Jon has written, the code uses `TLBINF32.dll` to extract `InterfaceInfoFromObject`, which returns `InterfaceInfo` that has property named `Name` which returns the name of the method implemented by the class. I guess, this is similar to what reflection does (in terms of enumerating methods to check whether setup/teardown/fixturesetup/fixtureteardown methods are implemented). – shahkalpesh Jun 21 '11 at 06:35
  • @shahkalpesh Correct, TLBINF32 is as close as you can get to reflection in VB6. As discussed in [this question](http://stackoverflow.com/questions/2656471/how-can-i-list-the-properties-of-an-object-programmatically-in-vb6/2657124#2657124) – MarkJ Jun 21 '11 at 08:09

1 Answers1

1

I've been using SimplyVBUnit in an old VB6 app. It's great. I have over 100 tests. But I had to heavily change it to integrate it with my app (e.g. so that I can go to the Help menu and run Unit Tests. Specifically, I separated the UI from the actual implementation, so it's UI agnostic.

The source is actually not that complicated (once you grok what the guy is doing). VB6 does not have any reflection and TLBINF32.DLL only works on external DLLs, thus if you wanted to integrate SimplyVBUnit into your app, you can't take that approach.

You create a Unit Test class in an ActiveX DLL that inherits from ITestCase. You implement a method called RunTest. Inside the method, you have the following:

Private Sub ITestCase_RunTest(ByVal TestNum As Long, TestName As String, ExpectErrNum As Long, EndOfTests As Boolean)
    Dim objTestNum As New TestNumGenerator

    Select Case TestNum
        Case objTestNum.NextNumber
            TestName = "Instantiate an APDebit Object"
            Test_InstantiateAPDebitObject
        Case objTestNum.NextNumber
            TestName = "Create an APDebit record."
            Test_CreateAPDebit
        Case objTestNum.NextNumber
            TestName = "Load an APDebit record."
            Test_LoadAPDebit
        Case Else
            EndOfTests = True

    End Select

End Sub

You then pass your class into the UnitTest framework and it calls the RunTest method on it.

I can share the code if you wish to disconnect the UI from the implementation.

AngryHacker
  • 59,598
  • 102
  • 325
  • 594