0

I'm new to unit testing and nUnit in general and I'm not sure what I'm trying to do is possible or if I'm using the right syntax (in C#).

I want to pass for a test a collection of IEnumerable using the TestCaseSource attribute and to compare it a collection of results but in a way that the X argument from the first collection would be tested against the X argument from the second with the Sequential attribute.

Something like this:

[Test]
[TestCaseSource("ListCases"), Sequential]
public void FormatListTest( [Values()] IEnumerable ListCases, 
   [Values("0,2,3", "1,thanks,<3", "0", "")] string listResult)
{
   string result = FormatUtils.FormatList(ListCases);
   Assert.AreEqual(listResult, result);
}

static object[] ListCases =
{
   new object[] {0,2,3},
   new object[] {"1","thanks","<3"},
   new object[] {0},
   new object[] {},
};

Obviously, it doesn't work. While writing this question I figured out that what I need to do is:

[Test]
[Sequential]
public void FormatListTest( [Values(new object[] { 0, 2, 3 },
   new object[] { "1", "thanks", "<3" },
   new object[] {0},
   new object[] {})] IEnumerable ListCases, [Values("0,2,3", "1,thanks,<3", "0", "")] string listResult)
   {
      string result = FormatUtils.FormatList(ListCases);
      Assert.AreEqual(listResult, result);
   }

But I'm still curious for future use if I can "route" different sources for several inputs (inputs and expected results that is) to assert one against its expected result is some fashion using TestCaseSource and Sequential at the same time.

For all intents and purposes, I'm using it for a Unity game but all the code above have nothing to do with Unity directly (besides running it) or with MonoBehaviour.

1 Answer 1

0

You can check https://github.com/nunit/docs/wiki/TestCaseData

Update: This is compiled code, using NUnit 3.11. Note that if you change the Returns() values in TestCases tests will fail.

class FormatUtils
{
    public static string FormatList(List<object> a)
    {
        //your code here
    }
}
[TestFixture]
public class MyTests
{
    [TestCaseSource(typeof(MyDataClass), "TestCases")]
    public string FormatListTest(List<object> list)
    {
        return FormatUtils.FormatList(list);
    }
}
public class MyDataClass
{
    public static IEnumerable TestCases
    {
        get
        {
            yield return new TestCaseData(new List<object> { 0, 2, 3 }).Returns("0,2,3");
            yield return new TestCaseData(new List<object> { "1", "thanks", "<3" }).Returns("1,thanks,<3");
            yield return new TestCaseData(new List < object > { 0 }).Returns("0");
            yield return new TestCaseData(new List<object> { }).Returns("");
        }
    }
}
4
  • Well, it definitely gets me closer but I still can't nail down the syntax to compare. return is not a valid keyword in this context and it must be some kind of Assert.AreEqauls("returned value", FormatUtils.FormatList(ListCases)) or something similar with AssertTrue(. Mar 13, 2019 at 15:29
  • If you have .Returns(something) in the testcase data, this acts as Assert. I will try to test the code later.
    – Jeni
    Mar 13, 2019 at 17:49
  • Updated the code snippet. This is test I successfully compiled.
    – Jeni
    Mar 13, 2019 at 20:18
  • Thank you very much, @Jeni . This is how it works! For some reason, it takes Unity a few tries before showing the right results for me (maybe some caching error?) but it definitely works. Mar 17, 2019 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.