Skip to content

Instantly share code, notes, and snippets.

@jasongorman
Last active November 18, 2020 17:38
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 jasongorman/5272a59b78a1d78fc1dfd1e373abf763 to your computer and use it in GitHub Desktop.
Save jasongorman/5272a59b78a1d78fc1dfd1e373abf763 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Reflection;
using NUnit.Framework;
namespace Excel.CustomerTests
{
[TestFixture]
public class MathsCustomerTests
{
[Test, TestCaseSource(typeof (ExcelDataSource), "Data")]
public void SquareRoot(string number, string tolerance, string squareRoot)
{
Assert.That(Maths.SquareRoot(ToDouble(number)),
Is.EqualTo(ToDouble(squareRoot)).Within(ToDouble(tolerance)));
}
private double ToDouble(string squareRoot)
{
return Double.Parse(squareRoot);
}
}
public class Maths
{
public static double SquareRoot(double number)
{
return Math.Sqrt(number);
}
}
internal class ExcelDataSource
{
private static readonly string binPath = Assembly.GetCallingAssembly().CodeBase;
private static readonly string projectPath =
new Uri(binPath.Substring(0, binPath.LastIndexOf("bin", StringComparison.Ordinal))).LocalPath;
public IEnumerable<TestCaseData> Data
{
get
{
var connectString =
string.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";",
projectPath + "customerdata.xlsx");
var connection = new OleDbConnection(connectString);
connection.Open();
var command = new OleDbCommand("SELECT * FROM [examples]", connection);
var reader = command.ExecuteReader();
var testCases = new List<TestCaseData>();
while (reader.Read())
{
var row = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
row.Add(reader.GetValue(i).ToString());
testCases.Add(new TestCaseData(row.ToArray()));
}
return testCases;
}
}
}
}
@ceddlyburge
Copy link

Hi Jason, I enjoyed your blog post about this, and have done something similar (generating BDD / C# tests from Excel), which maybe you'll find ineteresting :)
https://dev.to/ceddlyburge/introducing-customertestsexcel-4f7b

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