如何访问另一个程序集中的类以进行单元测试? [英] How to access classes in another assembly for unit-testing purposes?

查看:24
本文介绍了如何访问另一个程序集中的类以进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Basically, I have two projects in one solution:

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

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

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 class level, but there are a few delegates involved.

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

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.

除了直接回答之外,我们将不胜感激提供解决方案示例.

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

推荐答案

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

You can use assembly-level attribute InternalsVisibleToAttribute to achieve this.

添加

[assembly:InternalsVisibleTo("MyProjectTests")]

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

to AssemblyInfo.cs in your MyProject assembly.

这篇关于如何访问另一个程序集中的类以进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆