Slava
Slava

Reputation: 6650

How to write this test case?

I need to write this test method that checks that methods AMethod1 and AMethod2 return same results with same boolean parameters (AMethod1 was refactored to AMethod2)

How to implement all possible combinations of parameters when calling these methods? Of course, I can just copy paste 9 different combinations, but maybe there is a more elegant way?

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void OriginalMethodAndOptimizedReturnSameResult()
    {
        // By supplying same set of parameters for both methods, 
        // the test should succeed if methods return same result

        var result1 = Program.AMethod1(false, false, false);
        var result2 = Program.AMethod2(false, false, false);

        Assert.AreEqual(result1, result2);
    }
}

And another question - how to fail this test if it runs longer than 10 seconds?

Upvotes: 1

Views: 194

Answers (2)

Chrille
Chrille

Reputation: 1453

Use the bit pattern of an iterated counter:

for(var i = 0; i < 8; i++) {

    var a = (i & 1) > 0;
    var b = (i & 2) > 0;
    var c = (i & 4) > 0;

    var result1 = Program.AMethod1(a, b, c);
    var result2 = Program.AMethod2(a, b, c);

    Assert.AreEqual(result1, result2);    
}

For the timeout mstest supports the timeout argument @ MSDN

Upvotes: 1

Jorge Y.
Jorge Y.

Reputation: 1133

In VS2017 this seems to work, not sure which VS version are you using. Still verbose, but better than writing the 8 combinations:

[DataTestMethod]
[DataRow(false, false, false)]
[DataRow(false, false, true)]
[DataRow(false, true, false)]
[DataRow(false, true, true)]
[DataRow(true, false, false)]
[DataRow(true, false, true)]
[DataRow(true, true, false)]
[DataRow(true, true, true)]
public void OriginalMethodAndOptimizedReturnSameResult(bool b1, bool b2, bool b3)
{
    // By supplying same set of parameters for both methods, 
    // the test should succeed if methods return same result

    var result1 = Program.AMethod1(b1, b2, b3);
    var result2 = Program.AMethod2(b1, b2, b3);

    Assert.AreEqual(result1, result2);
}

Upvotes: 0

Related Questions