Saturday, 15 February 2014

.net - C# NUnit not running tests asynchronously -



.net - C# NUnit not running tests asynchronously -

i have written next test trying create utilize of new async/await keywords available in .net 4.5:

[test] public async void defaultfilesvalid() { logtest(log); var librarytasks = new list<task<string>>(); foreach (var def in librariesmodel.librarydefinitions) { iviewmodel viewmodel = (iviewmodel)reflectionutility.createnew(def.modeltype); var libraries = def.libraryfiles.where(f => def.isdefaultfile(f)).toarray(); foreach(var library in libraries) { var librarytask = loadembeddedlibraryasync(viewmodel, library); librarytasks.add(librarytask); } } // wait results var libraryerrors = await task.whenall(librarytasks); var errors = libraryerrors.selectmany(m => m); assert.true(!errors.any(), string.join(environment.newline, errors)); } private async task<string> loadembeddedlibraryasync(iviewmodel viewmodel, string library) { homecoming await task.factory.startnew<string>(() => { var librarydef = viewmodel.library; var librarymessage = string.format("the default library '{0}' of type '{1}' ", library, librarydef.libraryname); seek { // open library viewmodel.open(library, library: true); // check hash expected var filehash = librarydef.getdefaultfilehash(library); var hash = viewmodel.source.getstatewisehashcode().tostring(); if (filehash != hash) homecoming librarymessage + string.format("has been modified, model hash '{0}' '{1}' expected.", hash, filehash); } grab (exception ex) { homecoming string.format("failed open: {0}", exceptionutility.joinexceptionmessages(ex)); } homecoming null; }); }

however code executing synchronously, can please explain doing wrong?

thanks, alex.

change unit test async void async task. nunit supports async void unit tests v2.6.2 onward v2, , supported them in v2.9.6 v3 (support in v3 has been removed in v2.9.7). async void tests require nunit provide special context, changes behavior of await (and perchance startnew, though haven't tested that).

as side note:

use task.run instead of task.factory.startnew. have blog post describing why. use task.run call methods, not implement (fake-)asynchronous methods. have blog post describing why.

c# .net nunit .net-4.5

No comments:

Post a Comment