Your code is correct!
To clarify this answer is 5 years, 2 months after the initial question. Back then having async [TestInitialize]
might have been a compile error, but these days it isn't.
It's possible to have async [TestInitialize]
, async [ClassInitialize]
and async [TestMethod]
just simply use await.
Using async and await properly is probably the cleanest way to do it. I have something like the following in my code where I need to get our category structure in order to be able to test if the my classes work well with the category structure that we have.
private Category rootCategory;
[TestInitialize]
public async Task TestInitialize()
{
var loader = new CategoryLoader();
rootCategory = await loader.GetAllCategoriesAsync();
}
[TestInitialize]
runs before every [TestMethod]
, so depending on what I'm trying to test here, it might be better to load only once and then make all the assertions, to not pay for the loading time multiple times. But you need to be careful so that the tests don't affect each other to get consistent and correct results.
Just a note that this is not a unit test anymore since I'm testing integration with external service.