简体   繁体   English

单元测试 - 隔离测试

[英]Unit testing - testing in isolation

I've got a set up with a number of layers: 我有一个有多个图层的设置:

  • Website 网站
  • Application / Service 申请/服务
  • Domain (contains entities) 域(包含实体)
  • Persistence (contains repositories) 持久性(包含存储库)

I'm testing the persistence layer in isolation OK using data created in memory from a stub object. 我正在使用从存根对象在内存中创建的数据来隔离测试持久层。

Now, Im thinking about testing my Website layer. 现在,我正在考虑测试我的网站层。 I know I should be testing it in isolation which Im thinking means creating a stub for the Application layer object it uses but this stub would need its own set of in memory data, duplicated in the stub in the persistence layer and I dont want to do duplicate this and manage it. 我知道我应该单独测试它,我认为这意味着为它使用的Application层对象创建一个存根但是这个存根需要它自己的内存数据集,在持久层中的stub中重复,我不想这样做复制它并管理它。

So my question is should the subject under test always work with stub objects from the layer below in order to be isolated and do they normally have their own set of data? 所以我的问题是,测试对象是否应该始终使用下面层中的存根对象才能被隔离,并且它们通常是否有自己的数据集? Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data? 或者我的Web方法是否可以在Application Layer中调用轻量级对象,该对象使用存根数据调用Persistence层?

Thanks for your help. 谢谢你的帮助。 This feels like the last bit of the puzzle for me ... 对我来说这感觉就像拼图的最后一点......

Ideally in unit testing each subject under test is isolated from its dependencies. 理想情况下,在单元测试中,每个受测试的主体都与其依赖关系隔离。 You do not want to think your subject under test is broken because one of its dependencies broke and caused the subject under test to fail. 您不希望认为您所测试的主题被破坏,因为其中一个依赖项已破坏并导致测试中的主题失败。 If you test like this, you might spend a lot of time tracking down bugs in the wrong place. 如果你这样测试,你可能会花很多时间在错误的地方追踪错误。

Testing how things operate together is the domain of integration testing, not unit testing 测试事物如何一起运行是集成测试的领域,而不是单元测试

Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data? 或者我的Web方法是否可以在Application Layer中调用轻量级对象,该对象使用存根数据调用Persistence层?

If you do this, I wouldn't call the test a unit test in isolation anymore - if the test fails, where is the bug? 如果你这样做,我不会再把测试称为单独测试 - 如果测试失败,那么bug在哪里? - but an integration test. - 但是集成测试。 Don't misinterpret me, integration testing is fine too, it just has another purpose. 不要误解我,集成测试也没关系,它只是有另一个目的。 But if your goal is to unit test the website layer in isolation, you should mock/stub direct dependencies. 但是如果你的目标是单独测试网站层,你应该模拟/存根直接依赖。

Setting up test data can be tedious task. 设置测试数据可能是一项繁琐的任务。 If you are using DotNet you can make use of a library called NBuilder to generate test data very easily and quickly. 如果您使用DotNet,您可以使用名为NBuilder的库来非常轻松快速地生成测试数据。 It supports a nice fluent interface. 它支持一个漂亮的流畅界面。 You can read more about it here . 你可以在这里阅读更多相关信息。

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

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