xUnit Assert.All() async
Asked Answered
I

2

23

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?

Ina answered 26/7, 2016 at 17:32 Comment(2)
Change your test method signature to public async Task SomeTest()Boccherini
@Igor: That change only makes SomeTest() superficially awaitable. Assert.All() still behaves the same way.Mitchellmitchem
S
3

As for 2023, accepted answer is no longer true, xunit team added Assert.AllAsync: https://github.com/xunit/xunit/discussions/2498

Stile answered 16/1, 2023 at 14:29 Comment(1)
Applies to XUnit 2.4.2+Ina
A
33

In xUnit 2.4.2 and above, use Assert.AllAsync. Otherwise, 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));
}
Anticatalyst answered 26/7, 2016 at 18:31 Comment(0)
S
3

As for 2023, accepted answer is no longer true, xunit team added Assert.AllAsync: https://github.com/xunit/xunit/discussions/2498

Stile answered 16/1, 2023 at 14:29 Comment(1)
Applies to XUnit 2.4.2+Ina

© 2022 - 2024 — McMap. All rights reserved.