Last active
November 18, 2020 17:38
-
-
Save jasongorman/5272a59b78a1d78fc1dfd1e373abf763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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