新的单元测试单元测试

2023-09-03 01:59:20 作者:°浅晴萝木︷

我想知道如何使用Visual Studio 2008(.NET 2.0),以实现在现有的(相当大的)应用程序的单元测试。

I would like to know how to implement unit testing in an existing (quite large) application using visual studio 2008 (.net 2.0).

据我所知,开发单元测试的现有/遗留code是不现实的,但我想有测试code前进。

I understand that developing unit tests for the existing/legacy code is not realistic but I would like to have tests for code moving forward.

我已经发现大量的实例就如何编写测试code,但对如何设置它从头现有项目,以及如何将其集成到开发周期无关。

I have found plenty of examples on how to write tests for code but nothing on how to set it up from scratch on an existing project, and how to integrate it into the development cycle.

推荐答案

简单的方法:

在挑选单元测试框架之一(NUnit的,MbUnit的,的xUnit,VS单元测试框架,...) 添加项目,单元测试解决方案(比如单元测试)。 在开始写测试。

有关更方便地组织,至少创建一个命名空间中的解决方案,每个项目,例如UnitTest.Project1,UnitTests.Project2,......根据项目的规模,添加更多的级别到命名空间。

For easier organization, create at least one namespace for each project in solution, for example UnitTest.Project1, UnitTests.Project2, ... Depending on size of projects, add more levels to namespace.

这样的测试项目是解决方案的一部分。如果你使用一些持续集成工具,然后测试可以自动对每个执行的承诺源$ C ​​$ C库。

This way test project is part of solution. If you use some continuous integration tool, then tests can be automatically executed on each commit to source code repository.

编辑: 关于对MS单元测试框架的问题,创建单元测试的评论,有知道的问题:的创建单元测试向导不起作用。它看起来有不支持的类型的$正常工作p $ pvent单元测试向导项目。我只是想有一个解决方案,它有一个F#和几个C#项目。我加的单元测试项目,并尝试添加测试。测试向导有问题,直到我卸载F#项目。然后一切正常。

Regarding comment on MS Unit Test Framework problem with creating unit tests, there is know problem: "Create unit test" wizard does not work. It looks that there are unsupported types of projects that prevent unit test wizard from working correctly. I just tried with one solution that have one F# and several C# projects. I added unit testing project and tried to add tests. Test wizard had problems until I unloaded F# project. Then everything worked fine.