unit testing - Test driven development - when/what to test? -
i trying started tdd right away unsure of when , should testing. first 2 tasks in new project i'm working on follows.
1) receive json formatted data on rest endpoint , save in database. data several car records -
{ "cars": [{ "make": "ford", "color": "blue", "year": "2010", "for_sale": true }, { "make": "bmw", "color": "black", "year": "2011", "for_sale": false } ] }
so data arrives @ rest endpoint , need save in database. need test task , if should like?
2) retrieve records database , display them in view/webpage (i.e. using templating system). records car records above , should displayed follows -
<ul id="cars"> <li id="car-1"> <div><span>make:</span><span>ford</span> </div> <div><span>color:</span><span>blue</span> </div> <div><span>year:</span><span>2010</span> </div> <div><span>for sale:</span><span>yes</span> </div> </li> <li id="car-2"> <div><span>make:</span><span>bmw</span> </div> <div><span>color:</span><span>black</span> </div> <div><span>year:</span><span>2011</span> </div> <div><span>for sale:</span><span>no</span> </div> </li> </ul>
so need test task , if should like?
what language, platforms etc using? perhaps can find examples you.
tdd tricky @ first, , task (with database , web parts) requires testing @ several levels.
first divide task single-responsibilities (which map classes) can unit tested. example, class takes json input, , hydrates object properties on it, tdd class. database layers hard unit test, normal use repository pattern mock when testing other classes.
db unit testing hard, consider "acceptance" or "integration" test around database. might test connects real test database, puts in test data, pulls out again, , verifies looks right. in theory, don't care database is, long stuff store comes out again, know it's working.
html / web testing best done @ high level using tools such selenium webdriver, allows write test code fires real browser, interacts page, , asserts content/behaviour expected.
this stuff learnt pair programming knows it, or perhaps attending class or training course. there lots of books blogs , tutorials, let learn in sandbox, simpler trying learn on real project, pressure stuff done in conflict learning.
edit: java , play framework. ok, don't know play framework specifically, quick json parsing if set right, reduces json parse function boilerplate code. there's not huge amount of value in tdd here, can if want. similarly, there's active-record style db layer? there's not lot of value in testing code library has provided (and dbs hard/impossible/pointless* unit test) .
edit:edit - turns out icky, apparently play uses static controller methods makes hard unit test (because can't inject dependencies - makes mocking difficult). i'm afraid without doing hours of research can't specifics, integration tests way go here, test several of units of code together, including db.
so in summary:
- don't sweat tdd on boilerplate. keep boilerplate isolated , thing (ie controllers web stuff, hand on other classes. repositories save , retrieve objects, not rules/decisions/manipulations.
- when start adding more business logic guts of app - keep isolated in business classes (ie - away web or db boilerplate) can unit test easily. tdd stuff.
- try integration tests across app test db. have real test db behind app, use app save something, retrieve it, , assert it's correct.
- use selenium test web pages.
*delete according testing beliefs.
Comments
Post a Comment