[Tutor] writing effective unittests

Alan Gauld alan.gauld at btinternet.com
Thu Jan 3 22:55:24 CET 2013


On 03/01/13 19:46, Luke Thomas Mergner wrote:
> What am I missing? The biggest problem is that no one is explaining the rationale behind testing.

See my other reply and check out the references to programming by 
contract, pre/post conditions and invariants. Those are all good things 
to test. Invariants are probably hardest but also most powerful IMHO.

But viesw your code as a contract with the user. What can the user put 
in, what should they get out? What could break it?

> I think what I need is a conceptual shift: how do python programmers use unittests?

Personally I only use TDD and formal unit tests when I'm writing something
a) big and/or complex
b) reusable - I'll be coming back to it in the future

For small, one-off tools and scripts the overhead is usually not worth 
while. And they are never 100% reliable, bugs will still sneak through. 
So for one-offs I just do informal testing.

In most cases with TDD I stick to black box testing but if its a 
reuseable class or a framework then I'll probably drop down to white box 
and if it's mission or safety critical I'll do glass box testing (ie 
100% code coverage) - That's to say in about 3 projects in 30 years...

> My next step is to look into buying some real books.

I'd only go there if you are really comfortable with programming in 
Python. Learn and use the principles but don't sweat over it until you 
really need to. It's more important to be comfortable writing good 
Python code than writing good tests for bad code!

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list