views:

469

answers:

8

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?

+25  A: 

Try taking the 3rd approach that you haven't mentioned, that of refactoring your code so that you can share one implementation of the test between all 10 tests.

The jist is, duplicating code is almost always the wrong thing to do. In this example you could refactor the checking code into a method called, for example IsTetrisPieceUnableToMoveLeftBecauseOfAPieceOrAWall. I always go for very descriptive method names like that when writing a bit of "shared" functionality for a unit test as it makes it extraordinarily clear just what's being done / tested.

Rob
`Is_Tetris_piece_unable_to_move_left_because_of_a_piece_or_a_wall`
Inverse
Thank god for auto-completion.
Mark
@Inverse, sure, if underscores do it for you =)
Rob
+12  A: 

Test code is like any other code and should be maintained and refactored.

This means that if you have shared logic, extract it to its own function.

Some unit test libraries such as the xUnit family have specific test fixture, setup and teardown attributes for such shared code.

See this related question - "Why is copy paste of code dangerous?".

Oded
+7  A: 

There's nothing wrong with copy-pasting, and it's a good place to start. Indeed, it's better than from scratch, as if you've got working code (whether tests or otherwise), then copy-paste is more reliable than from scratch, as well as quicker.

However, that's only step 1. Step 2 is refactoring for commonality, and step 1 is only to help you see that commonality. If you can already see it clearly without copying (sometimes it's easier to copy first and then examine, sometimes it isn't, and it depends on the person doing it) then skip step 1.

Jon Hanna
+1  A: 

If you are repeating code, then you must refactor. Your situation is a common problem and is solved using 'Parameteric Testing'. Parameteric testing when supported by the test harness allows for passing multiple sets of input values as parameters. You may also want to look up Fuzz testing, I have found that it useful in situations such as this.

kashif
+11  A: 

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/maintenable. 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: I you really don't agree, please leave a comment.

ewernli
+1, I find ur argument compelling. Maybe we could say if ur code has x level complexity, then it needs to be tested. This applies to testing code too. So if a proper refactoring raises the testing code complexity above x, then either (1) refactor and add another testing level; or (2) Keep It Simple Stupid.
emory
@ewernli I agree with your point that test code should be simple. But, then at the same time it should be maintainable and duplicating code isn't what you would like to do
P.K
@P.K Actually, code with duplication is most of the time easier to understand, because it's complexity is low (less abstraction). So maintenance isn't hard, but might be a bit repetitive. In software engineering, most of the time is spend *understanding* what needs to be done, not *doing* it. If maintaining tests is repetitive and boring, that's fine, that the way it should be. The problem with duplication is the fear to not update the logic in all places. You can not take the risk for production code to leave an incoherence unnoticed, but in test code, the test will fail until you change it.
ewernli
+1 for making a really good case FOR code duplication.
nes1983
+1  A: 

Remember that your tests are pushing against your code. if you find that your tests look duplicated except for something like left/right, then maybe there issome underlying code that left and right is duplicating. So you might want to see if you can refactor your code to use either left or right and send a left or right flag to it.

Gutzofter
A: 

The xunitpatterns.org web site has this description online:

and for further reading it also links to the article

By: Arie van Deursen, Leon Moonen, Alex van den Bergh, Gerard Kok

mjustin
A: 

I agreed @Rob. The code needs refactoring. But if you don't want to do refactor the code at this point of time then you can go and have parametrized tests. The same test run for different parameters. See TestCase and TestCaseSource attributes in nunit.

Refer http://nunit.org/index.php?p=parameterizedTests&r=2.5

P.K