Code duplication in tests is not bad.

Change my mind.

@musicmatze I think it makes sense to try to deduplicate code in tests when doing test driven development, since if goals change it's less likely you will have to update many tests.
@musicmatze I suppose the same is true without TDD, but I feel like it's more important when doing TDD.

@robby so, in your opinion, I should try to implement a framework for filling my special datastructures with testing-data before testing algorithms that work on them?

What if the framework for filling them is buggy? Do I need tests for my testing code?

> Do I need tests for my testing code?

No, when you are doing "the right way" (meaning "red-green-refactor") you don't need it.

See this post (and thread). @vertigo explains it beautifully:


@janriemer @musicmatze @vertigo so ideally with TDD things go like:

1. Create idea
2. Write tests to check that code matches idea
3. All tests fail because code hasn't been written
4. Write code until all tests pass
5. Fin

What if the idea from 1 changes between 2 and 4? Maybe customer requirements change, maybe adjacent processes change, etc. In this case, we will have to make some changes to the tests probably. Deduplication can make changes to the tests faster to do, and less error prone.

@robby @vertigo @musicmatze Yes, maintainability can be harder when there is duplicate test code, but here is the thing:

1. Tests must be understood easily - if you are hiding away the inputs and the "acting" of the SUT (system under test) behind some abstraction to reduce duplication, you make it maybe easier to maintain, but much harder to understand what the test should verify.



@robby @vertigo @musicmatze

2. If you have a lot of "acting" to do on your SUT and, as a consequence, a lot of duplication of method call sequences between your tests, this is actually an indication that you should build an abstraction for it _in your production code_.

is not only - it is also .


· · Web · 0 · 0 · 1
Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!