哪个更好?每个解决方案或每个项目的单元测试项目?
每个解决方案都有一个单元测试项目还是每个项目都有一个单元测试项目更好?
对于每个解决方案,如果解决方案中有 5 个项目,您最终会得到 1 个单元测试项目,其中包含 5 个项目中每一个项目的测试。
对于每个项目,如果解决方案中有 5 个项目,您最终会得到 5 个单元测试项目。
什么是正确的方法?
我认为这与 将单元测试写入程序集中还是单独的程序集中?
Is it better to have a unit-test project per solution or a unit-test project per project?
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.
With per project, if you have 5 projects in the solution you end-up with 5 unit-test projects.
What is the right way?
I think it's not the same question as Write Unit tests into an assembly or in a separate assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
程序集是一个打包/部署问题,因此我们通常将它们分开,因为我们不想将它们与我们的产品一起部署。无论您将它们按库还是按解决方案分开,两者都有优点。
最终,您希望所有开发人员立即可以使用测试,以便开发人员知道在需要时在哪里可以找到它们。您还需要一个无障碍的环境,以最小的开销来编写新测试,这样您就不会武装那些不想编写测试的愤世嫉俗者。测试还必须快速编译和执行 - 项目结构可以在所有这一切中发挥作用。
您可能还需要考虑不同级别的测试是可能的,例如单元测试、集成测试或 UI 自动化测试。在某些工具中可以通过使用测试类别来隔离这些类型的测试,但有时如果它们是单独的库,则执行或报告会更容易。
如果您有特殊的打包注意事项,例如模块化应用程序,其中模块不应该相互了解,那么您的测试项目也应该反映这一点。
在项目不多的小型项目中,1:1 的比例通常是首选方法。然而,随着项目数量的增加,Visual Studio 的性能会迅速下降。大约 40 个项目标记编译成为编译和运行测试的障碍,因此较大的项目可能会从合并测试项目中受益。
我倾向于采用务实的方法,以便解决问题的复杂性。通常,一个应用程序将由多个层组成,其中每个层可能有多个项目。我喜欢从每层一个测试库开始,并使用文件夹模仿解决方案结构。当复杂性需要时进行划分。如果您设计的测试项目具有灵活性,那么转换通常是轻松的。
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.
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.
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.
我想说每个单元测试项目都有一个单独的项目,而不是每个解决方案一个项目。我认为这更好,因为如果您决定从解决方案中取出特定项目并将其移至另一个解决方案,它会为您省去很多麻烦。
I would say a separate project for each unit test project rather than in one project per solution. I think this is better because it will save you a lot of hassle if you decide to take a particular project out of a solution and move it to another solution.
在一个非常大的系统中,我们从文件到解决方案项目的比例是一比一的,我们已经达到了每次签入时构建都需要超过 90 分钟的地步。我创建了新的解决方案配置,它仅构建测试项目,新配置每天运行一次,以确保所有测试用例都正常工作,开发人员可以切换到单元测试配置以在开发环境中测试他们的代码。 PL。让我知道您的反馈
We have one to one ratio from file to projects of solution in a very large system, we have reached a point where build takes more than 90 mins every time we check-in. I have created new solution configuration which builds only test projects, new configuration runs once a days to make sure all test cases are working ,developers can switch to unittest config to test their code in their development environment. Pl. let me know your feed back
我将采用“视情况而定”的答案之一。就我个人而言,我倾向于将所有测试放在一个项目中,每个程序集在项目中都有单独的文件夹(根据需要还可以添加更多子文件夹)。这样可以轻松地从 VisualStudio 或 CruiseControl 中运行整个测试集。网。
如果您有数千个测试,单个项目可能会难以维护。此外,正如 Peter Kelly 在他的回复中提到的,如果您移动项目,能够轻松地拆分测试可能会很有用。
I am going with one of those "it depends" answers. Personally, I tend to put all of the tests in a single project, with separate folders within the project for each assembly (plus further sub-folders as necessary.) This makes it easy to run the entire set, either from within VisualStudio or CruiseControl.net.
If you have thousands of tests, a single project might prove too difficult to maintain. Also, as Peter Kelly mentioned in his response, being able to easily split the tests if you move a project can be useful.
我个人为每个项目编写一个测试程序集。然后,我为每个解决方案创建一个 nunit 项目,并从中引用所有相关的测试程序集。这反映了项目的组织,并且意味着如果在不同的解决方案中重用项目,则只需要运行相关的单元测试。
Personally I write one test assembly per project. I then create one nunit project per solution and reference all of the relevant test assemblies from that. This reflects the organisation of the projects and means that if projects are reused in different solutions then only the relevant unit tests need to be run.