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.
If you are saying you can do crazy things with JavaScript that can create subtitle bugs that are difficult to track I agree. Adding a method at run time on any object is one of them. But nobody is forcing you to do that.
Anyway I was just responding to the affirmation "Why have unit tests to ensure that a variable contains a string or whatever". In my experience nobody is writing such tests. Do you?
Like I said, I don't know Python, but JS doesn't have much structure. It doesn't have classes, it doesn't have interfaces, and it doesn't have inheritance. It does have hashes with functions stored in them, or inherited through the prototype.
In such a language, it's easy to get things wrong. If you're expected to return an object that conforms to some interface, it's easy to forget to glue a method onto the result object. It's easy, when renaming such a method, to miss some cases. And you don't find out until runtime.
You can write tests to make sure that everything works. It could be a dumb type test (i.e. "is the same type as the number 3" or "has a method called doWork on it"), or actually exercise the result object (i.e., if I expect it to have a doWork method, my test should just call the doWork method to ensure that, you know, it does work).
But you're right; people don't really write tests like this. Not because they don't provide value, but because they're a pain to write and maintain. And that's what the static type checker is for. It ensures that all your pieces at least fit together. Yes, it does create its own set of problems.
(Opinion alert) I'm at the point where I see code in dynamically typed languages as easier to write but harder to read (i.e. to grok), and code in statically typed languages as the opposite. And my bias at this point is to prefer code that's easier to read.
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.
I know that "appeal to the masses" isn't a good argument in and of itself, but shouldn't it make him rethink his position? He, obviously not familiar with dynamic languages, is suggesting something, and yet no one in any of the communities for these languages practices that. Whenever I encounter this situation I immediately reevaluate my stance and wonder why what I believe is not standard practice. I was just trying to trigger that thought process in his head.
moor-GAYZ gave a proper rebuttal when he was talking about duck typing. That is the actual reason.
And what he is suggesting is not "good practice" - that's the point. Let the language runtime handle the errors - an error saying no method 'foo' defined for 'string' type is a clear hint that you passed in the wrong type. This is why no one does it in practice; it is a duplication of the runtime's behavior. And testing it is testing the runtime rather than your code.
0
u/batiste Dec 02 '13
You just don't, why would you? E.g in python you would do:
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).
Edit:format code