Reputation: 8211
How pass data from the testrunner to the unittest?
For example an output path
or interface configuration
of the host machine?.
Upvotes: 1
Views: 2200
Reputation: 2245
You may have already gone done a different path at tis point but I though I would share this. In post 2.5 versions of NUnit the ability to drive test cases in a via an external source was implemented. I did a demo of a simple example using a CSV file.
The CSV was something that contained my two test inputs and the expected result. So 1,1,2 for the first and so on.
CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace NunitDemo
{
public class AddTwoNumbers
{
private int _first;
private int _second;
public int AddTheNumbers(int first, int second)
{
_first = first;
_second = second;
return first + second;
}
}
[TestFixture]
public class AddTwoNumbersTest
{
[Test, TestCaseSource("GetMyTestData")]
public void AddTheNumbers_TestShell(int first, int second, int expectedOutput)
{
AddTwoNumbers addTwo = new AddTwoNumbers();
int actualValue = addTwo.AddTheNumbers(first, second);
Assert.AreEqual(expectedOutput, actualValue,
string.Format("AddTheNumbers_TestShell failed first: {0}, second {1}", first,second));
}
private IEnumerable<int[]> GetMyTestData()
{
using (var csv = new StreamReader("test-data.csv"))
{
string line;
while ((line = csv.ReadLine()) != null)
{
string[] values = line.Split(',');
int first = int.Parse(values[0]);
int second = int.Parse(values[1]);
int expectedOutput = int.Parse(values[2]);
yield return new[] { first, second, expectedOutput };
}
}
}
}
}
Then when you run it with the NUnit UI it looks like (I included a failure for example purposes:
Upvotes: 2