Reputation: 177
I have such code:
public class Foo
{
...
public override bool EqualsTo(object obj)
{
...
}
public static bool operator==(Foo left, Foo right)
{
...
}
public static bool operator!=(Foo left, Foo right)
{
...
}
}
The main purpose of this code sample is to show that class can have multiple ways of doing same things, such as checking objects' equality. So method EqualsTo
and operators ==
and !=
are used only for simplicity. It can be any different set of methods. The main point is that there are multiple ways to get the same behavior.
I have two variants of unit tests:
Variant 1:
[TestClass]
public class FooTest
{
[TestMethod]
public void ShouldCheckEqualityWhenObjectsEqual()
{
var foo1 = new Foo(1);
var foo2 = new Foo(1);
Assert.IsTrue(foo1.EqualsTo(foo2));
Assert.IsTrue(foo2.EqualsTo(foo1));
Assert.IsTrue(foo1 == foo2);
Assert.IsTrue(foo2 == foo1);
Assert.IsFalse(foo1 != foo2);
Assert.IsFalse(foo2 != foo1);
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsNotEqual()
{
var foo1 = new Foo(1);
var foo2 = new Foo(2);
Assert.IsFalse(foo1.EqualsTo(foo2));
Assert.IsFalse(foo2.EqualsTo(foo1));
Assert.IsTrue(foo1 == foo2);
Assert.IsTrue(foo2 == foo1);
Assert.IsTrue(foo1 != foo2);
Assert.IsTrue(foo2 != foo1);
}
}
Variant 2:
[TestClass]
public class FooTest
{
[TestMethod]
public void ShouldCheckEqualityWhenObjectsEqualUsingMethod()
{
var foo1 = new Foo(1);
var foo2 = new Foo(1);
Assert.IsTrue(foo1.EqualsTo(foo2));
Assert.IsTrue(foo2.EqualsTo(foo1));
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsEqualUsingEqualityOperator()
{
var foo1 = new Foo(1);
var foo2 = new Foo(1);
Assert.IsTrue(foo1 == foo2);
Assert.IsTrue(foo2 == foo1);
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsEqualUsingInequalityOperator()
{
var foo1 = new Foo(1);
var foo2 = new Foo(1);
Assert.IsFalse(foo1 != foo2);
Assert.IsFalse(foo2 != foo1);
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsNotEqualUsingMethod()
{
var foo1 = new Foo(1);
var foo2 = new Foo(2);
Assert.IsFalse(foo1.EqualsTo(foo2));
Assert.IsFalse(foo2.EqualsTo(foo1));
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsNotEqualUsingEqualityOperator()
{
var foo1 = new Foo(1);
var foo2 = new Foo(2);
Assert.IsTrue(foo1 == foo2);
Assert.IsTrue(foo2 == foo1);
}
[TestMethod]
public void ShouldCheckEqualityWhenObjectsNotEqualUsingInequalityOperator()
{
var foo1 = new Foo(1);
var foo2 = new Foo(2);
Assert.IsTrue(foo1 != foo2);
Assert.IsTrue(foo2 != foo1);
}
}
Variant 1 is shorter and easy to understand as for me, but Variant 2 is much more precise in what is done and how it's done and have more boilerplate code, such as data initialization and set up. I'd like to know which of these two variants is more preferred. Or maybe there are more better variants than those ones.
Equality tests used only for simplicity, and data set up and initialization can be longer.
Another example:
public class Bar
{
...
public int this[short index] { ... }
public int this[int index] { ... }
public int this[long index] { ... }
}
Should I create separate test for each indexer or can test them all in one test for the same object, assuming that they should behave identically.
Upvotes: 1
Views: 30
Reputation: 391336
Yes, you should, and I'm also going to vote for closing your question as asking for opinions.
Here's why.
A test should only test 1 thing.
1 thing.
If you want to test that the Equals
method works, that's 1 test.
If you want to test that the ==
operator works, that's another test.
Some people might think of these two as the same, in particular if one of the methods calls the other.
As such, I vote to close this question as a matter of asking for an opinion.
But in my opinion, you should only test 1 thing per test. If you have multiple methods implementing the same concept, each method warrants its own test.
In the end, you want a failed test to tell you specifically what went wrong. If that "specifically" can be any number of methods, your test is too broad.
But that's my opinion.
Upvotes: 1