Based on this question about the correct usage of nUnit's TestCaseAttribute
, I was wondering whether to specify the test case directly on the implementation or create test methods (as when using explicit Assert.AreEqual
etc.).
In the following, I specified examples for both approaches and also listed potential arguments for and against both cases that I could think of.
Specifying the test on the implementation
public class Math { [TestCase(1, 2, ExpectedResult = 3)] public int Add(int a, int b) { return a + b; } }
Pros:
- Less code to write
TestCase
kind of works as documentation
Cons:
- having to reference
nunit.framework.dll
in the implementation project
Creating a test method
public class Math { public int Add(int a, int b) { return a + b; } } public class MathTest { [TestCase(1, 2, ExpectedResult = 3)] public int AddTest(int a, int b) { return new Math().Test(a, b); } }
Pros:
No need to reference
nunit.framework.dll
in the implementation project, only in the test projectTests are clearly separated from the code they test
Cons:
Code duplication - the test method has exactly the same signature as the actual implementation etc.
more code to write
test doesn't work as a kind of documentation anymore
Are there any more arguments for or against either of the mentioned approaches?
What approach should I follow? (as in what approach is generally used, which is considered "Better Practice"?)
TestCase
directly in the production code, but was confused when I saw the question on SO. Your comments mention very good points, maybe consider adding this as an answer?