简体   繁体   English

每个解决方案是单个还是多个单元测试项目?

[英]Single vs. Multiple Unit Test Projects per Solution?

I have typically had a 1:1 mapping between my product assemblies and my unit test assemblies.我的产品程序集和单元测试程序集之间通常有 1:1 的映射。 I generally try to keep the overall number of assemblies low, and a typical solution may look something like...我通常尽量保持较低的程序集总数,典型的解决方案可能看起来像......

  • Client (Contains Views, Controllers, etc)客户端(包含视图、控制器等)
  • Client.Tests客户端测试
  • Common (Contains Data/Service Contracts, Common Utilities etc)通用(包含数据/服务合同、通用实用程序等)
  • Common.Tests通用测试
  • Server (Contains Domain, Services, etc)服务器(包含域、服务等)
  • Server.Tests服务器.测试
  • Server.WebHost服务器.WebHost

Lately at work, people have been mentioning having just a single Unit Test project vs. breaking them down by the assembly that they are testing.最近在工作中,人们一直在提到只有一个单元测试项目,而不是通过他们正在测试的程序集来分解它们。 I know back in the day, this made life easier if you were running NCover etc as part of your build (no longer matters of course).我知道在那天,如果您将 NCover 等作为构建的一部分运行(当然不再重要),这会让生活更轻松。

What is the general rational behind single vs. multiple UnitTest projects?单个与多个 UnitTest 项目背后的一般理由是什么? Other than reducing the number of projects in a solution, is there a concrete reason to go one way or the other?除了减少解决方案中的项目数量之外,是否有具体的理由采取一种或另一种方式? I get the impression this may be one of those "preference" things, but Googling hasn't turned up much.我的印象是这可能是那些“偏好”之一,但谷歌搜索并没有出现太多。

There is no definite answer because it all depends on what you work on as well as personal taste.没有明确的答案,因为这完全取决于您的工作以及个人品味。 However, you definitely want to arrange things in a way so you can work effectively .但是,您肯定希望以某种方式安排事情,以便您可以有效地工作

For me this means, I want to find things quickly, I want to see what tests what, I want to run smaller things to have better control in case I want to profile or do other things on the tests as well.对我来说,这意味着,我想快速找到东西,我想看看什么测试什么,我想运行更小的东西来更好地控制,以防我想在测试中分析或做其他事情。 This is usually good when you're debugging failing tests.当您调试失败的测试时,这通常很好。 I don't want to spend extra time figuring anything out, it should speak for itself how things are mapped and what belongs to what.我不想花额外的时间去弄清楚任何事情,它应该自己说明事物是如何映射的,什么属于什么。

Another very important thing for me is that I want to isolate as much as possible and have clear boundaries.对我来说另一个非常重要的事情是,我想尽可能地隔离并有明确的界限。 You want to provide an easy way to refactor/move out parts of your big project into an independent project.您想提供一种简单的方法来将大项目的一部分重构/移出到一个独立的项目中。

Personally, I always arrange my tests around how my software is structured which means a one-to-one mapping between class and its tests, library and test executable.就我个人而言,我总是围绕我的软件的结构来安排我的测试,这意味着类与其测试、库和测试可执行文件之间的一对一映射。 This gives you a nice test structure which mirrors your software structure, which in turn provides clarity for finding things.这为您提供了一个很好的测试结构,它反映了您的软件结构,从而为查找内容提供了清晰度。 In addition, it provides a natural split in case something is moved out independently.此外,它提供了一个自然的分割,以防某些东西被独立移出。

This is my personal choice after trying various ways to do things.这是我尝试了各种做事方式后的个人选择。

In my opinion, grouping things when there are too many is not necessarily a good thing.在我看来,当东西太多时分组并不一定是件好事。 It can be, but I believe in the context of this discussion it is the wrong argument for a single test project.可以,但我相信在本次讨论的上下文中,这是针对单个测试项目的错误论点。 Too many test projects with many files inside means just one with a lot of test files.太多包含许多文件的测试项目意味着只有一个包含大量测试文件。 I believe the real problem is that the solution you're working on is getting big.我相信真正的问题是你正在研究的解决方案越来越大。 Maybe there are other things you can do to avoid having "one world"?也许您还可以做其他事情来避免“同一个世界”? :) :)

In addition to the other (good) answers, consider that on larger project teams individual team members may create their own solutions to include only the subset of projects they are working on.除了其他(好的)答案之外,请考虑在较大的项目团队中,个别团队成员可能会创建自己的解决方案,以仅包含他们正在处理的项目子集。

Assuming a monolithic solution, with one test project covering everything in that solution, breaks down in that scenario.假设一个单一的解决方案,其中一个测试项目涵盖了该解决方案中的所有内容,在这种情况下会崩溃。

Part of the reasoning I would say is that it forces you to enlist only the assembly you are testing.我要说的部分原因是它迫使您只招募您正在测试的程序集。 So your Unit tests don't accidentally turn into integration tests or worse.所以你的单元测试不会意外地变成集成测试或更糟。 It helps to ensure separation of concerns.它有助于确保关注点分离。

Having one UnitTest Project might slow you down considerably if you test functionality in a lower tier of your architecture.如果您在架构的较低层中测试功能,那么拥有一个 UnitTest 项目可能会大大减慢您的速度。 you would spend more time on waiting for the compiler to finish than on actually writing code.你会花更多的时间等待编译器完成而不是实际编写代码。 imagine one unit test project would depend on all projects.想象一个单元测试项目将依赖于所有项目。 The projects depend on each other.这些项目相互依赖。 a change in the code of a lower tier project could result in recompiling the projects above that project.较低层项目的代码更改可能会导致重新编译该项目上层的项目。 that could happen if you trigger a unit test如果您触发单元测试,可能会发生这种情况

for example if you have business logic depending on a model project.例如,如果您有依赖于模型项目的业务逻辑。 you change your model project.你改变你的模型项目。 the test project depends on the business logic and on the model.测试项目取决于业务逻辑和模型。

unit test -> model单元测试 -> 模型

unit test -> business logic -> model单元测试 -> 业务逻辑 -> 模型

but you only test changes in the model.但您只测试模型中的更改。 now it might happen that both the model and the business logic gets recompiled though you only changed your model.现在,尽管您只更改了模型,但模型和业务逻辑都可能会重新编译。 this happened to me in a more complex project and changing to having one test project per productive project helped.这发生在我的一个更复杂的项目中,并更改为每个生产项目有一个测试项目有所帮助。

I am uncertain as to a particular standard, but in my opinion, splitting them into separate test projects seems to be a better practice.我不确定特定标准,但在我看来,将它们拆分为单独的测试项目似乎是更好的做法。 It seems to me to be much akin to object oriented programming on a solution/project level.在我看来,它与解决方案/项目级别的面向对象编程非常相似。 Suppose one of your projects needs to be reused somewhere and you want to be able to take your tests with you.假设您的一个项目需要在某处重复使用,并且您希望能够随身携带您的测试。 If they are in a separate project from the other tests and are exclusive only to that particular project, you simply transfer that project along as well.如果它们与其他测试位于不同的项目中,并且仅适用于该特定项目,则您也只需将该项目也一起转移即可。 Otherwise you have to go file fishing through the mass test project.否则,您必须通过大规模测试项目进行文件钓鱼。

In addition, separation of projects helps keep things tidy when debugging.此外,项目的分离有助于在调试时保持整洁。 Rather than having to crack open a single massive project and go digging for the test file you need, it is much simpler if you have a matching test project for the functional projects...dramatically narrows down your search.不必打开一个庞大的项目并去挖掘您需要的测试文件,如果您有功能项目的匹配测试项目,那就简单多了……大大缩小了您的搜索范围。

Again, I think this is a preference thing, I've never heard a definite settlement one way or the other, but there is my two cents worth...再说一次,我认为这是一种偏好,我从来没有听说过以某种方式确定的解决方案,但是我的两分钱值...

I might actually go to the extreme of saying that if you consider splitting up your tests in different projects for testing one assembly, then that assembly is too large.. I generally split up my projects into small, reusable components, with small, 1-1 mapped tests.我实际上可能会极端地说,如果您考虑将测试拆分为不同的项目来测试一个程序集,那么该程序集就太大了。 1 映射测试。 This might of course lead to DLL-overflow, and might not always be possible with certain projects (such as a web project, where it's not as logically clean to split up controllers into different assemblies).这当然可能会导致 DLL 溢出,并且对于某些项目(例如 Web 项目,将控制器拆分为不同的程序集在逻辑上不那么干净)可能并不总是可能的。 Just an idea.只是一个想法。

我们正在尝试一个单独的测试解决方案,其中所有测试项目都与项目解决方案在 1-1 的基础上进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM