Archive for the ‘Code’ Category
DataDriven Testing with Excel
Validating code with losts of scenario’s gets easier with validating the function with Excel. All you need is Excel.
Start of by naming the variables in the first row, one variable per column. Add an extra column for the result value (or more if needed). Fill out the variables and results.
[TestMethod] [DeploymentItem("bonusCalculation.xlsx")] [DataSource("System.Data.OleDb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bonusCalculation.xlsx;Extended Properties=Excel 12.0;Persist Security Info=False", "bonus$", DataAccessMethod.Sequential)] public void BonusCalcualtion() { double result = Utils.GetSquareRoot(2); double omzet = (double)TestContext.DataRow ["Omzet"]; double winst = (double)TestContext.DataRow["Winst"]; double bonus = (double)TestContext.DataRow["Bonus"]; Assert.AreEqual(bonus, Utils.BonusCalculation(omzet, winst)); }
When you do not have Office 2007, but need to run the test with the .xlsx you might want to download the data driver :
at Microsoft
If you prefer using an older fileformat of excel, compatible with MS Excel 2003 and 2000, use:
[TestMethod] [DeploymentItem("bonusCalculation.xls")] [DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='Calculator.xls';Persist Security Info=False;Extended Properties='Excel 8.0'", "bonus$", DataAccessMethod.Sequential)] { //.... }
One language to rule them all?
Most of my software development is currently done on the .Net framework, with C# as programming language. I’m looking forward to start working with F#. While I’m doing most of my coding in C#, I’m also coding AS3 (ActionScript 3) and javascript on the side.
Meanwhile I’d like to take a week off and start working in plain old MASM again :). Do some hard time with C++ and the DDK to make some annoying filter drivers to tease colleague’s @ work, but all of that will probably won’t happen since I’m working so much lately. For example I’ve arrived at 07:00 sharp at the .. well office and left 18:35, with a small lunch break of only 15 minutes.
Well to get to the point,.. looking for some nice alternative to Flex (Eclipse with Adobe modifications) that will cost me $0.00 I came across a nice project that aims to be a single language to be used as an intermediary language for all the others. It is called haXe. It will convert / compile your code to the desired language, eg. haXe to JavaScript.
Will look at it soon!
Unsorter
Today’s generic lists support sorting calls that can be modified to sort ascending or descending. How about a sort method that will just mangle the data. It may have uses for your TDD (UnitTest) related work.
Here we go:
void Unsort() { Random rnd = new Random(); List<int> myList = new List<int>(); for (int i = 0; i < 100; i++) { myList.Add(i); } myList.Sort( delegate(int a, int b) { if (a != b) return rnd.Next(-1, 2); return 0; }); }
Now your list is unsorted.