News for hackers. Stuff that matters.
Re:Coming Soon: Machine-Checked Proofs in Everyday Development by hmottestad on Sunday December 31, 2017 @05:16AM (#16040035)
It's hard to know the intent of a developer by reading their code. You could always add a bunch of comments, which would help, but in the long run you risk them getting out of date.

My unit tests are basically "comments" for my code, that show me how the code is supposed to work. When I make changes, the tests will tell me if I broke the intentions of the initial developer.

Sure, when doing big changes, just updating and maintaining the tests is a lot of work, but so would maintaining documentation and comments be. So we have less documentation and fewer comments, and more tests.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by jondubois on Sunday December 31, 2017 @05:51AM (#16040117)
I find integration tests more useful for documentation because they show you how the system works at a higher level which is precisely what new developers need to understand.

If the code itself is written properly then the individual classes and objects used throughout should be high cohesion and easy to read without needing any comments or tests.

There are always cases where unit tests are important though like the parts of the code that deal with money.

But there is no benefit in adding unit tests if you're building a standard chat system for example; integration tests are sufficient in that case.

Also getting 100% test coverage is a complete waste of time if you don't actually test the right kinds of input and output parameters.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by rectang on Sunday December 31, 2017 @07:57AM (#16040422)
> But there is no benefit in adding unit tests if you're building a standard chat system for example

Disagree. Finding the right amount of unit testing is an optimization problem. Some amount is nearly always useful.

For starters: Integration tests will not necessarily exercise a full range of inputs for the low-level functions. For a chat system, I would think hard about how to test low-level functions with a wide range of Unicode input.

A chat system must survive in the harsh environment of the public internet. It needs to be hardened against unexpected user input, either incidental or malicious.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by crdoconnor on Sunday December 31, 2017 @10:13AM (#16040836)
>For starters: Integration tests will not necessarily exercise a full range of inputs for the low-level functions. For a chat system, I would think hard about how to test low-level functions with a wide range of Unicode input.

I don't see any problem at all with writing integration tests that exercise a chat system with a wide range of unicode input.

I'd view it as irresponsible not to, in fact.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by rectang on Sunday December 31, 2017 @12:35PM (#16041482)
Low-level functions may be reused in combinations which limit what gets fed to them by higher-level tests. Perhaps there are bugs in some text processing widget, which only happen on small blocks -- and small blocks only get fed to low-level functions on disconnection from the service, which would be too costly to repeat exhaustively in integration tests.

The most reliable way to address this kind of problem is to match the tests to the function. Look at what each function does, anticipate edge cases, contemplate the core purpose, then write what you believe are an appropriate number of unit tests.

You can choose not to prioritize the problems which are revealed by unit testing but not integration testing, but you can't say that such problems don't exist.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by crdoconnor on Monday January 01, 2018 @10:44AM (#16046452)
>on disconnection from the service, which would be too costly to repeat exhaustively in integration tests.

Um, disconnection from the service is the exact kind of scenario I'd want to repeat exhaustively in an integration test.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by rectang on Monday January 01, 2018 @12:14PM (#16046885)
This reply doesn't address the principle that there must be issues which can be addressed using unit tests but not integration tests. Even if you take every example I give and claim "doesn't count, I'd test for that", you're just writing unit-tests-once-removed using an integration testing framework.
Re:Coming Soon: Machine-Checked Proofs in Everyday Development by crdoconnor on Tuesday January 02, 2018 @10:05AM (#16052197)
>This reply doesn't address the principle that there must be issues which can be addressed using unit tests but not integration tests.

Well, that's asking me to disprove a negative. All I can say is that I don't think that principle is true.

One counter-example of a scenario that is impossible to integration test but possible to unit test would be sufficient to disprove my hypothesis, but I don't think it exists and I don't think you can provide it.

>Even if you take every example I give and claim "doesn't count, I'd test for that", you're just writing unit-tests-once-removed using an integration testing framework.

I'm really not. I can test disconnection scenarios in an integration test without even coupling to the language under test by running a service that intercepts network traffic. It's obviously not a "unit test once removed" if I can literally swap out the entire language under test and not have to change the test.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by rectang on Tuesday January 02, 2018 @05:06PM (#16056222)
I already provided such an example: the disconnection. The key aspect of that theoretical example was that it would be "too costly to repeat exhaustively in integration tests". You simply wished away that trait, avoiding the issue.
Re:Coming Soon: Machine-Checked Proofs in Everyday Development by crdoconnor on Thursday January 11, 2018 @06:57PM (#16129218)
No, I can test that.
Re:Coming Soon: Machine-Checked Proofs in Everyday Development by zxcmx on Sunday December 31, 2017 @07:56AM (#16040418)
I think a part of the difficulty is in the language of coverage.

"100% test coverage" usually means statement coverage due to the nature of popular tooling.

If you deeply care about correctness you are usually more interested in path coverage which is more tied to things like heap shape or state space... (which depending on your system design might encompass stuff in your database). Also yeah, technically the exceptions which can be thrown at each point in your software if you work in a language with those.

Unfortunately there's not much tooling for that so it's not visible in the same way as the more naive metrics.

Anway my point is that you can waste time pushing for "100% statement coverage" measurable by naive tools but still not exercise important paths, which I think is your point.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by z3t4 on Sunday December 31, 2017 @02:44PM (#16042402)
I want to clarify that the whole point of unit tests is to have 100% coverage, that all code paths are exercised by the tests. If you do unit testing and have code that is not covered it's either a bug or dead code that should be removed.
Re:Coming Soon: Machine-Checked Proofs in Everyday Development by hmottestad on Sunday December 31, 2017 @06:05AM (#16040152)
Integration tests are much more brittle in my experience. A simple test for doing frontend/backend integrations tests with web driver can easily break due to race conditions or changes in other parts of the system. Such tests are great for detecting that something is wrong, but it can be very hard to figure out what's wrong and where things went wrong.

Had a lot of "fun" rewriting a bunch of integration tests for angular, because the framework used a lot of Promises which caused race conditions. Those tests were much harder to write than unit tests and were much easier to mess up.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by jondubois on Sunday December 31, 2017 @06:45AM (#16040241)
They're not necessarily brittle but I know what you mean. When the integration tests reach across multiple services then things tend to get brittle but this can be fixed by stubbing out the external services.

Stubs are not just useful for unit tests. Service stubs are often much easier to write than module stubs because services often communicate via string based protocols which are easy to simulate.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by crdoconnor on Sunday December 31, 2017 @10:39AM (#16040942)
>Integration tests are much more brittle in my experience.

In my experience brittleness in tests is caused by bugs in tests or bugs in code.

Most of these things can be fixed, e.g.

* Add an order by to select statements that otherwise returns data in an indeterminate order.

* Fix the interpreter/database version you are using to run your code rather than relying upon the version the package manager has installed.

* Stub out code that gets date/time/random numbers.

* Stubbing out external APIs which go down on Tuesdays.

* Fixing the bugs in your code.

* Abandoning the code you rely upon which is buggy (I'm just sayin', angular might not be the ideal framework).

Avoiding writing these tests because you know that "they'll be too buggy" doesn't exactly send out a great signal.

>Such tests are great for detecting that something is wrong, but it can be very hard to figure out what's wrong and where things went wrong.

This is alleviated by integrating debugging tools into your test environments.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by pmontra on Sunday December 31, 2017 @08:20AM (#16040480)
Recently I found a get_info(doc) which was hard to understand. It should have been get_xrsf_token(web_page) which would have been self explanatory.

I mostly develop backends of web apps. In my experience not every web app has some code that needs to be explained. Picking the correct names is usually enough.

That said, unit tests really do what you write: provide examples, use cases and guidance.

Re:Coming Soon: Machine-Checked Proofs in Everyday Development by z3t4 on Sunday December 31, 2017 @02:31PM (#16042299)
I find the best approach is the keep the code base small enough so you can keep everything in your head. Knowing that if you change something you also have to update this and that , and make sure feature x, y, z still works (which could be done with automated tests).