xUnit Assert.All() async

17,318

There's no built-in async All. You can use Task.WhenAll:

[Fact]
public async Task SomeTest()
{
    var itemList = ...;
    var results = await Task.WhenAll(itemList.Select(async item =>
    {
        var i = await Something(item);
        return i;
    }));
    Assert.All(results, result => Assert.Equal(1, result));
}
Share:
17,318

Related videos on Youtube

J2ghz
Author by

J2ghz

Updated on September 16, 2022

Comments

  • J2ghz
    J2ghz over 1 year

    I have this example test using xUnit:

        [Fact]
        public void SomeTest()
        {
            Assert.All(itemList, async item=>
                    {
                        var i = await Something(item);
                        Assert.Equal(item,i);
                    });
        }
    

    Is there a good solution to make the whole test async/awaitable?

    • B Bulfin
      B Bulfin almost 8 years
      @Igor: That change only makes SomeTest() superficially awaitable. Assert.All() still behaves the same way.
  • Stephen Cleary
    Stephen Cleary almost 8 years
    Good approach, but var results = await Task.WhenAll(tasks) gives you a nice result array to work with.
  • Eli Arbel
    Eli Arbel almost 8 years
    Thanks @StephenCleary. Edited.