简体   繁体   中英

Can I have an assert inside a loop and get a result for each of the asserts, not just one at the end?

So I have a table of values that I'd like to process one by one and assert the results. Rather than have multiple asserts I have the Assert inside a loop. This works and it iterates each result in turn, but only provides me with one result. If there are ten items in the table I'd like ten test results - is this possible?

[Test]
public void TestOne()
{
    var test = new SemParserLibrary.SemSummaryInfo();
    double[] testValues = new double[] {0, 0.0, 0.1, 1.1, 10, 010, 150, 299, 299.9, 300, 300.0};

    foreach (int i in testValues)
    {
        test.Rate.LogRate = i;
        var testFile = new ParserLibrary.SummaryInfo(test);
        var parsedRecord = ParserLibrary.FileManager.ParseRecord(testFile);
        Assert.AreEqual(parsedRecord.Summary.Data.Rate.LogRate, i, "The de-serialised Rate {0} does not match the input value of {1}", parsedRecord.Summary.Data.Rate.LogRate, i);
    }
}

As noted by @Peter, you can instead use a data-driven test, using TestCaseAttribute or TestCaseSourceAttribute . That's what I would prefer in your example.

However, just to answer the question as asked, yes it is possible. You would do that like this...

[Test]
public void TestOne()
{
    var test = new SemParserLibrary.SemSummaryInfo();
    double[] testValues = new double[] {0, 0.0, 0.1, 1.1, 10, 010, 150, 299, 299.9, 300, 300.0};

    Assert.Multiple(() =>
    {
        foreach (int i in testValues)
        {
            test.Rate.LogRate = i;
            var testFile = new ParserLibrary.SummaryInfo(test);
            var parsedRecord = ParserLibrary.FileManager.ParseRecord(testFile);
            Assert.AreEqual(parsedRecord.Summary.Data.Rate.LogRate, i, "The de-serialised Rate {0} does not match the input value of {1}", parsedRecord.Summary.Data.Rate.LogRate, i);
        }
    });
}

Given that my comment seems to answer the question, I will post it as an answer.

Data driven tests allow you to parameterize unit tests with test data. The simplest way to use that would be with the [TestCase] attribute:

[TestCase(0)]
[TestCase(0.0)]
[TestCase(0.1)]  // etc.
public void TestOne(int i)
{
    var test = new SemParserLibrary.SemSummaryInfo();
    test.Rate.LogRate = i;
    var testFile = new ParserLibrary.SummaryInfo(test);
    var parsedRecord = ParserLibrary.FileManager.ParseRecord(testFile);
    Assert.AreEqual(parsedRecord.Summary.Data.Rate.LogRate, i, "The de-serialised Rate {0} does not match the input value of {1}", parsedRecord.Summary.Data.Rate.LogRate, i);
}

This can get tedious if you have a lot of test data. In that case you can use the [TestCaseSource] attribute which allows you to get the test data from a static property. In the code for the property you could either hardcode that data or get it from a database or file or any other data source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM