0

I am getting started with Unit testing in order to better manage this Legacy code in VB6 that I’m working on. I started with the C# unit test on a small demo application to get the hang of it and then started to play with SimplyVBUnit (which I’m still no proficient with).

What I immediately noticed is that if a create unit tests in VB6 and then migrate my code I will have to recreate\migrate my tests code on the .NET side. I tried resolve this by searching a way to do this via COM Interop to see if I could show my VB6 methods in a Unit Test Project in .NET but couldn’t find a good source of information.

What I want to ask then is if this approach is even viable? And if so, what are the steps to accomplish this?

UPDATE I: Partial success

After searching around I found that I could create an ActiveX dll in VB6 to pass a class. In order to test this I created a new VB6 project with the ActiveX Dll template called “MyTestProject” and in it created a class called CalculatorLib with the following code:

Public Function Addition(ByVal operand1 As Long, ByVal operand2 As Long) As Long
    Addition = (operand1 + operand2)
End Function

Public Function Subtract(ByVal operand1 As Long, ByVal operand2 As Long) As Long
    Subtract = (operand1 - operand2)
End Function

Public Function Divide(ByVal operand1 As Long, ByVal operand2 As Long) As Long
    Divide = (operand1 + operand2)
End Function

Then create the dll by pressing make under File -> Make MyTestProject.dll

In Visual studio 2015 create a new Unit Test project. There is a naming convention for this but for this test I did not take that into account.

Select your project in the Solution explorer and right click and select Add and then reference. In the COM tab search for your dll and add it to you project. In the test I created a (quick and dirty) test method called TestAdd:

    [TestMethod]
    public void TestAdd()
    {
        MyTestProject.CalculatorLib calculator = new MyTestProject.CalculatorLib();
        int io = calculator.Addition(10, 30);
        Assert.AreEqual(io, 20);
    }

This test fails, as expected, and changin the 30 to a 10 makes the test pass. JOY!

So now that I am able to do this how do I test the function and sub’s that are in my modules and forms without having to convert them in classes in a ActiveX dll?

BMFS
  • 19
  • 4

1 Answers1

0

There are really only two options:

  1. Re-write your unit tests in C#.
  2. Expose your VB6 unit tests as ActiveX DLL functions.

In either case you are going to need to write some form of C# code. If the unit tests in VB6 are still being used then I recommend that you use the second option so you won't have to worry about maintaining two sets of source code.

Where I work we have all of the unit tests in C# because we want to distance ourselves from the legacy product as much as possible.

vbguyny
  • 1,170
  • 1
  • 10
  • 29
  • Thanks for the reply @vbguyny. I do think that having the tests in C# is the way to go. We currently have no Unit tests implemented. This is something that I’m trying to introduce in the project. I am reading both “The Art of Unit Testing: With Examples in C#” and “Working Effectively with Legacy Code” since I’m fairly new to these subjects. So, any examples or source of information that you could provide would be greatly appreciated. – BMFS Dec 01 '16 at 23:57