TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What's the boundary to watch for brittle tests when writing unit tests?

8 点作者 sriram_iyengar将近 8 年前
Because tests are meant to break when code changes.

2 条评论

richardknop将近 8 年前
Try to only write tests for public&#x2F;exported methods. Don&#x27;t test internal functions (there will be exceptions as with anything but this should be a general rule).<p>Design your interfaces and method signatures in a way which would minimize future need for changes.<p>For example, do not pass configuration options to your module as option1 and option2, instead wrap them in options&#x2F;config object and pass that to your module.<p>This allows you to add more configuration options in the future without having to break any existing interface.<p>Finally, when it comes to brittle unit tests, an extra layer of functional &#x2F; integration tests is what you should have.<p>These tests will be less brittle as they will break in more cases (i.e. if there is an issue on the edges of &quot;units&quot; of your codebase).<p>The tradeoff will be that functional&#x2F;integration tests will be slower and more difficult to execute (I suggest using docker-compose to run integration tests in a platform agnostic way).
itamarst将近 8 年前
You want to write the tests at the boundary where you want stability, where you want to prevent change.<p>Sometimes you don&#x27;t want to prevent change because it&#x27;s an implementation detail. That can mean private APIs, but also whole layers of abstraction.<p>Common layering, for example, is:<p>1. Low level operation that needs to be stable, so needs tests.<p>2. Medium level abstraction layer, not core business logic and builds on stable layer.<p>3. High level abstraction, the exposed public business logic API. Public API so needs tests.<p>Medium layer does not necessarily need tests in this case.<p>I have a bit higher level discussion of this in a talk I gave in May (first video): <a href="https:&#x2F;&#x2F;codewithoutrules.com&#x2F;talks&#x2F;" rel="nofollow">https:&#x2F;&#x2F;codewithoutrules.com&#x2F;talks&#x2F;</a>