开发者

Why is this unit test so "slow"

I was wondering this. I've created a Complex class just for fun (to store complex numbers) and to get used to TDD.

And I've written a following test:

    [TestMethod]
    [TestCategory("COMPLEX_OPERATOR")]
    public void ComplexAdditionWorks()
    {
        var c1 = new Complex(1.5, 3.0);
        var c2 = new Complex(3.0, 2.5);

        var cOutcome = c1 + c2;

        var cExpected = new Complex(4.5, 5.5);

        Assert.AreEqual(cOutcome, cExpected);
    }

And this is the implementation:

    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
    }

However the runtime of this unit test is relatively slow, at 0.10sec. And it's a huge difference compared to the subtraction operator:

  public static Complex operator -(Complex c1, Complex c2)
    {
        return new Complex(c1.Real - c2.Real, c1.Imaginary - c2.Imaginary);
    }

With the comparable unit test:

    [TestMethod]
    [TestCategory("COMPLEX_OPERATOR")]
    public void ComplexSubstractionWorks()
    {
        var c1 = new Complex(3.0, 2.5);
        var c2 = new Complex(1.0, 1.5);

        var cOutcome = c1 - c2;

        var cExpected = new Complex(2.0, 1.0);

        Assert.AreEqual(cOutcome, cExpected);
    }

But this one runs extremely fast (unit test says 0.00 secs)

Why is the addition operator/unit test so slow here...开发者_如何学JAVA

By the way, I ran this unit test like 20 times to see if it wasn't just a hiccup or something.


Okay I have found out what it is.

It's just that public void ComplexAdditionWorks() is the first test that runs. I thought they would run in the order they were programmed, but it's alphabetically.

It's still strange that R# adds the initialisation time to the first test...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜