Mais conteúdo relacionado
Semelhante a Unit Testing Done Right (20)
Unit Testing Done Right
- 3. What is a unit test?
• Small
• Fast
• Tests a single unit <- Here there be dragons
- 8. Goals of unit testing over
time
• Find bugs
• Find regressions
• Improve design
- 14. "When a measure becomes a target, it
ceases to be a good measure.” -
Goodhart’s Law
- 17. Coverage tricks
• if (false) this_function_call_is_covered();
• // @codeCoverageIgnore
• multiple checks on the same line
- 20. Example
for
($i
=
0;
$i
>
10;
$i++)
{
if
($i
%
2
===
0)
{
echo
"Even:
";
}
else
{
echo
"Odd:
";
}
echo
$i,
"n";
}
- 28. Randomness
• Can the randomness come from outside?
• Do you need to know the exact value?
• Can you just verify “randomness”?
- 33. Good unit tests
• Single reason to fail
• Clearly named
• testGuestCanRegisterWithValidInformation
• testProcessData
- 37. Test smells
• Too many mocks
• Lots of setup required
• Can’t test w/o database/network/etc…
• Can’t inject mocks where needed
- 38. Test smells
• Mocks returning mocks
• Flaky results
• Unrelated code changes breaking tests
• Testing the implementation