8 Jul 2011 00:26
Re: Duplicate constants
JeffGrigg <jeffreytoddgrigg <at> gmail.com>
2011-07-07 22:26:29 GMT
2011-07-07 22:26:29 GMT
--- "arnonaxelrod" <arnonaxelrod <at> ...> wrote:
> Here's another question I often have doubts about:
> ...
> var result = MyMethod();
> Assert.AreEqual("This is a constant string", result);
Don't worry abut it. There will always be some amount of duplication between the tests and the code.
The tests should be examples of results expected from the production code. The production code should use
algorithms, where appropriate. In corner cases, you end up with the same internal constants in both the
tests and the code under test. This is not a problem. Don't worry about it.
What's most important is that the tests should not use the same algorithm as the code under test.
If the code under test says...
return "Error: Table " + tableName + " exceeds the limit of " + limitBytes + " bytes."
then the test should not have similar string concatenation. Hard coding the expected values in the test is
better, as tests should (generally) be examples of expected behavior:
assertEquals("Error: Table ACCOUNT exceeds the limit of 32767 bytes.", makeTable("ACCOUNT", 32768))
Sometimes it makes sense to write "algorithm" code in a test to test the results of an algorithm under test.
This is OK -- if they are two different algorithms. And the test algorithm should be simpler and easier to
verify. It's OK if the test algorithm is substantially less efficient; you'll probably only use it on a
limited range of values.
There will also often be duplication within and between tests. Eliminate duplication here, but don't go
crazy with it: It's more important that tests be expressive, easy to read and easy to change. It's OK for
test code to contain some duplication within and between tests.
It is NOT OK for production code to contain duplication: Production code should contain each meaningful
value, each algorithm, each concept, once and only once.
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/testdrivendevelopment/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/testdrivendevelopment/join
(Yahoo! ID required)
<*> To change settings via email:
testdrivendevelopment-digest <at> yahoogroups.com
testdrivendevelopment-fullfeatured <at> yahoogroups.com
<*> To unsubscribe from this group, send an email to:
testdrivendevelopment-unsubscribe <at> yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
RSS Feed