简体   繁体   English

单元测试代码的最佳位置

[英]Best location for unit test code

I have a multi-project solution which I wish to apply unit testing to. 我有一个多项目解决方案,我希望将单元测试应用到。 The project is well encapsulated and most of the unit tests will use classes only from within the project they are testing. 该项目封装良好,大多数单元测试仅使用他们正在测试的项目中的类。

I also have a number of integration test cases which test the interaction of multiple projects. 我还有一些集成测试用例,用于测试多个项目的交互。

Obviously I can just throw all the test code together in a separate project but I am cautious about the idea of creating a Great Project that has dependencies through the entire solution. 显然,我可以将所有测试代码放在一个单独的项目中,但我对创建一个在整个解决方案中具有依赖关系的Great Project的想法持谨慎态度。

Is it better to create a private class for unit tests in each project and have a separate project for integration testing? 为每个项目中的单元测试创​​建一个私有类是否更好,并且有一个单独的集成测试项目? Or is there no appreciable architectural benefit? 或者没有明显的建筑效益?

You should be putting the unit tests into a separate assembly. 您应该将单元测试放入单独的程序集中。 By doing this you can ensure that they are not part of any released product. 通过这样做,您可以确保它们不属于任何已发布的产品。

If you adopt a convention where the test projects have the word Test in their name then it is a simple matter to do a wild card deletion on your final build output to ensure that you're not shipping test assemblies. 如果您采用的惯例是测试项目的名称中包含单词Test ,那么在最终构建输出中删除外卡是一件简单的事情,以确保您不会发送测试程序集。

I am cautious about the idea of creating a Great Project that has dependencies through the entire solution 我对创建一个通过整个解决方案具有依赖性的Great Project的想法持谨慎态度

As your solution grows in size you can split your tests up in to assemblies that target specific areas of your application - you can start with a monolithic test assembly then split it up when it makes sense to. 随着您的解决方案规模的扩大,您可以将测试分解为针对应用程序特定区域的程序集 - 您可以从单片测试程序集开始,然后在有意义的时候将其拆分。

Having experience with working with a solution that has about 110 projects including unit test projects, I may have have something to add 拥有使用包含大约110个项目(包括单元测试项目)的解决方案的经验,我可能需要添加一些内容

  • Having many little projects will increase your build time versus having larger projects. 拥有许多小项目会增加您的构建时间,而不是拥有更大的项目。 Depending on the size and complexity of your compilation, this might be something you're worried about. 根据编辑的大小和复杂程度,这可能是您担心的问题。

One implementation I worked with used a project structure something like this: 我使用过的一个实现使用了类似这样的项目结构:

- CompanyName.Feature
- CompanyName.Fetaure.Test.Unit
- CompanyName.Feature.Test.Integration
- CompanyName.Feature.Test.Load
- etc...

This level of granularity is an improvement compared to a monolithic test project; 与整体测试项目相比,这种粒度级别是一种改进; however, I feel like there is a better way 但是,我觉得有更好的方法

Instead of breaking out all the test, instead, make sure that you test on a feature by feature basis. 而不是打破所有测试, 而是确保您按功能测试功能。 At that point I would be comfortable actually having my unit and integration tests in the same project mapped out to different folders. 那时我会很自在地将我的单元和集成测试放在同一个项目中,映射到不同的文件夹。 Ideally something like this: 理想情况下是这样的:

 - CompanyName.Feature
 - CompanyName.Feature.Test
      \Unit
      \Integration
      \Load
      \etc...
  1. Assuming your codebase isn't currently suffering from some issues with circular dependencies, this should also minimize the references that span across the code base. 假设您的代码库当前没有遇到循环依赖的某些问题,这也应该最小化跨代码库的引用。
  2. This also has the benefit where if you write any code that assists in testing (ie: generation of test data) , you can share it between your unit and integration tests. 如果您编写任何有助于测试的代码(即:生成测试数据) ,您也可以在单元和集成测试之间共享它。

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

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