C# 如何访问另一个程序集中的类以进行单元测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1211707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 11:01:52  来源:igfitidea点击:

How to access classes in another assembly for unit-testing purposes?

c#unit-testing.net-assembly

提问by Kevin Montrose

I'm jumping into unit-testing the Visual-Studio 2008 way, and I'm wondering what's the best way to accomplish cross-assembly classaccess for testing purposes.

我正在使用 Visual-Studio 2008 方式进行单元测试,我想知道class为了测试目的完成跨程序集访问的最佳方式是什么。

Basically, I have two projects in one solution:

基本上,我在一个解决方案中有两个项目:

  1. MyProject (C#)
  2. MyProjectTests (C# Test Project)
  1. 我的项目 (C#)
  2. MyProjectTests(C# 测试项目)

Everything in MyProject currently has default accessibility, which if I recall correctly means everything is effectively internal. I'm mostly looking to test at the classlevel, but there are a few delegatesinvolved.

MyProject 中的所有内容目前都有默认的可访问性,如果我没记错的话,这意味着一切都有效internal。我主要希望在class级别上进行测试,但也有一些delegates涉及。

There will probably be an external API sometime in the future, but I'm about 20% of the way to feature complete (at least on paper) and I'm getting pretty leery of layering more code on top of this untested core. Accordingly I'd like to get some testing done now, before the app is complete enough for traditional (read: bad and/or lazy) functional testing and definitely before the version n+1 external API is up.

将来某个时候可能会有一个外部 API,但我已经完成了大约 20% 的功能(至少在纸面上),而且我对在这个未经测试的核心之上分层更多代码感到非常谨慎。因此,我想现在就完成一些测试,在应用程序足够完整以进行传统(阅读:坏和/或懒惰)功能测试之前,并且绝对是在版本 n+1 外部 API 启动之前。

In addition to a straight answer, an example of the solution would be greatly appreciated.

除了直接的答案之外,将不胜感激解决方案的示例。

采纳答案by Arnold Zokas

You can use assembly-level attribute InternalsVisibleToAttributeto achieve this.

您可以使用程序集级属性InternalsVisibleToAttribute来实现这一点。

Add

添加

[assembly:InternalsVisibleTo("MyProjectTests")]

to AssemblyInfo.cs in your MyProject assembly.

到您的 MyProject 程序集中的 AssemblyInfo.cs。

回答by AutomatedTester

You need to add

你需要添加

[assembly:InternalsVisibleTo("Unit.Tests.Assembly")] 

to AssemblyInfo.cs of your "MyProject (C#)". That then allows your tests to access the internal methods for testing.

到“MyProject (C#)”的 AssemblyInfo.cs。然后允许您的测试访问内部测试方法。

回答by Gishu

Looks like you need the InternalsVisibleToAttribute

看起来你需要InternalsVisibleToAttribute

However I'd recommend against this approach - test your internal classes via the public interface or API.

但是我建议不要使用这种方法 - 通过公共接口或 API 测试您的内部类。

回答by AdaTheDev

You can test internal methods, by adding an attribute to the AssemblyInfo.cs for your main project, giving access to the internal methods to a named assembly:

您可以通过向主项目的 AssemblyInfo.cs 添加一个属性来测试内部方法,从而为命名程序集提供对内部方法的访问权限:

[assembly:InternalsVisibleTo("MyProjectTestsNameSpace.MyProjectTests")]

[程序集:InternalsVisibleTo("MyProjectTestsNameSpace.MyProjectTests")]

Further info is here

更多信息在这里

回答by StuartLC

Although [InternalsVisibleTo]is most sensible way IMO, there are at least 2 other ways to go about this:

尽管[InternalsVisibleTo]IMO 是最明智的方式,但至少还有两种其他方法可以解决此问题:

  • By using Reflection

     var method = instance.GetType().GetMethod(
        methodName, BindingFlags.NonPublic | BindingFlags.Instance, 
        null, paramTypeArray, null);
     return method.Invoke(instance, parameters);
    
  • 通过使用反射

     var method = instance.GetType().GetMethod(
        methodName, BindingFlags.NonPublic | BindingFlags.Instance, 
        null, paramTypeArray, null);
     return method.Invoke(instance, parameters);
    

The problem with this approach is that if the method name or signature changes, the unit test will start failing at Run Time, whereas [InternalsVisibleTo]would have easily been picked up this breaking change at compile time.

这种方法的问题在于,如果方法名称或签名发生变化,单元测试将在运行时开始失败,而[InternalsVisibleTo]在编译时很容易发现这种破坏性变化。

回答by Mo D Genesis

I found this one https://msdn.microsoft.com/en-us/library/hh598957.aspxHope it might help someone.

我找到了这个https://msdn.microsoft.com/en-us/library/hh598957.aspx希望它可以帮助某人。

Summary:

概括:

  • In your unit test project, add a reference to the code under test. Here's how to create the reference to a code project in the same solution:
  • Select the project in Solution Explorer.
  • On the Project menu, choose Add Reference....
  • In the Reference Manager dialog box, open the Solution node and choose Projects.
  • Check the code project name and close the dialog box.
  • 在您的单元测试项目中,添加对被测代码的引用。以下是在同一解决方案中创建对代码项目的引用的方法:
  • 在解决方案资源管理器中选择项目。
  • 在“项目”菜单上,选择“添加引用...”。
  • 在“引用管理器”对话框中,打开“解决方案”节点并选择“项目”。
  • 检查代码项目名称并关闭对话框。