TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

TDD, where did ‘I’ go wrong

32 pointsby hashxalmost 11 years ago

7 comments

chatmastaalmost 11 years ago
The author suggests redirecting the scope of &quot;TDD&quot; to testing his app&#x27;s public API endpoints, rather than internal functions. His logic is that public API endpoints represent the contracts for business logic with customers, and therefore are the highest level that can be accurately tested.<p>I am not convinced of this argument. Obviously, it&#x27;s important to heavily test the endpoints of any API you make public. In fact, that&#x27;s a situation where you want 100% coverage. However, testing the endpoints of an API does not allow you to forego testing its internals. At best, a passing suite of API endpoints should give you <i>reasonable</i> confidence that the internals are functioning properly.<p>An API is an abstraction over many moving parts. Any good API will consolidate thousands of lines of business logic into a few endpoints. There is a lot of room for error in the layer of abstraction between API and internal logic. It&#x27;s entirely possible that a API endpoints could appear to be functioning properly, but actually be relying on broken internal code.<p>For example, consider a fruit basket API. You can insert fruit into the basket, and check what fruit is in the basket. A suite of tests for the API endpoints could insert fruit, and then check that it&#x27;s there. In this case, the API is hiding a lot of internal logic. Storage mechanisms, data persistence, fault tolerance, and a slew of other logic decisions are completely opaque to the API consumer.<p>What if the internal code incorrectly stores the fruit in a temporary file? The API test will pass if it inserts fruit and then checks that it&#x27;s there. But it is not going to check for that same fruit in an hour. What if it&#x27;s gone?<p>Internal logic like data persistence is (rightfully) opaque to API consumers. That means that no testing of API endpoints can validate all internal logic. Therefore, you cannot ignore testing the internal logic in favor of only testing API endpoints.
评论 #7975715 未加载
评论 #7975507 未加载
评论 #7975474 未加载
评论 #7975513 未加载
评论 #7975455 未加载
morgantealmost 11 years ago
I&#x27;m not sure why the author attempts to redefine &quot;unit testing&quot; at a higher level of testing. It seems pretty well-accepted that most API tests are, in fact, integration tests. There&#x27;s no need to use the term &quot;unit test&quot; in this case.<p>Otherwise, I quite agree. I don&#x27;t write unit tests. A complete set of integration tests gives me about 80% confidence in the correctness of code. Unit tests might bump that up to 90%, but you&#x27;ll never get to 100% from testing alone and the substantial effort necessary to get there just isn&#x27;t worth it.<p>Testing follows the classic 80&#x2F;20 rule. Integration tests only take 20% of the effort required for full unit test coverage, but can give you the bulk (80%) of the benefits. For all but the most sensitive of applications, it&#x27;s probably not worth it to put in the additional 80% effort for a mere 20% gain.
评论 #7975508 未加载
corporealshiftalmost 11 years ago
I think there&#x27;s a lot of value in balancing this approach with smaller sized tests, especially around complex code.<p>The problem that the article doesn&#x27;t address is if your test fails finding the failure point isn&#x27;t as straightforward as it is with smaller units.<p>However the points made here and in the discussions linked are valid and I think this is the right approach to testing. But I think that there&#x27;s still lots of value in writing and maintaining tests around smaller units of code. Balancing the two approaches brings us solid test coverage along with easy to debug test failures.
derefralmost 11 years ago
When you have a service-oriented architecture with small, atomic services (&quot;microservices&quot;), unit tests are indistinguishable from functional tests: each test suite covers exactly one unit, which performs exactly one function, which maps to exactly one service with exactly one API. (Handily, in such a scenario, the service-registry also doubles as your mocking framework.)<p>I think unit tests do have one important use: testing library functions, e.g. cryptographic primitives. Most people aren&#x27;t writing &quot;library code&quot;, though; they&#x27;re employing it.
CHY872almost 11 years ago
Erm this smacks a little of absolutism. In many cases you&#x27;ll definitely want to test at a method level - if the behaviour of a single method is complex but its not part of the API, if you want to test it properly, you&#x27;ll likely still have to have a similar number of unit tests (to properly pinpoint a bug), you&#x27;ll just need to move them up to the API level - and furthermore, the tests you need to come up with are far more complex, as they have to anticipate the outputs of each function call as well (in order to obtain the same coverage).<p>It&#x27;s correct that by reducing your number of units and making them more broad scoped, you potentially reduce the number of undetectable faults you are catching (but need not), but you also make crafting the input that detects the remaining faults much more difficult.<p>The correct approach is (it seems obvious to me) to mix the two. Don&#x27;t force tests to be on a per-method basis, but also don&#x27;t neglect to individually test those methods that can be better tested by individual testing. It seems like common sense - following a policy such as this so rigidly clearly leads to either wasted time and inferior APIs or missed test cases - the former I believe the OP has discovered already, the latter I&#x27;m sure they&#x27;ll discover in due time.<p><a href="http://www.quickmeme.com/img/0f/0fb4fa35fad1b9ed112dc7584f47c531cf14a1c3c55d64bed93d33d5330dfcd1.jpg" rel="nofollow">http:&#x2F;&#x2F;www.quickmeme.com&#x2F;img&#x2F;0f&#x2F;0fb4fa35fad1b9ed112dc7584f47...</a>
评论 #7978531 未加载
stephenbezalmost 11 years ago
This sounds a lot like Mock Client Testing: <a href="http://www.jaredrichardson.net/blog/2005/06/20/" rel="nofollow">http:&#x2F;&#x2F;www.jaredrichardson.net&#x2F;blog&#x2F;2005&#x2F;06&#x2F;20&#x2F;</a><p>(I think the writeup in the Ship It! book was better than this post)
CmonDevalmost 11 years ago
When unit tests are your only tool for verification, everything looks like a TDD target.