哪个更好?每个解决方案或每个项目的单元测试项目? [英] Which is better? Unit-test project per solution or per project?

查看:55
本文介绍了哪个更好?每个解决方案或每个项目的单元测试项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个解决方案有一个单元测试项目还是每个项目有一个单元测试项目更好?

Is it better to have a unit-test project per solution or a unit-test project per project?

对于每个解决方案,如果您在解决方案中有 5 个项目,那么您最终会得到 1 个单元测试项目,其中包含 5 个项目中每一个的测试.

With per solution, if you have 5 projects in the solution you end-up with 1 unit-test project containing tests for each of the 5 projects.

对于每个项目,如果您的解决方案中有 5 个项目,那么您最终会得到 5 个单元测试项目.

With per project, if you have 5 projects in the solution you end-up with 5 unit-test projects.

什么是正确的方式?

我认为这与 将单元测试写入程序集中还是单独的程序集中?

推荐答案

程序集是一个打包/部署问题,因此我们通常将它们分开,因为我们不想将它们与我们的产品一起部署.无论您是按库还是按解决方案将它们分开,两者都有优点.

Assemblies are a packaging/deployment concern, so we usually split them out because we don't want to deploy them with our product. Whether you split them out per library or per solution there are merits to both.

最终,您希望所有开发人员都可以立即使用测试,以便开发人员在需要时知道在哪里可以找到它们.您还需要一个无障碍环境,以最小的开销来编写新测试,这样您就不会武装不想编写测试的愤世嫉俗者.测试还必须快速编译和执行 - 项目结构可以在所有这些中发挥作用.

Ultimately, you want tests to be immediately available to all developers, so that developers know where to find them when needed. You also want an obstacle free environment with minimal overhead to writing new tests so that you aren't arming the cynics who don't want to write tests. Tests must also compile and execute quickly - project structure can play a part in all of this.

您可能还需要考虑不同级别的测试是可能的,例如针对单元、集成或 UI 自动化的测试.在某些工具中可以通过使用测试类别来隔离这些类型的测试,但有时如果它们是单独的库,则执行或报告会更容易.

You may also want to consider that different levels of testing are possible, such as tests for unit, integration or UI automation. Segregating these types of tests is possible in some tools by using test categories, but sometimes it's easier for execution or reporting if they are separate libraries. 

如果您有特殊的打包注意事项,例如模块不应相互了解的模块化应用程序,则您的测试项目也应反映这一点.

If you have special packaging considerations such as a modular application where modules should not be aware of one another, your test projects should also reflect this.

在项目不多的小型项目中,1:1 的比例通常是首选方法.但是,随着项目数量的增加,Visual Studio 的性能会迅速下降.大约 40 个项目标记编译成为编译和运行测试的障碍,因此更大的项目可能会从合并测试项目中受益.

In small projects where there aren't a lot of projects, a 1:1 ratio is usually the preferred approach. However, Visual Studio performance quickly degrades as the number of projects increases. Around the 40 project mark compilation becomes an obstacle to compiling and running the tests, so larger projects may benefit from consolidating test projects. 

我倾向于采用务实的方法,以便复杂性适合问题.通常,一个应用程序将由若干层组成,其中每一层可能有多个项目.我喜欢从每层一个单独的测试库开始,并使用文件夹模拟解决方案结构.在复杂性需要时进行划分.如果您设计测试项目的目的是为了灵活性,那么转换通常是轻松的.

I tend to prefer a pragmatic approach so that complexity is appropriate to the problem.  Typically, an application will be comprised of several layers where each layer may have multiple projects.  I like to start with a single test library per layer and I mimic the solution structure using folders. Divide when complexity warrants it. If you design your test projects for flexibility then changeover is usually painless.

这篇关于哪个更好?每个解决方案或每个项目的单元测试项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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