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.

Start test names with “should” (2020)

51 pointsby nigamanthover 2 years ago

17 comments

codetrotterover 2 years ago
I prefer that the test name doesn’t say “should”. Instead, I like when it states the expected behaviour as though it was a fact.<p>“Should” to me is too weak.<p>No, it’s not just “should”. It does. And if it doesn’t then either the implementation is broken, or the test is bad.<p>“Should” has connotations for me of something that we want but which we don’t require.<p>Therefore, I don’t use the word “should” in my test names.<p>Instead of “should have length 256”, I say “has length 256”.
评论 #34762709 未加载
评论 #34762323 未加载
评论 #34762215 未加载
评论 #34762907 未加载
评论 #34762231 未加载
评论 #34762533 未加载
mnd999over 2 years ago
&gt; It removes redundancy<p>If it’s at the start of every test it’s completely redundant.
评论 #34762226 未加载
doctor_evalover 2 years ago
Nothing in this article makes sense to me. What is this, Hungarian notation for tests?<p>What if a test is testing that something shouldn’t happen? Apostrophes aren’t even legal function names in most languages. (&#x2F;s)<p>&gt; If the only output of a failing test is just a binary value like “FAIL”, that test is only giving one bit of information to the developer.<p>Well that aaaand<p>&gt; A good test framework will also print the test name and a call stack.<p>Ok so more than one bit of information, it literally tells you the line number and filename of the failed test so you can go look at it. But yeah maybe bad developer didn’t put any comments in the test and the code isn’t obvious but sure they’re gonna name the test using a magic template.<p>&gt; For example, they could point out that “should replace children when updating instance”<p>So in Go we would have:<p><pre><code> func TestShouldReplaceChildrenWhenUpdatingInstance(t *testing.T) { … } </code></pre> Argh this nonsense cargo culting just does my head in and I should go to sleep.
评论 #34763032 未加载
jjgreenover 2 years ago
Strange, RSpec seems to go in the opposite direction, used to be<p><pre><code> it &#x27;should increment the counter&#x27; do ... </code></pre> but now the active voice preferred<p><pre><code> it &#x27;increments the counter&#x27; do ... </code></pre> I much prefer the latter.
评论 #34762930 未加载
评论 #34762556 未加载
评论 #34766973 未加载
mike256over 2 years ago
Oh no, please not again... In 2021 we had some of our workers do courses where this was said. The effect was that many tests with really bad names where just renamed from testXXX to shouldXXX. It took a very long time to get those workers to write meaningful names and get rid of this should in the beginning (our framework recognizes tests by a name starting with test. The renamed tests have not been executed since the renaming).
splittingTimesover 2 years ago
Roy osherove&#x27;s naming convention served me well over the years:<p>UnitOfWork_StateUnderTest_ExpectedBehavior<p><a href="https:&#x2F;&#x2F;osherove.com&#x2F;blog&#x2F;2005&#x2F;4&#x2F;3&#x2F;naming-standards-for-unit-tests.html" rel="nofollow">https:&#x2F;&#x2F;osherove.com&#x2F;blog&#x2F;2005&#x2F;4&#x2F;3&#x2F;naming-standards-for-unit...</a>
评论 #34762306 未加载
ChrisMarshallNYover 2 years ago
...unless you use Apple&#x27;s XCTest framework[0].<p>In that case, it&#x27;s required that tests be prefixed with &quot;test&quot;[1].<p>[0] <a href="https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;xctest" rel="nofollow">https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;xctest</a><p>[1] <a href="https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;xctest&#x2F;defining_test_cases_and_test_methods" rel="nofollow">https:&#x2F;&#x2F;developer.apple.com&#x2F;documentation&#x2F;xctest&#x2F;defining_te...</a>
评论 #34762613 未加载
评论 #34763658 未加载
chrisandchrisover 2 years ago
&gt; It removes redundancy, because the function name should already be in the call stack.<p>I agree, as long as there is 1 methot to test per file containing the tests. Otherwise, I put the tested method name first (if it&#x27;s a unit test) and adhere to the pattern [method]_[should]_[condition]_[result], e.g. Divide_Int_ByZero_FailsWithException()
victorvoskover 2 years ago
I thought &quot;should&quot; was a product of silly TDD, like my widget &quot;should do this thing&quot; and it doesn&#x27;t yet because TDD, and then the developer would implement it.
tennis_80over 2 years ago
My team use GIVEN X AND Y SHOULD Z<p>It works nicely with Jest as you can nest test suites indefinitely, so each GIVEN statement goes in a nested describe block, but it is verbose.
评论 #34762335 未加载
hk1337over 2 years ago
&gt; Some frameworks require that test names are formatted in some special way, like starting with “test”, using snake case, camel case or other. I’m ignoring that part of the naming for brevity and to avoid focusing on a specific language.<p>I was thinking when I first saw this I thought this depends on what testing framework you’re using.<p>Basically, what this is saying is to follow the conventions of the testing framework?
klyrsover 2 years ago
Or just, y&#x27;know, write a brief comment. Testing is one of those places where the function name occurs exactly once, perhaps twice if the language doesn&#x27;t have a nice testing framework. It doesn&#x27;t need to be self-documenting.
评论 #34764157 未加载
clarkdaleover 2 years ago
The reason I was taught in a TDD classroom, is that starting with &quot;should&quot; lets your human brain start typing an easy to comprehend sentence.<p>It should be easy to write out requirments in simple terms.
nailerover 2 years ago
&gt; should replace children when updating instance<p>Or better yet, don’t start it with should:<p>test(‘replaces children when updating instance’)
评论 #34762957 未加载
nathanaldensrover 2 years ago
I prefer <i>must</i> instead of <i>should</i>. Should is passive and non-committal.
dborehamover 2 years ago
Please no. Grates to read code that doesn&#x27;t parse correctly as English.
funstuff007over 2 years ago
We often use the word &quot;ensure&quot; in test names and &#x2F; or doc strings.
评论 #34762974 未加载