News for hackers. Stuff that matters.
Re:Goodbye, Clean Code by babarock on Saturday January 11, 2020 @06:24PM (#22023155)
One of the areas where I really like "incidental duplication" is in tests.

Tests can sometimes be very repetitive and identical, and it's tempting to want to refactor it in some clever way. That's almost never good.

On top of the reasons laid out in parent comment, tests also function as unofficial documentation. I like having everything explicit in there, it makes them easier to read and understand.

Re:Goodbye, Clean Code by ummonk on Saturday January 11, 2020 @09:24PM (#22024027)
If you try to abstract away tests, you often just end up re-implementing the same abstractions used in the actual code, and you can end up not catching unfounded assumptions that your abstraction is making in both the tests and the code. There is a scope for having test helpers / utils to make tests easier to write, but you should be minimalist with these.
Re:Goodbye, Clean Code by 3pt14159 on Saturday January 11, 2020 @10:36PM (#22024300)
I agree, but some test helpers are reasonable. It's about balance and readability of the tests in question. Otherwise integration tests end up being a hundred lines of code.
Re:Goodbye, Clean Code by ajuc on Sunday January 12, 2020 @06:58PM (#22030668)
The problem with abstractions in testing is - you should test it :)
Re:Goodbye, Clean Code by rbanffy on Sunday January 12, 2020 @01:24AM (#22024815)
Tests should also be treated as a form of documentation. A test should reflect the way you'd use the code in real life as closely as feasible.
Re:Goodbye, Clean Code by therealdrag0 on Wednesday January 15, 2020 @12:30PM (#22056191)
I'm not sure I agree. I like to move all setup code to helper methods so that my tests are just a few lines

  // Given <setup>
  
  // When <thing happens>

  // Expect <thing> to be <such>
This allows the reader to easily see which workflows are actually tested. If the reader is interested in implementations the utility code is one click away and usually only needs to be looked at once to be completely understood. The test bodies themselves however have many flavors for many work-flows so getting rid of the repetition is critical to highlight the specific nature of individual tests.
Re:Goodbye, Clean Code by philwelch on Saturday January 11, 2020 @09:58PM (#22024169)
I do think a lot of test frameworks would do well to support data-driven testing a la Spock: http://spockframework.org/spock/docs/1.0/data_driven_testing...
Re:Goodbye, Clean Code by lonelappde on Saturday January 11, 2020 @08:44PM (#22023858)
Terrible acronym for a good idea: DAMP

stackoverflow.com/questions/6453235/what-does-damp-not-dry-mean-when-talking-about-unit-tests

Re:Goodbye, Clean Code by blazespin on Saturday January 11, 2020 @07:00PM (#22023351)
Tests rarely have bugs, I find, so generally dry isn’t critical. Also, dry is for security (see below)
Re:Goodbye, Clean Code by dboreham on Saturday January 11, 2020 @07:21PM (#22023445)
Tests frequently have bugs, especially bugs that result in the test passing when it should fail.
Re:Goodbye, Clean Code by chubot on Saturday January 11, 2020 @08:16PM (#22023728)
That’s why I make the test fail before writing the code. If the code is already written, then I break it in the minimal way to test the test, and then fix it.
Re:Goodbye, Clean Code by sojournerc on Saturday January 11, 2020 @08:32PM (#22023804)
IME things never fail in the way you expect them to... You can build a fortification where you think you are weak only to find the enemy is already in the castle.
Re:Goodbye, Clean Code by serpix on Sunday January 12, 2020 @05:08AM (#22025488)
A green test does not equal bug free code. There may be a misimplementation / misunderstanding of the spec or your code passes beautifully under right conditions, with just this data setup.
Re:Goodbye, Clean Code by lonelappde on Saturday January 11, 2020 @08:48PM (#22023876)
That's a test for your test, so why only run it once transiently instead of running every time? "Mutant" testing helps with this. It's basically fuzzing your test code to make sure that every line is meaningful.
Re:Goodbye, Clean Code by chubot on Sunday January 12, 2020 @12:17AM (#22024636)
I can see how that would be useful, but I also think it's a matter of priorities.

I'm basically saying I rarely have bugs in my tests because I verify them first. In fact I can't think of a single bug in my tests over the last 4 years (or even 10 years), but I can think of dozens of bugs in my code.

For example here are some pretty exhaustive tests I've written for shell, which have exposed dozens of bugs in bash and other shells (and my own shell Oil):

https://www.oilshell.org/release/0.7.pre11/test/spec.wwz/osh...

I would rather spend time using the tests to improve the code than improving the tests themselves. But I don't doubt that technique could be useful for some projects (likely very old and mature ones)

Re:Goodbye, Clean Code by scaryclam on Monday January 13, 2020 @04:12PM (#22038641)
ITYM "I rarely have bugs in my tests that I'm aware of". The amount of tests I've seen that look like they are working properly, have been "verified" and are buggy is huge. Usually we find they were buggy because someone moves some other tests around or changes functionality that should have broken the tests, but didn't.

Please, don't ever assume that your tests are beyond reproach just because you verified them. Tests are software as well and are as prone to bugs as anything.

Re:Goodbye, Clean Code by pvorb on Sunday January 12, 2020 @03:40AM (#22025218)
And how do you do this in practice? I am struggling to think of a good way to keep the production code that fails the test and the production code that doesn't fail the test together. I might have my test check out an old version of the production code, compile it and test against that. But that is hard to get right.
Re:Goodbye, Clean Code by buzzkillington on Saturday January 11, 2020 @07:17PM (#22023433)
Tests always have bugs, it's just that you don't know about the edge cases yet.
Re:Goodbye, Clean Code by smnplk on Saturday January 11, 2020 @07:16PM (#22023427)
As a preventative measure, I write some tests for my tests. Also in TDD style of course. And on a very rare occasion, I have to write a test for those tests as well.
Re:Goodbye, Clean Code by natalyarostova on Sunday January 12, 2020 @01:36AM (#22024863)
It’s time for TTDD. Start by writing tests for your tests :)
Re:Goodbye, Clean Code by afarrell on Sunday January 12, 2020 @06:12AM (#22025708)
I do actually do that. I'll write some buggy code in order to learn how to test for it.

TDD for me is primarily a way to guide myself toward accomplishing a goal. So I sometimes write way more tests for myself than the business needs. I will then delete the scaffolding tests before I tag my PR for review.

Re:Goodbye, Clean Code by lonelappde on Saturday January 11, 2020 @08:46PM (#22023871)
Tests have fewer bugs if you write them before the system under test, and if they don't have mocks, and if you have enough of them that anything you get wrong the first time will get noticed by the results of the many similar tests.
Re:Goodbye, Clean Code by gusmd on Sunday January 12, 2020 @01:16PM (#22027941)
> Tests have fewer bugs ... if they don't have mocks

100x this. I've repeatedly fail to convince my team members that mocks are unnecessary in most cases. I've reviewed code with mocks for classes like BigDecimals and built-in arrays. This is especially prevalent in Java teams/codebases.

Drives me insane.

Re:Goodbye, Clean Code by dragonwriter on Saturday January 11, 2020 @09:52PM (#22024146)
> Tests rarely have bugs, I find, so generally dry isn’t critical

DRY is important for tests, but the areas where it is have probably (if you aren't writing a testing framework or using a language that doesn't have one that covers your use case) largely covered by your testing framework already.

Re:Goodbye, Clean Code by jammygit on Saturday January 11, 2020 @10:59PM (#22024373)
Tests are there to catch issues. In a way, almost every bug that makes it to production is a test bug.

Not to mention the inconsistent tests that fail so randomly from timing issues that the team ignores them