提问 HN:单元测试应该侧重多少,以及何时开始?
1 分•作者: theturtlemoves•18 天前
我很好奇,是不是发生了转变。十多年前,我刚开始做初级软件工程师的时候,就学习了单元测试、集成测试和系统测试。我们整个代码库都经过了彻底的单元测试,并且还有多层集成测试和系统测试。后来我换了其他几家公司,但在某些情况下,根本没有任何自动化测试。然而,在阅读和学习最佳实践时,我得到的讯息是:单元测试所有东西!
我个人发现,当系统架构还不成熟时,单元测试反而会碍事,非常碍事。在这种和其他情况下,包括还没有任何测试的时候,通过集成测试或系统测试来验证行为似乎是更好的起点。
最近,我读到了一篇关于放弃严格的“单元测试一切”思维模式,转而采用集成测试的观点。我想这可能取决于很多因素,就像所有事情一样,比如你所从事的系统类型、系统的成熟度、工程师对自动化测试的经验等等。
我很想知道,在什么情况下,每种类型的测试对你有所帮助,以及在什么情况下会碍事(以及它会妨碍什么)。
查看原文
I'm wondering if a shift has occurred. When I started as a junior software engineer, over a decade ago, I learned about unit testing, integration testing, system testing. The whole codebase we worked on was thoroughly unit tested, and had layers of integration tests and system tests as well. I've worked for other employers since and in some cases any kind of automated testing was completely absent. Still, the message I got when reading and keeping up with best practices was: unit test ALL the things!<p>I've personally found that when the architecture of the system is not mature yet, unit tests can get in the way. Terribly so. Integration tests or system tests to assert behavior seem the starting point in this and other scenario's, including when there are no tests at all yet.<p>I've recently read a statement about letting go of a strict "unit test everything" mindset and go for integration tests instead. I'm thinking it probably depends, as with everything, on the type of system you're working on, the maturity of the system, the engineers' experience with automated testing, etc.<p>I'd be interested to learn when each type of testing helps you and when it gets in the way (and what it gets in the way of).