本文翻译自:What does “DAMP not DRY” mean when talking about unit tests?

I heard someone say that unit tests (eg nUnit, jUnit, xUnit) should be 我听说有人说单元测试(例如nUnit,jUnit,xUnit)应该是

DAMP not DRY DAMP 不干

(Eg unit tests should contain "damp code" not "dry code") (例如,单元测试应该包含“潮湿代码”而不是“干代码”)

What are they talking about? 他们在说什么?


#1楼

参考:https://stackoom.com/question/R4mR/在讨论单元测试时-DAMP-not-DRY-是什么意思


#2楼

It's a balance, not a contradiction 这是一种平衡,而不是矛盾

DAMP and DRY are not contradictory, rather they balance two different aspects of a code's maintainability . DAMP和DRY并不矛盾,而是它们平衡了代码可维护性的两个不同方面。 Maintainable code (code that is easy to change) is the ultimate goal here. 可维护的代码(易于更改的代码)是此处的最终目标。

DAMP (Descriptive And Meaningful Phrases) promotes the readability of the code. DAMP (描述性和有意义的短语)促进了代码的可读性

To maintain code, you first need to understand the code. 要维护代码,首先需要了解代码。 To understand it, you have to read it. 要理解它,你必须阅读它。 Consider for a moment how much time you spend reading code. 考虑一下您花多少时间阅读代码。 It's a lot. 这是很多。 DAMP increases maintainability by reducing the time necessary to read and understand the code. DAMP通过减少读取和理解代码所需的时间来提高可维护性。

DRY (Don't repeat yourself) promotes the orthogonality of the code. DRY (不要重复自己)促进代码的正交性。

Removing duplication ensures that every concept in the system has a single authoritative representation in the code. 删除重复可确保系统中的每个概念在代码中都具有单个权威表示。 A change to a single business concept results in a single change to the code. 对单个业务概念的更改会导致对代码的单个更改。 DRY increases maintainability by isolating change (risk) to only those parts of the system that must change. DRY通过将变更(风险)仅隔离到必须更改的系统部分来提高可维护性。

So, why is duplication more acceptable in tests? 那么,为什么复制在测试中更容易接受?

Tests often contain inherent duplication because they are testing the same thing over and over again, only with slightly different input values or setup code. 测试通常包含固有的重复,因为它们一遍又一遍地测试相同的东西,只是输入值或设置代码略有不同。 However, unlike production code, this duplication is usually isolated only to the scenarios within a single test fixture/file. 但是,与生产代码不同,此复制通常仅与单个测试夹具/文件中的方案隔离。 Because of this, the duplication is minimal and obvious, which means it poses less risk to the project than other types of duplication. 因此,重复是最小和明显的,这意味着它比其他类型的重复对项目造成的风险更小。

Furthermore, removing this kind of duplication reduces the readability of the tests. 此外,删除这种重复会降低测试的可读性。 The details that were previously duplicated in each test are now hidden away in some new method or class. 之前在每个测试中重复的细节现在隐藏在一些新的方法或类中。 To get the full picture of the test, you now have to mentally put all these pieces back together. 为了全面了解测试结果,您现在必须将所有这些部件重新组合在一起。

Therefore, since test code duplication often carries less risk, and promotes readability, its easy to see how it is considered acceptable. 因此,由于测试代码重复通常风险较小,并且提高了可读性,因此很容易看出它被认为是可接受的。

As a principle, favor DRY in production code, favor DAMP in test code. 作为一个原则,在生产代码中支持DRY,在测试代码中支持DAMP。 While both are equally important, with a little wisdom you can tip the balance in your favor. 虽然两者同样重要,但只要有一点点智慧,你就可以为自己提供平衡。


#3楼

I agree with Chris Edwards in that you need to strike a balance between the two. 我同意Chris Edwards的意见,你需要在两者之间取得平衡。 Another thing to note is that if, in an attempt to remove duplication, you end up adding a lot of additional structure in your unit test code (ie when taking DRY to extremes), you run the risk of introducing bugs in there. 另一件需要注意的事情是,如果在尝试删除重复时,最终会在单元测试代码中添加大量额外结构(即将DRY置于极端情况下),则存在在其中引入错误的风险。 In such a situation, you would either have to unit test your unit tests or leave bits of structure untested. 在这种情况下,您要么必须对单元测试进行单元测试,要么保留未经测试的结构。


#4楼

I don't wish to duplicate the effort here, but you can have tests that are DAMP but have the benefit of DRY. 我不想在这里复制这些工作,但你可以进行DAMP测试但是有DRY的好处。 On the flip side, DRY tests won't satisfy DAMP tests in some cases. 另一方面,在某些情况下,DRY测试不能满足DAMP测试。

I've blogged about DRY vs DAMP which includes some examples. 我发表了关于DRY与DAMP的博客,其中包括一些例子。

Neither approach should be your only solution, sometimes DAMP is overkill, other times a very nice addition. 这两种方法都不是你唯一的解决方案,有时DAMP是矫枉过正,有时候是非常好的补充。

As a general rule you should apply the rule of three. 作为一般规则,您应该应用三个规则。 If you spot duplication a third time, it may be worth looking into writing DAMP style tests, but even then not all duplication is bad . 如果你第三次发现重复,那么编写DAMP样式测试可能是值得的,但即便如此, 并非所有重复都是错误的 。 Context matters. 环境很重要。


#5楼

Damp = 'Descriptive And Meaningful Phrases' - your unit tests should be able to be 'read': 潮湿='描述性和有意义的短语' - 您的单元测试应该能够“阅读”:

Readability is more important than avoiding redundant code. 可读性比避免冗余代码更重要。

From the article: 来自文章:

DAMP stands for “descriptive and meaningful phrases” and is the opposite of DRY, not in the sense that it says “everything should look like a trash heap and be impossible to read”, in that readability is more important than avoiding redundant code. DAMP代表“描述性和有意义的短语”,与DRY相反,不是说“一切看起来像垃圾堆而且无法读取”,因为可读性比避免冗余代码更重要。

What does this mean and where to use it? 这意味着什么以及在哪里使用它?

DAMP mostly applies when writing test code. DAMP主要适用于编写测试代码。 Test code should be very easy to understand to the point that some redundancy is acceptable. 测试代码应该非常容易理解,以至于某些冗余是可以接受的。


#6楼

DAMP - Descriptive And Meaningful Phrases. DAMP - 描述性和有意义的短语。

"DAMP not DRY" values readability over code re-use. “DAMP not DRY”重视代码重用的可读性。 The idea of DAMP not DRY in test cases is that tests should be easy to understand, even if that means test cases sometimes have repeated code. 在测试用例中DAMP不干的想法是测试应该易于理解,即使这意味着测试用例有时会重复代码。

See also Is duplicated code more tolerable in unit tests? 另请参阅单元测试中是否可以容忍重复代码? for some discussion on the merits of this viewpoint. 关于这一观点的优点的一些讨论。

It may have been coined by Jay Fields , in relation to Domain Specific Languages. 它可能是由Jay Fields创建的 ,与Domain Specific Languages相关。

在讨论单元测试时,“DAMP not DRY”是什么意思?相关推荐

  1. SSM中进行Junit单元测试时无法注入service

    场景 在SSM项目中进行Junit单元测试时调用外部的service时,在使用时打断点发现为空. 代码如下: public class AlipayTester {private PassOrderS ...

  2. Junit单元测试时提示:Method should have no parameters

    场景 Junit中运行单元测试时提示: Method XXX should have no parameters 如图: 代码如下: package com.ws.test.common;import ...

  3. SSM+Maven+Eclipse进行单元测试时提示找不到Config配置项/文件

    场景 在Eclipse+SSM+Maven中进行单元测试时,会提示Config配置文件/信息找不到 原因 在测试类中调用了正常业务中的方法/接口,而在方法中有用到常量的地方, 而常量又是在封装的常量类 ...

  4. java报错空指针异常_分析使用Spring Boot进行单元测试时,报出空指针异常

    使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...

  5. 单元测试时使用Ninject的小问题

    主要是Kernel没有被释放,Ninject的Kernel必须调用IDispose释放.也有可能是静态类型在VS单元测试时无法被释放. 先记录下这个问题 转载于:https://www.cnblogs ...

  6. SSM项目使用junit单元测试时Mybatis通配符加载Mapper不能正常加载

    问题描述:项目使用maven build 以及tomcat run能够正常运行,但是使用junit单元测试时却报错误 Caused by: java.lang.IllegalArgumentExcep ...

  7. java 单元测试 私有成员变量,单元测试时测试一个private私有方法 - - ITeye博客

    直接上例子 待测试的类 [code="java"]public class Calculator { private int count = 0; private int add( ...

  8. java类添加单元测试代码_如何在java中单元测试时跳过一段代码

    如果问题确实是: 如何在Java 然后我给出的答案同意单元测试时,我跳过一段代码.依赖注入,嘲讽框架绝对是真正的单元测试的正确途径. 但是,如果问题是: 使用JUnit(或其他单元测试框架) 然后我想 ...

  9. springboot进行单元测试时容易被忽略的几个问题

    在进行单元测试时,一般创建测试类,在生成的测试方法上加上@Test就行了.但使用spring的都知道,spring的各层类是依赖注入的,特别是分模块开发时更加明显,直接Test调用的注入类方法是没法执 ...

最新文章

  1. python基础语法有哪些-Python基础语法
  2. D3js(三):force实例
  3. Mac中常用的终端配置命令总结
  4. unity算法面试_Unity面试题汇总
  5. Ubuntu(Deepin)搭建Android开发环境(Android Studio)
  6. Coding之路——重新学习C++(6):一个String类
  7. 统计自然语言处理第二版 下载
  8. 小说取名软件(附带截图)分享与介绍
  9. java正则表达式 用户名_java之正则表达式
  10. android高仿人人网
  11. Python批量提取Excel文件中所有单元格批注
  12. debian linux上usb摄像头,[Debian] 安装USB摄像头(芯片ZC0301)驱动
  13. proc 文件的创建和读写
  14. 从前慢-Mysql高级及实战
  15. Olivetti Faces人脸数据集下载后图像分割matlab(亲测有效)
  16. 这是一个只有一句话的木MA
  17. win10桌面版outlook邮箱配置流程
  18. Redis学习之publish命令
  19. 关于LCD1602液显的使用细则
  20. oracle会计科目明细请求,Oracle EBS R12财务月结基础

热门文章

  1. shell正则表达式提取数字
  2. 前端案例-跟随鼠标移动的天使
  3. open cyper还是open cypher,李逵or李鬼?
  4. 0.96寸OLED驱动(基于STM32f103)
  5. java-php-python-ssm校园闲置物品交易系统计算机毕业设计
  6. 元数据治理平台Datahub学习交流群成立!
  7. 家用洗地机排名前十的产品有哪些、十大洗地机品牌排行榜
  8. [CODEVS1537] 血色先锋队 - BFS
  9. 类型四:间断点及分类
  10. 【Python】Python时间序列预测 | 经典季节性分解