Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests that return a ValueTask to be discovered #4056

Open
radmorecameron opened this issue Nov 12, 2024 · 2 comments · May be fixed by #4059
Open

Allow tests that return a ValueTask to be discovered #4056

radmorecameron opened this issue Nov 12, 2024 · 2 comments · May be fixed by #4059

Comments

@radmorecameron
Copy link

radmorecameron commented Nov 12, 2024

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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;

namespace TestProject1
{

    [TestClass]
    public class UnitTest1
    {
        public static class Uploader
        {
            // Uploads data or returns false if data is missing
            public static bool Upload(MyFileObject mfo)
            {
                return !(mfo.Data == null || mfo.Data.Length == 0) && mfo.FileName == "Report.pdf";
            }
        }

        public class MyFileObject
        {
            public string FileName { get; set; }
            public byte[] Data { get; set; }
        }


        public enum FileUploadValues
        {
            FileIsUploaded,
            FileBytesIsNull,
            FileBytesIsEmpty,
        }

        [ExcludeFromCodeCoverage]
        [DataTestMethod]
        [DataRow(FileUploadValues.FileIsUploaded)]
        [DataRow(FileUploadValues.FileBytesIsNull)]
        [DataRow(FileUploadValues.FileBytesIsEmpty)]
        public async ValueTask Early_Return_Tests(FileUploadValues fileUploadValue)
        {
            byte[] fileBytes = null;
            if (fileUploadValue == FileUploadValues.FileIsUploaded)
            {
                using (var ms = new MemoryStream())
                {
                    using (var fs = File.Open("./myfile.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        await fs.CopyToAsync(ms);
                        fileBytes = ms.ToArray();
                    }
                }
            }
            else if (fileUploadValue == FileUploadValues.FileBytesIsEmpty)
            {
                fileBytes = Array.Empty<byte>();
            }

            var fileObj = new MyFileObject
            {
                Data = fileBytes,
                FileName = "INVALID_FILE_NAME"
            };

            bool isSuccessful = Uploader.Upload(fileObj);

            Assert.IsFalse(isSuccessful);
        }
    }
}

AB#2300885

@Youssef1313
Copy link
Member

Youssef1313 commented Nov 13, 2024

@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.

@radmorecameron
Copy link
Author

radmorecameron commented Nov 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants