简体   繁体   English

如何在.NET中对VB6代码进行单元测试

[英]How to Unit test VB6 code in .NET

I am getting started with Unit testing in order to better manage this Legacy code in VB6 that I'm working on. 我开始进行单元测试,以便更好地管理我正在研究的VB6中的旧版代码。 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). 我首先在一个小型演示应用程序上进行了C#单元测试,以掌握其要点,然后开始使用SimplyVBUnit(我仍然不熟练)。

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. 我立即注意到,如果创建单元在VB6中进行测试,然后迁移我的代码,则必须在.NET端重新创建/迁移我的测试代码。 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. 我试图通过搜索通过COM Interop进行此操作的方法来解决此问题,以查看是否可以在.NET的单元测试项目中显示我的VB6方法,但是找不到很好的信息源。

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. 搜索后,我发现可以在VB6中创建ActiveX dll来传递类。 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: 为了对此进行测试,我使用名为“ MyTestProject”的ActiveX Dll模板创建了一个新的VB6项目,并在其中创建了一个名为CalculatorLib的类,并带有以下代码:

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 然后通过按File-> Make MyTestProject.dll下的make创建dll。

In Visual studio 2015 create a new Unit Test project. 在Visual Studio 2015中,创建一个新的单元测试项目。 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. 在“ COM”选项卡中搜索您的dll并将其添加到您的项目中。 In the test I created a (quick and dirty) test method called TestAdd: 在测试中,我创建了一个名为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. 如预期的那样,该测试失败,并且将30更改为10使测试通过。 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? 因此,现在我能够执行此操作,如何测试模块和窗体中的功能和子项而不必在ActiveX dll中的类中进行转换?

There are really only two options: 实际上只有两种选择:

  1. Re-write your unit tests in C#. 用C#重新编写单元测试。
  2. Expose your VB6 unit tests as ActiveX DLL functions. 将您的VB6单元测试公开为ActiveX DLL函数。

In either case you are going to need to write some form of C# code. 无论哪种情况,您都需要编写某种形式的C#代码。 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. 如果仍在使用VB6中的单元测试,那么我建议您使用第二个选项,这样就不必担心维护两组源代码。

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. 在我工作的地方,我们使用C#进行所有单元测试,因为我们希望尽可能地与传统产品保持距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM