Essa apresentação é um relato de experiência a respeito da utilização do Test-driven Development (TDD) para o desenvolvimento do framework Esfinge QueryBuilder. Serão abordadas questões práticas e lições aprendidas durante esse desenvolvimento, focando em como trabalhar em um design complexo utilizando TDD. Algumas das questões abordadas serão: quando utilizar mock objects, como reduzir o esforço com a codificação dos testes, quando utilizar testes de integração e como fazer grandes refatorações. Cada tópico será ilustrado com exemplos reais ocorridos durante o desenvolvimento do framework.
11. That is the question!
To mock orTo mock or
not tonot to
mock?mock?
12. What should I do when
I have things that are
hard to test, like
external resources?
MOCK
13. Why?
It will make the test difficult
Test will be coupled with
external APIs
It can make the test slow
14. But should I mock
the external APIs
themselves?
MOCK
15. Why not? Create a class that
encapsulates the
access to this API!
It is a perfect match on the
class needs
The API can be hard to mock
Decouple the class from the API
16. What should I do when
I have classes that are
not exposed to the
class clients?
MOCK
17. Why not?
The solution can't be refactored
without changing the test
Class don't need to be exposed
Test will be coupled to the
solution
18. What should I do when my
class have a hotspot or a
dependence with variable
behavior?
MOCK
What should I do when my
class have a hotspot or a
dependence with variable
behavior?
19. Why?
Mock can explore all possibilities,
such possible errors
Mock can be used to divide class
responsibilities
Mock can be used to design the
dependence API
21. Unit or Integration ?Unit or Integration ?
Can I use both on TDD?Can I use both on TDD?
22. Unit Test
decouplingdecoupling
=
Creating unit tests you areCreating unit tests you are
dividing responsibilities and definingdividing responsibilities and defining
the interaction among the classes.the interaction among the classes.
23. You are having feedback on yourYou are having feedback on your
implementation, but it is notimplementation, but it is not
helping to define your design.helping to define your design.
Integration Test
black boxblack box
=
24. If I'm defining my
design using tests,
when can I use
integration tests?
26. Class A Class B Class C
Imagine that an
architecture
with these three
classes
27. Class A
Class C
MOCKUNIT TEST
Developing Class A, the services needed
from Class B were defined.
Class C interface were defined on its
own TDD session.
UNIT TEST
28. Class A Class B Class C
INTEGRATION
TEST
Now that everything
is defined, you can
use integration tests
to develop Class B
using TDD.
29. If you designed everything upfront, youIf you designed everything upfront, you
don't need TDD as adon't need TDD as a designdesign technique!technique!
32. When you always search for theWhen you always search for the
simplest solution, sometimessimplest solution, sometimes
you reach a dead end!you reach a dead end!
33. However, most of the time theseHowever, most of the time these
problems are concentrated on a singleproblems are concentrated on a single
class and isolated from the others.class and isolated from the others.
37. You still have to knowYou still have to know
patterns to understand the solutionpatterns to understand the solution
that you are driving through the tests.that you are driving through the tests.