简体   繁体   English

如何在.Net中设置单元测试项目?

[英]How do you Setup your Unit Test Project(s) in .Net?

Can you share the way you setup your unit test projects within your .net solutions? 您能否分享在.net解决方案中设置单元测试项目的方式?

I can imagine several possible methodologies. 我可以想象几种可能的方法。 For example: 例如:

  1. Having a separate solution for unit tests, perfectly mirroring the structure of the original code solution being tested. 为单元测试提供单独的解决方案,完美地镜像正在测试的原始代码解决方案的结构。

  2. Inside the original code solution, have a solution folder in which you're perfectly mirroring... 在原始代码解决方案中,有一个解决方案文件夹,您可以在其中完美地镜像...

  3. Have a unit test project for each code project and residing alongside it inside the solution's tree structure. 为每个代码项目提供一个单元测试项目,并与解决方案的树结构一起驻留在其中。

  4. Have a unit test project covering several code projects residing alongside them in the tree structure. 有一个单元测试项目,涵盖在树形结构中与它们一起存在的几个代码项目。

Would love to hear how you're actually doing it in your solutions. 很想知道你在解决方案中是如何实际做到的。

Definitely a single solution, but separate projects: 绝对是单一的解决方案,但是单独的项目:

  • You don't want your tests to be part of your production assembly (why add the bloat?) 您不希望您的测试成为生产装配的一部分(为什么要添加膨胀?)
  • You don't want to have to switch between different solutions in order to switch between test code and production code; 您不希望必须在不同的解决方案之间切换,以便在测试代码和生产代码之间切换; this can seriously break the rhythm in my experience. 这可以严重打破我的经验节奏。 (I've been forced into this pattern once, and it was horrible.) It also breaks refactoring - if you change a method in your production code, you want tests to automatically use the new method name. (我曾经被迫进入这种模式,这很糟糕。)它也会破坏重构 - 如果你在生产代码中更改了一个方法,你希望测试自动使用新的方法名称。 (Admittedly the names of some tests may need to be changed manually.) (不可否认,某些测试的名称可能需要手动更改。)

One test project per production project works well for me. 每个生产项目的一个测试项目对我来说很有效。 I tend to make the namespace the same for both tests and production code, which means you typically don't need as many using directives. 我倾向于使命名空间对于测试和生产代码都相同,这意味着您通常不需要使用尽可能多的using指令。

IMO, if you want to make your tests easy to run, the test project(s) absolutely must go in the same solution as the production code. IMO,如果您想让测试易于运行,测试项目绝对必须与生产代码在同一解决方案中。 While putting tests in another solution may work if all developers are extremely diligent, it adds an extra barrier between changing production code and running the tests. 如果所有开发人员都非常勤奋,那么将测试放在另一个解决方案中可能会有效,但它会在更改生产代码和运行测试之间增加额外的障碍。 I tend to prefer removing as many barriers as possible. 我倾向于选择尽可能多的障碍。

There are a few different ways you can do it. 有几种不同的方法可以做到这一点。

  • A single test project per single solution (Product.UnitTests.csproj) 每个解决方案的单个测试项目(Product.UnitTests.csproj)
  • A single test project per system (Product.SystemName.UnitTests.csproj) 每个系统一个测试项目(Product.SystemName.UnitTests.csproj)
  • A one to one mapping of production projects and test projects (Product.ProjectName.csproj and Product.ProjectName.Tests.csproj) 生产项目和测试项目的一对一映射(Product.ProjectName.csproj和Product.ProjectName.Tests.csproj)

Each has its own trade-offs you have to weigh up. 每个人都有自己的权衡,你必须权衡。

Single test project per single solution 每个解决方案的单个测试项目

Makes sense if the entire solution contains cohesive projects. 如果整个解决方案包含有凝聚力的项目,则有意义。 If you are always going to be developing using the same solution, then it's nice to have a centralised test project. 如果您总是要使用相同的解决方案进行开发,那么拥有一个集中的测试项目会很不错。

It means there's a reduced overhead on your build process (when you have solutions with scores of projects, reducing the assembly count also reduces the build time) and there's no overhead of maintaining .nunit files or the like. 这意味着您的构建过程的开销会减少(当您拥有大量项目的解决方案时,减少组装计数也会缩短构建时间)并且没有维护.nunit文件等的开销。

Also, everyone knows where the tests go. 此外,每个人都知道测试的去向。 The downside is that you have to divide up different production projects by using namespaces, and the tests for one project are now tied into the others. 缺点是您必须使用命名空间来划分不同的生产项目,现在一个项目的测试与其他项目相关联。 Ie this is the 'easiest' one to get buy-in for as there's less to keep track of and developers can easily run the tests. 也就是说,这是最容易获得支持的,因为没有必要跟踪,开发人员可以轻松地运行测试。 The downside is that in some cases this isn't a good fit for what you're developing. 缺点是在某些情况下,这不适合你正在开发的东西。

Single test project per system 每个系统单个测试项目

Basically the same as the above, except it's finer grained. 基本上和上面一样,除了它的粒度更细。 You group related projects into areas / systems and use a single test assembly for each. 您将相关项目分组到区域/系统中,并为每个项目使用单个测试程序集。 This increases the complexity a little, but also means the systems are more easily extractable/re-usable in other solutions. 这稍微增加了复杂性,但也意味着系统在其他解决方案中更容易提取/可重复使用。

A one to one mapping of production projects and test projects 生产项目和测试项目的一对一映射

Has the most overhead in terms of creating new assemblies, increases the build times a bit when there's loads of projects and generally makes your solution file bigger. 在创建新程序集方面开销最大,在项目负载时会略微增加构建时间,并且通常会使解决方案文件更大。 Requires diligence in terms of always keeping .nunit project files up to date and doesn't play so nicely with in-IDE unit testing. 需要尽职尽责地保持.nunit项目文件是最新的,并且在IDE单元测试中不能很好地发挥作用。

The up-side is that maintaining a test project for each production project means that the tests are only dependent on the functionality they're testing (so it's easier to re-use projects) and you can more easily check code coverage by running one project at a time (whereas if you run all of your tests, you will get higher coverage due to unrelated tests sometimes touching code they're not interested in). 最重要的是,为每个生产项目维护一个测试项目意味着测试只依赖于他们正在测试的功能(因此更容易重用项目),并且您可以通过运行一个项目更轻松地检查代码覆盖率一次(如果您运行所有测试,由于不相关的测试有时会触及他们不感兴趣的代码,您将获得更高的覆盖率)。 Furthermore, it's a simple pattern to follow, so people will understand where the tests are meant to go without a problem. 此外,这是一个简单的模式,因此人们将了解测试的目的在哪里没有问题。

In summary: I think any of the above can work well depending on what you're developing, but if you want a solution that 'just works', then I'd be inclined to go for the one to one mapping. 总结:我认为上述任何一种都可以很好地运行,具体取决于你正在开发什么,但如果你想要一个“正常工作”的解决方案,那么我倾向于选择一对一的映射。

The following structure has worked well for me. 以下结构对我来说效果很好。 It also keeps the production and test code separate. 它还使生产和测试代码分开。

\source
\source\code\MainSolution.sln
\source\code\Project1
\source\code\...
\source\test\TestSolution.sln
\source\test\TestProject1
\source\test\...

Have the same solution for both, but 2 projects, the .NET project and the NUnit project (One to One), now if I have a solution that have multiple projects, then I have just 1 NUnit project for the solution as it and 1 for each project.. I think is a good way because it serves me to mantain organized my projects. 对于两个项目,包括两个项目,.NET项目和NUnit项目(一对一)都有相同的解决方案,现在如果我有一个有多个项目的解决方案,那么我只有1个NUnit项目用于解决方案,因为它和1对于每个项目..我认为这是一个很好的方式,因为它有助于我组织我的项目。

Also, you can take a look at this book: http://www.artofunittesting.com/ it's very good. 另外,你可以看一下这本书: http//www.artofunittesting.com/非常好。

我更喜欢在与源代码相同的解决方案中使用测试项目,并通过添加文件夹对测试进行分组

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

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