1

Say we have a Point class.

public class Point {
    public double x;
    public double y;
    ...
    public isEqual(Point p) {
        // here we worry about epsilons
    }
}

Some TestProject computes, say, the median of a triangle and confirms (by comparing x and y coordinates) that the point returned is correct. But we need to introduce tolerances.

Now we're damned if we do, damned if we don't:

Scenario 1

C# conveniently overloads the Assert.AreEqual() to handle tolerances. We put the comparison in the TestProject. We use Assert.AreEqual() and all is good.

But if we write ten different tests in ten different projects, we have not encapsulated point comparison. It is scattered throughout, which is bad.

Scenario 2

We put the comparison along with the Point class. Now any time we need to compare two points we just send the two points to the Point class and we get the answer.

But we can no longer use Assert.AreEqual(). All Asserts must be located in a TestProject.

We have to use our own comparator.

The question then is: what is the C# way for doing Assert.AreEqual(), but returning a bool?

Vrokipal
  • 784
  • 5
  • 18
  • Can you share the code which you are trying to test and the test code? – Chetan Sep 26 '19 at 19:55
  • `Assert.AreEqual` is a unit testing facility but it sounds like you're talking about using it for more than just that. – madreflection Sep 26 '19 at 20:00
  • Could you have a shared library that contains some common Test code, including this point comparison? Then all ten of your test projects could reference that shared library and your Point class doesn't need any test code – devNull Sep 26 '19 at 20:08
  • Which test framework are you using? You should be able to do `Assert.IsTrue(p1.isEqual(p2))` at least or `Assert.AreEqual(p1, p2, new PointEqualityComparer(tolerance))`. – Lee Sep 26 '19 at 20:11
  • I could be wrong but it sounds like OP is using `Assert.AreEqual` in regular application code and this isn't a unit testing issue. The name `isEqual` is arbitrary. @Vrokipal: you should look at overriding `Equals` and implementing `IEquatable` for custom equality tests that can be used by the internals of the framework. – madreflection Sep 26 '19 at 20:18
  • 2
    Maybe I'm not understanding the question, but isn't the answer as simple as using `Assert.IsTrue(point1.isEqual(point2))`? – John Wu Sep 26 '19 at 20:24

0 Answers0