Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Last active October 18, 2023 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerryNixon/3df3944f856737b5cef831ab8b02641c to your computer and use it in GitHub Desktop.
Save JerryNixon/3df3944f856737b5cef831ab8b02641c to your computer and use it in GitHub Desktop.
Using complex data types as InlineData for Xunit tests.
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace PlaygroundXunit
{
public class UnitTest1
{
[Theory]
[MemberData(nameof(Test1Data))]
public void Test1Test((string FirstName, string LastName) values)
{
Assert.NotNull(values.FirstName);
Assert.NotNull(values.LastName);
}
public static IEnumerable<object[]> Test1Data
{
get => new List<(string FirstName, string LastName)>
{
("Jerry", "Nixon"),
("Rob", "Bagby")
}.Select(x => new[] { (object)x });
}
}
}
@bradwilson
Copy link

bradwilson commented Oct 17, 2020

I think this is simpler with TheoryData:

using Xunit;

namespace PlaygroundXunit
{
    public class UnitTest1
    {
        [Theory]
        [MemberData(nameof(Test1Data))]
        public void Test1Test(string firstName, string lastName)
        {
            Assert.NotNull(firstName);
            Assert.NotNull(lastName);
        }

        public static TheoryData<string, string> Test1Data
        {
            get => new TheoryData<string, string>
            {
                { "Jerry", "Nixon" },
                { "Rob", "Bagby" }
            };
        }
    }
}

@JerryNixon
Copy link
Author

I agree.

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