Setting up Async UnitTests in C #

I need to have a registered user for each of my unit tests, which forces me to make an asynchronous call (login) in my SetUp test.

I can’t find a way to make this work, I either get null pointer exceptions or invalid signatures for installation.

 public async void SetUp() {} 

This makes my test fail with my objects, probably because I have not logged in.

 public async Task SetUp() {} 

Makes all my tests ignored because the installation has an invalid signature.

And I would not want to copy my X strings in each test, since they are all the same and ... what the setup is for.

What am I missing? it looks like a trivial problem.

That's what I have now, for the sake of showing something

 CreateTicketViewModel _viewModel; [SetUp()] public async void SetUp() //I have tried using Task instead of void { IUserService userService = Dependency.Instance.Resolve<IUserService>(); await userService.LoginAsync(this.UserName, this.Password); _viewModel = Dependency.Instance.Resolve<CreateTicketViewModel>(); } [TearDown()] public void TearDown() { _viewModel = null; // I have tried removing this } [Test()] public void Initialization() { // If I put what in SetUp here and add "async" before void, // it works just fine Assert.IsNotNull(_viewModel); Assert.IsNotNull(_viewModel.Ticket); } 
+5
source share
3 answers

Depending on the unit test structure you are using async , the installation may not be properly processed by the framework.

in the case of NUnits, I think it does not yet support the async Setup configuration methods.

So what you need to do in your installer is just to wait synchronously to log in:

userService.LoginAsync(this.UserName, this.Password).Wait() ;

EDIT: looks like an open question https://github.com/nunit/nunit/issues/60

This also applies to MSTests.

+4
source

Could you just make your SetUp method non-asynchronous and write this?

 new Task(() => userService.LoginAsync(this.UserName, this.Password)).RunSynchronously() 
+2
source

Asynchronous installations are not supported, but asynchronous testing methods are supported. You can make your testing methods asynchronous, rather than a tuning method.

 [TestFixture] public class AsyncSetupTest { private Task<CreateTicketViewModel> viewModelTask; [SetUp()] public void SetUp() { viewModelTask = Task.Run(async () => { IUserService userService = Dependency.Instance.Resolve<IUserService>(); await userService.LoginAsync(this.UserName, this.Password); return Dependency.Instance.Resolve<CreateTicketViewModel>(); }); } [Test()] public async Task Initialization() { CreateTicketViewModel viewModel = await viewModelTask; Assert.IsNotNull(viewModel); Assert.IsNotNull(viewModel.Ticket); } } 

The idea, instead of doing all the setup work in the Setup method, we create a Task that represents the completion of the installation and expects it in the Test method.

Thus, you do not repeat the entire installation logic. But just extracting ViewModel from Task inside all testing methods.

+2
source

Source: https://habr.com/ru/post/1234128/


All Articles