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));
}
Related videos on Youtube
Author by
J2ghz
Updated on September 16, 2022Comments
-
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 almost 8 years@Igor: That change only makes
SomeTest()
superficially awaitable.Assert.All()
still behaves the same way.
-
-
Stephen Cleary almost 8 yearsGood approach, but
var results = await Task.WhenAll(tasks)
gives you a nice result array to work with. -
Eli Arbel almost 8 yearsThanks @StephenCleary. Edited.