Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to copy & paste unit-tests when the logic is basically the same?

Tags:

I currently have like 10 tests that test whenever my Tetris piece doesn't move left if there is a piece in the path, or a wall. Now, I will have to test the same behaviour for the right movement.

Is it too bad if I just copy the 10 tests I already have for the left movement and make only the needed changes and do the same for the code itself too? Or should I go again and make each test from the beginning, even so if the logic is basically the same?

like image 218
devoured elysium Avatar asked Aug 08 '10 19:08

devoured elysium


People also ask

Is copying a good thing?

This is called learning by the experience of doing. Copying helps you better understand how something is made all while teaching the learner technique and developing muscle memory. When I was first learning watercolor I did this! But there's a time to copy and then there's a definite time to not copy.

Is it OK to copy a drawing?

But you guys, there's nothing wrong with copying, as long as you follow some best practices. And in fact there are many reasons you should copy. Almost every artist's journey begins with imitating other artists. Over time, the experience leads them to explore and discover their own style and voice.

Why is copying not good?

When they copy from someone, students steal someone else's words and work and pass it off as their own. Cheating and copying in tests is not the right thing to do. It's not right by the students who actually studied or by the teacher. Those who copy and cheat are just masking their lack of knowledge.

Is it OK to copy someone elses art?

Forms of Art Plagiarism Plagiarists copy sketches, paintings, photos, and even sculptures. When you copy someone else's art without consent or credit—you are stealing.


1 Answers

I have a somewhat controversial position on this one. While code duplication must be avoided as much as possible in production code, this is not so bad for test code. Production and test code differ in nature and intent:

  • Production code can afford some complexity so as to be understandable/maintainable. You want the code to be at the right abstraction level, and the design to be consistent. This is ok because you have tests for it and you can make sure it works. Code duplication in production code wouldn't be a problem if you had really a 100% code coverage at the logical level. This is really hard to achieve, so the rule is: avoid duplication and maximize code coverage.

  • Test code on the other hand must be as simple as possible. You must make sure that test code actually tests what it should. If tests are complicated, you might end up with bug in the tests or the wrong tests -- and you don't have tests for the tests, so the rule is: keep it simple. If test code is duplicated, this is not so a big problem when it something changes. If the change is applied only in one test, the other will fail until you fix it.

The main point I want to make is that production and test code have a different nature. Then it's always a matter of common sense, and I'm not saying you should not factor test code, etc. If you can factor something in test code and you're sure it's ok, then do it. But for test code, I would favor simplicity over elegance, while for production code, I would favor elegance over simplicity. The optimum is of course to have a simple, elegant solution :)

PS: If you really don't agree, please leave a comment.

like image 108
ewernli Avatar answered Sep 19 '22 13:09

ewernli