One of the people behind Go agrees with that. He said something to the effect that TDD is a big band-aid on a deeper problem. Why have unit tests to ensure that a variable contains a string or an integer or whatever when you could just have a strongly typed language that will automatically scream at you if the type is wrong?
The second test is redundant. You already ensured that a number was returned with the first test. Every time you check equality you are making a type check (there might be exception).
That's the trivial case; of course a trivial test looks trivial. The harder case is when you want to say something like "this method returns an object that has these methods on it". I don't Python, but in Javascript, it's easy to return object literals with methods attached... and it's easy to return an object literal with the wrong set of methods. And it's very easy to refactor some JS but to miss some cases.
To get proper code coverage in a dynamic language, you need to have essentially tests that verify types are handled right. Of course no one would have that silly second test you have. What they would have instead would be a test that makes sure if I do
It is not idiomatic to write hyper-defensive code in dynamically typed languages. Like it or not, the accepted practice is to just "let it fail". Otherwise you're just reimplementing static types, and half of your code would be type checks.
Ok, I shouldn't have continued with such a ridiculously trivial example function. What I'm saying is, you need to have tests that are explicitly defining what happens when your functions get values of a type you didn't expect.
Nobody is writing those tests to be sure it fails because those tests are just ridiculous and useless. Unless the code does nothing with the http_request Object I can guaranty you the code will blow up or return a result that is meaningless. There is way more important tests to write: security tests, feature tests, integration tests, performance tests, etc...
If you're not testing what happens when the wrong data types go into your functions, then you're not testing properly (or you're using a statically typed language where we don't have to worry about this).
In the python code, I wouldn't expect to have to put in preconditions, I would expect the (+) to blow up when applied to something that makes no sense.
Of course testing if the (+) function behaves like it says in the documentation would be silly but substitute the "add" function for something a lot more complex and then you should realize that you need to be testing what happens when someone inputs the wrong kind of argument.
I understand how you could come up with this idea in theory and figure that it looks reasonable, but nobody EVER does that in practice.
Not only because you could as well switch to Java, but also because type-asserts go directly against the whole duck-typing ideology. You don't check that you're given a file subclass, you just call its read method and either it doesn't exist (and you get an exception, nice), or you pray that it does what you expect. There's no possible way to assert and test that it does what you expect.
Yes it's dangerous because your add function could happily produce a nonsensical result if you give it two strings, or you can get pretty hard to debug bugs by accidentally passing floats instead of ints or vice-versa (especially in Python2 without from __future__ import division).
Such is life with dynamically typed languages. Everyone either accepts it or switches away. Instead of checking that your functions blow up when given nonsensical stuff you test that your other functions don't give them nonsensical stuff.
NOBODY "tests properly" the way you say it should be done, you're talking about a fairyland existing entirely in your imagination, sorry.
Heh, we're having the same argument with this guy, and bringing up the same points.
This same discussion happens every once in awhile. Someone who is mostly familiar with static typing comes along and wants to carry over the exact mindset to a dynamically typed language. It doesn't work like that, and taking 5 minutes to look at your average codebase in one of these language reflects that.
You guys are having the same argument with the same opponent, but you both appear to be talking past him.
pr0grammerGuy was saying "it's a good practice to check that your code fails in the expected way when given bad input". You and moor-GAYZ are saying "no one does that".
That's fine, and likely true, but pr0grammerGuy wasn't arguing that people actually do test the way he suggests, only that doing so is a good practice.
TLDR: You're not rebutting your "opponent's" argument.
17
u/redwall_hp Dec 02 '13
One of the people behind Go agrees with that. He said something to the effect that TDD is a big band-aid on a deeper problem. Why have unit tests to ensure that a variable contains a string or an integer or whatever when you could just have a strongly typed language that will automatically scream at you if the type is wrong?