unit testing - How to approach unittesting and TDD (using python + nose) -
i have been trying hang of tdd , unit testing (in python, using nose) , there few basic concepts i'm stuck on. i've read lot on subject nothing seems address issues - because they're basic they're assumed understood.
the idea of tdd unit tests written before code test. unit test should test small portions of code (e.g. functions) which, purposes of test, self-contained , isolated. however, seems me highly dependent on implementation. during implementation, or during later bugfix may become necessary abstract of code new function. should go through tests , mock out function keep them isolated? surely in doing there danger of introducing new bugs tests, , tests no longer test same situation?
from limited experience in writing unit tests, appears isolating function results in test longer , more complicated code testing. if test fails tells there either bug in code or in test, not obvious which. not isolating may mean shorter , easier read test, not unit test...
often, once isolated, unit tests seem merely repeating function. e.g. if there simple function adds 2 numbers, test
assert add(a, b) == + b
. since implementationreturn + b
, what's point in test? far more useful test see how function works within system, goes against unit testing because no longer isolated.my conclusion unit tests in situations, not everywhere , system tests more useful. approach implies write system tests first, then, if fail, isolate portions of system unit tests pinpoint failure. problem this, obviously, not easy test corner cases. means development not test driven, unit tests written needed.
so basic questions are:
- should unit tests used everywhere, small , simple function?
- how 1 deal changing implementations? i.e. should implementation of tests change continuously too, , doesn't reduce usefulness?
- what should done when test gets more complicated code testing?
- is best start unit tests, or better start system tests, @ start of development easier write?
regarding conclusion first: both unit tests , system tests (integration tests) both have use, , in opinion useful. during development find easier start unit tests, testing legacy code find approach start integration tests easier. don't think there's right or wrong way of doing this, goal make safetynet allows write solid , tested code, not method itself.
- i find useful think each function api in context. unit test testing api, not implementation. if implementation changes, test should remain same, safety net allows refactor code confidence. if refactoring means taking part of implementation out new function, it's ok keep test without stubbing or mocking part refactored out. want new set of tests new function however.
- unit tests not holy grail! test code should simple in opinion, , should little reason test code fail. if test becomes more complex function tests, means need refactor code differently. example own past: had code took input , produced output stored xml. parsing xml verifying output correct caused lot of complexity in tests. realizing xml-representation not point, able refactor code test output without messing details of xml.
- some functions trivial separate test them adds no value. in example you're not testing code, '+' operator in language works expected. should tested language implementer, not you. function won't need more complex before adding test worthwhile.
in short, think observations relevant , point towards pragmatic approach testing. following rigorous definition closely in way, though definitions may necessary purpose of having way communicate ideas convey. said, goal not method, result; testing have confidence in code.
Comments
Post a Comment