You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using DataTestMethod and DataRow, sometimes only some of the tests will be asyncronous. It'd be nice if test methods that return ValueTask instead of Task or Void can be discovered.
This would be useful if the tests sometime run an async method to get data to pass to it or if the awaited methods in a test are all ValueTask
Here's some example code of when this might be useful :
Example Code
using Microsoft.VisualStudio.TestTools.UnitTesting;using System;using System.Diagnostics.CodeAnalysis;using System.IO;using System.Threading.Tasks;namespaceTestProject1{[TestClass]publicclassUnitTest1{publicstaticclassUploader{// Uploads data or returns false if data is missingpublicstaticboolUpload(MyFileObjectmfo){return!(mfo.Data ==null|| mfo.Data.Length ==0)&& mfo.FileName =="Report.pdf";}}publicclassMyFileObject{publicstringFileName{get;set;}publicbyte[] Data {get;set;}}publicenumFileUploadValues{FileIsUploaded,FileBytesIsNull,FileBytesIsEmpty,}[ExcludeFromCodeCoverage][DataTestMethod][DataRow(FileUploadValues.FileIsUploaded)][DataRow(FileUploadValues.FileBytesIsNull)][DataRow(FileUploadValues.FileBytesIsEmpty)]publicasync ValueTask Early_Return_Tests(FileUploadValuesfileUploadValue){byte[]fileBytes=null;if(fileUploadValue== FileUploadValues.FileIsUploaded){using(varms=new MemoryStream()){using(varfs= File.Open("./myfile.txt", FileMode.Open, FileAccess.Read, FileShare.Read)){await fs.CopyToAsync(ms);fileBytes= ms.ToArray();}}}elseif(fileUploadValue== FileUploadValues.FileBytesIsEmpty){fileBytes= Array.Empty<byte>();}varfileObj=new MyFileObject
{Data=fileBytes,FileName="INVALID_FILE_NAME"};boolisSuccessful= Uploader.Upload(fileObj);
Assert.IsFalse(isSuccessful);}}}
AB#2300885
The text was updated successfully, but these errors were encountered:
@radmorecameron Thanks for reporting this. What TargetFramework are you using for the test project? Also what version are you using?
The support for this should be in 3.5 brought by #3137. However, I can see this is broken if you are using .NET Framework (getting ValueTask via System.Threading.Tasks.Extensions), or if you are using the EOL netcoreapp3.1.
Hi, I am using MSTest 3.6.1 (The metapackage, https://www.nuget.org/packages/MSTest ). This Test project is targeting .NET Framework 4.8 so I guess that's why it's not working. I wrote the example code with .NET 8 (which is working after cleaning and building), sorry for any confusion.
Summary
When using DataTestMethod and DataRow, sometimes only some of the tests will be asyncronous. It'd be nice if test methods that return ValueTask instead of Task or Void can be discovered.
This would be useful if the tests sometime run an async method to get data to pass to it or if the awaited methods in a test are all ValueTask
Here's some example code of when this might be useful :
Example Code
AB#2300885
The text was updated successfully, but these errors were encountered: