News for hackers. Stuff that matters.
Re:Most Unit Testing Is Waste (2014) [pdf] by biesnecker on Friday June 28, 2019 @09:20PM (#20310195)
My experience with testing is like that old adage about advertising, "I know I'm wasting 50% of my money but I don't know which 50%." Most of it is a waste but it's hard to know in advance which test will stop an engineer in a couple of years from altering some fundamental contract and bringing the system down.
Re:Most Unit Testing Is Waste (2014) [pdf] by ChrisCinelli on Saturday June 29, 2019 @04:27AM (#20311454)
I have a 3 part article on testing that I need to post...

Regarding wasting time on 50% tests... it depends what your system is. In most of the systems (the no mission critical ones, no general libraries) I like to use the 80%/20% rule. I always ask: what are the 20% of the tests that deliver 80% of the value?

You learn with experience but if you do not have it, this is a simple way to learn (as a team).

During the sprint, have people write down:

1 - The tests that actually failed and found a real problem before the code was shipped.

2 - The tests you wrote for the bugs that were found in production during the sprint. If you haven't written the test yet, ask: what kind of test would have found this bug?

Have people in the team share in the retrospective meeting. If you need more time at the beginning you can hold a meeting just for bug sharing. Discuss about the patterns. Commit to write more of these kind of tests.

In a few sprint, you should see regression bugs going down. If you also take a look at the tests than never fail, you will also have a sense of the bugs you should not write.

Let me know how it goes!

Re:Most Unit Testing Is Waste (2014) [pdf] by Pistos2 on Sunday June 30, 2019 @03:55AM (#20316978)
> If you also take a look at the tests than never fail, you will also have a sense of the bugs you should not write.

Just checking: Did you mean "the tests you should not write"?

Re:Most Unit Testing Is Waste (2014) [pdf] by chongli on Friday June 28, 2019 @10:25PM (#20310411)
I'll say that any unit test for a bug which would have been caught by a more sophisticated type system is a waste. I don't know how much time people spend writing such "obsolete tests", but I doubt it's insubstantial.
Re:Most Unit Testing Is Waste (2014) [pdf] by acjohnson55 on Friday June 28, 2019 @11:23PM (#20310634)
What if most type system security is a waste?

I don't personally believe this is the case, but I also can't tell you I know it to be false. What I do know is that types have a non-zero cost, and I think sometimes that's ignored, and only their benefits are acknowledged. But the efforts to bring types to historically dynamic languages show that not all common dynamic language idioms are amenable to reasonable type signatures. While some idioms (e.g. monkey patching) are problematic, that's not the case of all of them.

Re:Most Unit Testing Is Waste (2014) [pdf] by walshemj on Saturday June 29, 2019 @07:06AM (#20311909)
Apart from a bit more typeing up front I cant see many costs to typing.
Re:Most Unit Testing Is Waste (2014) [pdf] by rstuart4133 on Saturday June 29, 2019 @08:45AM (#20312273)
Try programming in Rust. The upfront cost in figuring out how to write the code to minimise unsafe()'s can be very high. This is because Rust's type system is so strong it both does the work of the garbage collector and catches things other languages would miss, like memory leaks.

That aside, that "bit" of typing can amount to a lot. Python programs are usually very compact compared to statically typed languages. Yes, this means writing more unit tests to cover the bits the type checker would have caught, but in my experience the total LOC with unit tests for 100% coverage in Python is still less than a statically types language.

That said, I still prefer statically types languages. The big difference between statically typing and unit tests is when the error is caught. Static typing catches the errors very early. In fact in modern IDE's that do incremental compiles it's literally after you have typed the statement. Errors that hang around tend to ripple through your code because your mental model is wrong, so catching them early means less errors in total. The effect can be large.

Re:Most Unit Testing Is Waste (2014) [pdf] by Too on Saturday June 29, 2019 @01:18PM (#20313662)
Python is very compact even with heavy use of type annotations. It's not the types that add the bulk of the code, other languages usually are more verbose in other ways also.
Re:Most Unit Testing Is Waste (2014) [pdf] by anoncake on Sunday June 30, 2019 @03:43PM (#20320068)
In a statically typed language, you sometimes have to make the code more verbose to satisfy the type checker. In Python, you can just not use it in those cases.
Re:Most Unit Testing Is Waste (2014) [pdf] by tsimionescu on Saturday June 29, 2019 @01:43PM (#20313791)
There are costs, and they go up significantly the more sophisticated your type system is/the more speicifc you try to be with your types.

The way I see it there are two kinds of costs type systems may impose. Very simplistic type systems, such as the ones in C, Go, and even C++, Java, C#, often force you to write code that is too specific, thus forcing duplication (C and Go especially suffer terribly from not having generics - in Go especially its impossible to write a well typed function that can be passed an array of any kind and return an element of that array).

On the other hand, of your type system is powerful enough, you still get a similar problem when trying to write code that is abstract enough to be reusable, but specific enough to be safe.

For example, if you were writing code for a physical simulation system, you would want to have all quantities carry their measurement unit in their type, to avoid mismatches.

However, you wouldn't want to rewrite maths code for each measurement unit. However, to properly keep track of types in complex maths code gets pretty ugly pretty quickly. For example, a function that wants to compute the scalar product of 2 2-dimensional vectors would only work if the first values of each vector have the same/convertible units as the second pair, in any order (m,s dot s,m should be ok). If you move to matrix multiplication, the types become even more complicated, and this is still very basic algebra. If you want your function to be applicable to arbitrary matrix sizes you're already writing a complex library.

Re:Most Unit Testing Is Waste (2014) [pdf] by acjohnson55 on Saturday June 29, 2019 @01:50PM (#20313837)
I have two thoughts on this.

First, static typed languages often simply subtract features that would be available in dynamic languages. So, that's a cost that's paid on day zero, and it can be easy to forget about.

Secondly, there are absolutely times when something that is conceptually easy to describe is a real beast to model in a type system. Think DSLs for SQL, for example. A non-trivial amount of time can be lost fighting these battles.

Re:Most Unit Testing Is Waste (2014) [pdf] by kybernetikos on Saturday June 29, 2019 @05:40PM (#20314961)
That's because some of the cost you pay by changing the way you think so that it doesn't even occur to you to write programs that the type system won't like.
Re:Most Unit Testing Is Waste (2014) [pdf] by osrec on Saturday June 29, 2019 @07:56AM (#20312075)
They can also be more difficult to refactor and adjust at a later date. This can be especially problematic if you're in an environment where logic needs to be altered rapidly in response to changing external conditions/requirements.
Re:Most Unit Testing Is Waste (2014) [pdf] by dahfizz on Saturday June 29, 2019 @08:22AM (#20312168)
The added "difficulty" is just the cost of doing a proper, correct refactor.

Dynamic languages are not helpful just because they let you write bugs faster.

Re:Most Unit Testing Is Waste (2014) [pdf] by chrisan on Saturday June 29, 2019 @08:31AM (#20312206)
I wouldnt even call that difficulty. That is a bloody loved feature in my book
Re:Most Unit Testing Is Waste (2014) [pdf] by sjy on Saturday June 29, 2019 @08:41AM (#20312254)
Developers are expensive though. If dynamic languages let you write buggy code in half the time that it would take to write perfect code in a static language, that would be an acceptable trade-off in many contexts.
Re:Most Unit Testing Is Waste (2014) [pdf] by adwn on Saturday June 29, 2019 @11:31AM (#20313121)
> If dynamic languages let you write buggy code in half the time that it would take to write perfect code in a static language, that would be an acceptable trade-off in many contexts.

Shit at 50% off is still shit. You're not being paid to produce shit, even if you might be really, really efficient at doing so.

Re:Most Unit Testing Is Waste (2014) [pdf] by sjy on Sunday June 30, 2019 @02:19AM (#20316674)
I assure you that many developers are being paid to produce shit.
Re:Most Unit Testing Is Waste (2014) [pdf] by walshemj on Saturday June 29, 2019 @09:54AM (#20312580)
Fixing bug later on get progressively more expensive, I though we had got over the KLOC metric decades ago.
Re:Most Unit Testing Is Waste (2014) [pdf] by walshemj on Saturday June 29, 2019 @09:49AM (#20312555)
Can you give some examples why - do you mean changing a variables type? that should jut be a matter of a find and replace.
Re:Most Unit Testing Is Waste (2014) [pdf] by raverbashing on Saturday June 29, 2019 @09:04AM (#20312350)
That's because you (and many others) are drinking the static typing koolaid

Now try serializing things in XML instead of JSON. But not one type of message, multiple types, with flexible formats.

"Oh but this never happens" except it does.

Not to mention the iteration speed whenever you need to fix something.

Re:Most Unit Testing Is Waste (2014) [pdf] by walshemj on Saturday June 29, 2019 @09:52AM (#20312568)
?? sorry what do you mean, I was happily reading multiple types of logs with multiple different format records for a Billing system using statically typed PL/1G in the 80's

Apart from both json and XML are a PITA to work with in the first place

Re:Most Unit Testing Is Waste (2014) [pdf] by ptx on Saturday June 29, 2019 @10:58AM (#20312942)
JSON is wonderfully simple to work with - in a dynamic language. Having to map it to a statically known schema, as you would in a statically typed language, loses that simplicity and can be, as you say, a PITA.
Re:Most Unit Testing Is Waste (2014) [pdf] by Gibbon1 on Saturday June 29, 2019 @04:31PM (#20314606)
I think that's a defect of JSON not statically typed languages.
Re:Most Unit Testing Is Waste (2014) [pdf] by raverbashing on Saturday June 29, 2019 @04:45PM (#20314669)
"defect of JSON" while it could be better, it has some type definitions and as I said, people are too deep in the static world to see outside of it
Re:Most Unit Testing Is Waste (2014) [pdf] by quanticle on Saturday June 29, 2019 @01:22AM (#20310936)
The problem is that sophisticated type systems only catch a subset of the bugs that a unit test can catch. For example, let's say I'm adding the ability to transfer funds from one account to the other in a banking application. I want to display a warning when the amount of money being transferred is over a certain percentage (let's say 95%) of the funds in the account. That's pretty easy to do in a unit test: create mock account, call the transferFunds() method, and verify that the warning is triggered when the value being transferred is over 95% of the amount in the account.

How would I do that with a type system?

Re:Most Unit Testing Is Waste (2014) [pdf] by tempguy9999 on Saturday June 29, 2019 @04:45AM (#20311511)
This is very much not the kind of bug that the parent was talking about. He's talking about stuff like passing an int value to a (supposedly) string parameter. Basic type stuff.

I also agree, as years ago I wrote a large amount of javascript which had type assertions pretty much everywhere. They didn't take long to write but I'm very glad I did them, and I'd rather have the language do it to save me time, clutter and maintenance.

Lax typing is a real cost. I've been working on some SQL I inherited and even SQL's not-too-bad typing allowed the original coder to mix types where I wouldn't, and create potential runtime errors eg. to assign the contents of a 64-bit integer field to an 32-bit integer field. That's legal and gives no warning. It's also suddenly a runtime error when it exceeds 2^31 (and there are other possible consequences such as screwing up the optimiser). I'd much rather it forced me to match types exactly, or explicitly cast.

Re:Most Unit Testing Is Waste (2014) [pdf] by dahfizz on Saturday June 29, 2019 @08:30AM (#20312194)
The parent never claimed that a type system means bug free code, or replaces all unit tests. However, it does mean you need less tests.

In a typed, compiled language you get some pretty nice guarantees just by getting your code compiled. In a dynamic language, you have no way to know if your code even runs until you have 100% code coverage.

Re:Most Unit Testing Is Waste (2014) [pdf] by mrmr1993 on Saturday June 29, 2019 @01:49AM (#20310985)
To my mind, a unit test should test a unit: something which functions independently, and which is interacted with through an abstraction layer. Type systems are good tools for clarifying and catching bugs around these abstraction layers.

Maybe my understanding is wrong, but what you described doesn't seem like a unit test. I think the appropriate term for a test like this at the junction of UI, business logic and code-level triggers is 'feature test', 'integration test', or maybe even just 'test'.

Re:Most Unit Testing Is Waste (2014) [pdf] by jlg23 on Saturday June 29, 2019 @06:37AM (#20311831)
Re:Most Unit Testing Is Waste (2014) [pdf] by qznc on Saturday June 29, 2019 @02:26AM (#20311085)
No mainstream type system can do that so GP would be fine with a unit test here.

You could use Coq, Isabelle, or Lean. Their type systems are powerful enough to allow such checks.

Re:Most Unit Testing Is Waste (2014) [pdf] by slifin on Saturday June 29, 2019 @05:26AM (#20311636)
If anyone does have this case it's two distinct functions in my mind the first function does the comparison and returns the warning

You can then assert and type against the warning function, the printing function you could only assert against with output buffering or something like that but in my mind that's less likely to go wrong anyway

Keep your type system simple, custom types beyond in built primitives cause head aches imo

Re:Most Unit Testing Is Waste (2014) [pdf] by xorcist on Saturday June 29, 2019 @04:38AM (#20311486)
At the risk of veering off topic, I'm genuinely curious what kind of bug that unit test is designed to catch?

Given that transferFunds shows a warning over that 95% threshold, what type of change is going to break that logic? Some sort of botched code rewrite where values doesn't retain their meanings? That batchMode shouldn't trigger the warning? That race condition between the check and the actual transfer?

Most of the unit testing I see is more like test-of-defintions (is fullName still 80 characters wide and doesn't accept null bytes?) which confuses me because a definition can only be correct or incorrect in context.

I accept the usefulness in dynamic languages in the absence of types. My Personal preference is for tests to be a bit higher level (does login with a username containing null still cause an exception?) but I have come to terms with the fact that few people agree with me across multiple organizations, so there must be some point to this trivial testing that is completely lost on me.

Re:Most Unit Testing Is Waste (2014) [pdf] by nickjj on Saturday June 29, 2019 @07:22AM (#20311957)
> My Personal preference is for tests to be a bit higher level (does login with a username containing null still cause an exception?).

These tests are great, but what if username has a bunch of validations on it?

For example, it's reasonable to think a username field might be validated with:

- Must be required

- Within 1-32 characters that match a certain pattern (let's say a regex to limit it to lowercase letters, numbers and -)

- Must not be a blacklisted word (admin, administrator, etc.)

- Must be unique (enforced with a database index)

Pretty standard stuff. Are you going to write 5 integration tests for this? 4 to test each validation and then the success case? These would be tests that exercise your entire web framework's routing stack from request to response (ie. the user visiting a /register URL and then submitting the form).

Personally I would not. I would write 1 unit test for each of those things (4 unhappy cases where I assert a specific validation error for each invalid input and 1 happy case where with valid input I expect 0 validation errors). In this case, the "unit" would likely be a `register_user` function that accepts params as input and either aborts with validation errors, or succeeds by writing the record to the DB.

Then, for an integration test I would have 2 tests. One to make sure with invalid input I end up with some type of error displayed in the HTML response (it doesn't matter which one), and another test with the success case to make sure things work when they should (such as the user is registered and a new record was created in the DB).

So I end up with a tiny bit of overlap in tests. Technically the unit test for the success case doesn't need to be there since the integration test covers it but I usually include it for the sake of completeness because it's usually like 4 lines of code to make that test but I'm not 100% opposed to someone saying it should be left out.

Re:Most Unit Testing Is Waste (2014) [pdf] by will4274 on Saturday June 29, 2019 @06:04AM (#20311727)
> Some sort of botched code rewrite where values doesn't retain their meanings?

Yes, exactly. It's supposed to catch the rewrite where somebody turns (a * 100) / b > 95 into (a / b) * 100 > 95 and suddenly, the code doesn't work anymore.

Showing a warning is full scenario - it has a whole UX. If this warning is important enough to have a PM, a UX designer, and be translated into 20 languages, it's important enough for the engineer to make sure it actually shows up when its supposed to.

Re:Most Unit Testing Is Waste (2014) [pdf] by maccard on Saturday June 29, 2019 @04:29AM (#20311459)
I think the point is that if that was written in JS you would have a test to ensure your method behaves correctly with characters passed in, or strings, or just nil(/nul?) Whereas in Java et Al, you don't need any of those tests, you just make your parameter some sort of numeric and that just isn't a problem any more.
Re:Most Unit Testing Is Waste (2014) [pdf] by seppel on Saturday June 29, 2019 @07:33AM (#20311994)
While I agree that this is hard to implement with a type system, I never want to see this implemented as a unit test.

What you describe is a real use case that I personally want to have tested on a real system with all the stuff in place (but test database instead of the real one). It has nothing to do with one unit you can test on its own.

And the crucial problem here is that you tie your testing code on the inner workings of transferFunds, when you are mocking stuff.

> and verify that the warning is triggered when the value being transferred is over 95% of the amount in the account.

Please also note that you cannot verify this property using testing. You can only verify that this works for one particular set of values (or a big but finite set of values). Testing never can verify that something always works. That's the wrong tool for this job.

Re:Most Unit Testing Is Waste (2014) [pdf] by floriol on Saturday June 29, 2019 @09:47AM (#20312544)
With TDD, writing unit tests accompanies writing code - which in itself necessities thinking in terms of testability, which is a good thing making the program's components more separable. Regarding the former example, mocking only a single part where it connects to other parts of the system (for example some interface through which it sends warnings to users) is easy and makes the resulting code better (eg, easy to replace the notification manager).
Re:Most Unit Testing Is Waste (2014) [pdf] by seppel on Saturday June 29, 2019 @02:20PM (#20313973)
> With TDD, writing unit tests accompanies writing code - which in itself necessities thinking in terms of testability

I, as a user of your software, only care about the feature, and that the feature works. I don't care about testability, whatever that means. And since I case about features, I think this entails that features have to be tested end to end, and not some unrelated classes ("mocks") that are not even used in the final product.

> is easy and makes the resulting code better (eg, easy to replace the notification manager).

It might or it might not. I have seen too much code which was written way too complex which a lot of unnecessary classes and interfaces, just for the sake of "testability". However, some features did not work every once in a while.

If you have the need for different notification backends, go ahead. Even if you need a notification manager (whatever this is) for this. But don't make code more complicated (sometimes called "test-induced design damage") without need.

Re:Most Unit Testing Is Waste (2014) [pdf] by ben0x539 on Friday June 28, 2019 @10:36PM (#20310453)
Is it a waste to keep the test around until you're ready to switch to a more sophisticated type system?
Re:Most Unit Testing Is Waste (2014) [pdf] by chongli on Friday June 28, 2019 @10:40PM (#20310467)
It adds to the technical debt of the project. Some teams may never decide they're "ready" for a more sophisticated type system. That's fine, but they're still on the hook for maintaining those test suites which might pose significant roadblocks to refactoring or rewriting parts of the system.
Re:Most Unit Testing Is Waste (2014) [pdf] by iso-8859-1 on Friday June 28, 2019 @11:28PM (#20310654)
You can prove a merge sort correct with dependent types. However, a typed implementation of merge sort will arguably be more difficult to mold into a TimSort if it isn't dependently typed. What you say?
Re:Most Unit Testing Is Waste (2014) [pdf] by chongli on Saturday June 29, 2019 @06:36AM (#20311827)
I don't have any experience with dependently typed languages. I hope (schedule permitting) to take a course in dependently typed programming (using Agda) and formal verification in the fall. I'll have much more to say on the topic after completing the course.
Re:Most Unit Testing Is Waste (2014) [pdf] by pojzon on Saturday June 29, 2019 @03:39AM (#20311320)
A question, coz Im not sure I follow. How is type system (as described by wiki) supposed to test domain or functionality?

Or are you talking here about completely different type system ?

Re:Most Unit Testing Is Waste (2014) [pdf] by ben0x539 on Saturday June 29, 2019 @04:41PM (#20314654)
What you can do with the type system depends on the language. It can't replace tests entirely, but it can usually make some more boring tests superfluous, and in more exotic languages the type system can be surprisingly helpful.
Re:Most Unit Testing Is Waste (2014) [pdf] by baddox on Friday June 28, 2019 @11:06PM (#20310561)
Presumably you’d need to also know that the unit tests aren’t being used locally while developers develop. It’s pretty common to run a unit test watcher while doing local development, and have that catch breakages that never make it to CI.
Re:Most Unit Testing Is Waste (2014) [pdf] by sametmax on Saturday June 29, 2019 @04:43AM (#20311509)
My take on that is to make tests using a unit test lib, but a bit more macro. Midway between unit tests and e2e. It's not pure, it's not best practices, but it's great value.