I've been attempting to discipline myself into making more extensive use of testing in general, but of late, in Django.
There are parts of the project I'm working on that need refactored and writing tests that have good coverage of the various function points is going to put me in a better position to be able to refactor confidently. For reasons that should be obvious, I've begun implementing unit tests in the "account" app within the project.
I've managed to break the account registration and transaction process into three unit tests so far, but one of the tests seems a bit too big/fat, and it's a little bit repetitious. The reason for this is that the transaction is stateful and I cannot rely on globals for unit tests. (Test order not guaranteed, cannot use that.)
So that would be my first question, how do I cope with a stateful transaction in terms of breaking it down more? Just deal with the fact that I'm repeatedly retrieving and validating the same data cross more than one test, or is there a better way?
More importantly, are there any books/materials/tutorials that take a pragmatic viewpoint on unit testing in development (ie, not rigidly 'test first') that you would recommend?
Test-first methodology isn't really an option aesthetically and practically because I'm coming into an ongoing project and the culture/practice wasn't there for it. I'm the first one to start making use of tests.
Thanks.
On a slightly different note, perhaps the layout of the test looks off because you're testing too much. It's difficult to comment too much without code, but you might want to consider assuming that certain things "just work" when using them in more complex tests.
That is, if a User depends on an Account being created, create the account and just assume that everything went fine. If that happens to fail, another test elsewhere should fail also which will be more helpful when debugging.
PG's most recent essay said it nicely: every line of code is a liability, even tests. The more you repeat yourself (testing things were created correctly that are tested elsewhere) the more code you then need to maintain and update should the requirements ever change.