Home > Net >  Assert collection of variables in test
Assert collection of variables in test

Time:10-07

I'm trying to write a C# test method that tests tax calculations for each employee. My _records is a List<PayRecord>.

Is it possible to put all of my variables into a list and check each value against actualTax list?

public void TestTax()
{
    PayRecord empOne = _records[0];
    double actualTaxOne = empOne.Gross;
    PayRecord empTwo = _records[1];
    double actualTaxTwo = empTwo.Gross;
    PayRecord empThree = _records[2];
    double actualTaxThree = empThree.Gross;
    PayRecord empFour = _records[3];
    double actualTaxFour = empFour.Gross;
    PayRecord empFive = _records[4];
    double actualTaxFive = empFive.Gross;

    double expectedTax_empOne = 652;
    double expectedTax_empTwo = 418;
    double expectedTax_empThree = 2202;
    double expectedTax_empFour = 1104;
    double expectedTax_empFive = 1797.45;

    Assert.AreEqual(expectedTax_empOne, actualTaxOne);
    Assert.AreEqual(expectedTax_empTwo, actualTaxTwo);
    Assert.AreEqual(expectedTax_empThree, actualTaxThree);
    Assert.AreEqual(expectedTax_empFour, actualTaxFour);
    Assert.AreEqual(expectedTax_empFive, actualTaxFive);
}

CodePudding user response:

You mean like...

    public void TestTax()
    {
        var expect = new double[] { 652, 418, 2202, 1104, 1797.45 };

        for(int x = 0; x < expect.Length; x  ){
            Assert.AreEqual(expect[x], _records[x].Gross);
        }
    }

?

CodePudding user response:

Shorter code, especially the shortest code possible, is not always better. Remember you write the code for future you or your coworkers; the machine executing it doesn't care how short it is (unless you want to squeeze out every last bit of performance, and then still).

You want your assertions to be explicit and clear, so that future you still understands what's going on there and why. Perhaps even add a comment for each one why you expect that value.

It's unclear what your test is actually doing or why you need to test the same calculation for five different employees in one tests, but the code you show can be reduced to:

// These employees are in tax bracket 1
Assert.AreEqual(652, _records[0].Gross);
Assert.AreEqual(418, _records[1].Gross);

// These two are in the middle
Assert.AreEqual(1104, _records[3].Gross);
Assert.AreEqual(1797.45, _records[4].Gross);

// Look at mister big earner here
Assert.AreEqual(2202, _records[2].Gross);

There's no need for all these intermediate variables, and it's no problem to have actual values in the assertions.

CodePudding user response:

Using Linq and NUnit ist could be written like this:

public void TestTax()
{
    // NUnit Constraint Assertion Model
    Assert.That(_records.Select(x => x.Gross), Is.EqualTo(new double[] { 652, 418, 2202, 1104, 1797.45 }));

    // NUnit Classic Assertion Model
    Assert.AreEqual(_records.Select(x => x.Gross), new double[] { 652, 418, 2202, 1104, 1797.45 });
}

For failed assertions NUnit provides a detailed message like:

NUnit.Framework.AssertionException:   Expected is <System.Double[5]>, actual is <System.Linq.Enumerable WhereSelectListIterator`2[SimControl.Samples.CSharp.ClassLibrary.Tests.SampleClassTests PayRecord,System.Double]>
  Values differ at index [1]
  Expected: 418.0d
  But was:  1.0d
  • Related