C# 如何在 Visual Studio 中调试类库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2027698/
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 22:49:29  来源:igfitidea点击:

How to debug a class library in Visual Studio

c#visual-studiodebuggingdllclass-library

提问by PICyourBrain

I am working on a class library (DLL) project in Visual Studio 2008; programming in C#. In order to test my DLL I just created a second project that is a console application and in that project I can reference the first and run tests. Is there a simpler way of doing this? Can I just create another file within my class library project that has the tests in it and then somehow tell Visual Studio to run that file?

我正在 Visual Studio 2008 中处理类库 (DLL) 项目;用 C# 编程。为了测试我的 DLL,我刚刚创建了第二个项目,它是一个控制台应用程序,在该项目中我可以引用第一个并运行测试。有没有更简单的方法来做到这一点?我可以在我的类库项目中创建另一个包含测试的文件,然后以某种方式告诉 Visual Studio 运行该文件吗?

I know one way would be to add a text file to my project and then write my test code in JScript. Then in the Project settings on the debug menu I can tell it to Start External Program (JScript). Then, the name of my test file, test.js, goes in the Command Line Arguments box. But, I am wondering if there is a way to do it using C# code instead of JScript?

我知道一种方法是向我的项目添加一个文本文件,然后用 JScript 编写我的测试代码。然后在调试菜单上的项目设置中,我可以告诉它启动外部程序(JScript)。然后,我的测试文件的名称test.js,出现在命令行参数框中。但是,我想知道是否有办法使用 C# 代码而不是 JScript 来做到这一点?

采纳答案by mdm

You could add a testing project to your current solution, then set that project as the startup project. Then, hitting F5 on your class library project will start your testing project.

您可以向当前的解决方案添加一个测试项目,然后将该项目设置为启动项目。然后,在您的类库项目上按 F5 将启动您的测试项目。

回答by Joe

Take a look at NUnit or other similar unit testing framework.

看看 NUnit 或其他类似的单元测试框架。

The "Team Developer" and "Team Suite" flavors of Visual Studio already have Microsoft's unit testing framework built in.

Visual Studio 的“团队开发人员”和“团队套件”风格已经内置了 Microsoft 的单元测试框架。

回答by Wix

Are you talking about unit tests? You can use something like nUnit or the built in testing framework that comes with Visual Studio. The simplest tests just require you to add some attributes to your test fixture and make an assertion like obj1 == obj2.

你说的是单元测试吗?您可以使用类似 nUnit 或 Visual Studio 附带的内置测试框架之类的东西。最简单的测试只需要您向测试装置添加一些属性并做出像 obj1 == obj2 这样的断言。

Checking out something like Test-Driven Development (TDD), Domain-Driven Development (DDD) or Behavioral-Driven Development (BDD) may be beneficial. I like to use nUnit with nBehave, myself.

检查诸如测试驱动开发 (TDD)、领域驱动开发 (DDD) 或行为驱动开发 (BDD) 之类的东西可能是有益的。我自己喜欢将 nUnit 与 nBehave 一起使用。

回答by tvanfosson

Create a unit test project for the class library by using the right-click "Create Unit Tests" in a class/method in the library. I would recommend downloading TestDriven.NETand using the right-click test runner in it.

通过在库中的类/方法中使用右键单击“创建单元测试”,为类库创建单元测试项目。我建议下载TestDriven.NET并在其中使用右键单击测试运行器。