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
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("");
}
}
}
-
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 ofAssert.AreEqauls("returned value", FormatUtils.FormatList(ListCases))
or something similar withAssertTrue(
.Shay Krainer– Shay Krainer2019年03月13日 15:29:27 +00:00Commented 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– Jeni2019年03月13日 17:49:48 +00:00Commented Mar 13, 2019 at 17:49 -
Updated the code snippet. This is test I successfully compiled.Jeni– Jeni2019年03月13日 20:18:54 +00:00Commented 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.Shay Krainer– Shay Krainer2019年03月17日 14:18:21 +00:00Commented Mar 17, 2019 at 14:18