Was getting annoyed today with MsTest’s support for inline datasources for test cases. Started work on a little work around hack to simply generate and inject the xml files behind the scenes.
I ideally I will be able to inject enough logic into mstest that I can get rid of that intermediary as well.
For now I have
[DataProviderMethodName("DataProviderExample")]
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestMethod1.xml", "scenario", DataAccessMethod.Sequential)]
public void UseRawDataProviderEntries()
{
System.Diagnostics.Trace.WriteLine("somestring = " + TestContext.DataRow["somestring"].ToString());
System.Diagnostics.Trace.WriteLine("Gotcha = " + TestContext.DataRow["Gotcha"].ToString());
Assert.IsTrue((bool)RawDataProviderContext["Gotcha"]);
}
And I want to atleast get to
[DataProviderMethodName("DataProviderExample")]
[TestMethod]
public void UseRawDataProviderEntries()
{
int t = 3243;
System.Diagnostics.Trace.WriteLine("somestring = " + TestContext.DataRow["somestring"].ToString());
System.Diagnostics.Trace.WriteLine("Gotcha = " + TestContext.DataRow["Gotcha"].ToString());
Assert.IsTrue((bool)RawDataProviderContext["Gotcha"]);
}
although . . .
[TestMethod]
[DataProviderMethodName("DataProviderExample")]
public void UseRawDataProviderEntries(bool Gotcha, string somestring)
{
int t = 3243;
System.Diagnostics.Trace.WriteLine("somestring = " + somestring);
System.Diagnostics.Trace.WriteLine("Gotcha = " + Gotcha.ToString());
Assert.IsTrue(Gotcha);
}
//===============
// And
//===============
[TestMethod]
[InlineDataSet]
[DataSet("gotcha column","some string column")]
[DataSet("just used reflection "," to read attributes and plug them in order of appearance into testmethod")]
public void UseRawDataProviderEntries(string gotcha, string somestring)
{
int t = 3243;
System.Diagnostics.Trace.WriteLine("somestring = " + somestring);
System.Diagnostics.Trace.WriteLine("gotcha = " + gotcha);
Assert.IsTrue(Gotcha == "true");
}
would be swell.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace TestProject1
{
[System.AttributeUsage(System.AttributeTargets.Method)]
public class DataProviderMethodName : System.Attribute
{
private string name;
public double version;
public string Name
{
get
{
return name;
}
}
public DataProviderMethodName(string name)
{
this.name = name;
version = 1.0;
}
}
[TestClass]
public abstract class HackThePlanet
{
//todo encapsulate further
protected static Dictionary>> _dataprovidercache = new Dictionary>>();
protected Dictionary RawDataProviderContext
{
get
{
if (TestContext.DataRow["meta_provider"] != null)
{
return _dataprovidercache[TestContext.DataRow["meta_provider"] + "_" + TestContext.DataRow["meta_providerhash"]][int.Parse(TestContext.DataRow["meta_index"].ToString())];
}
else
{
return new Dictionary();
}
}
}
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
}
[TestClass]
public class UnitTest1 : HackThePlanet
{
static public List> DataProviderExample()
{
return new List>()
{
new Dictionary { {"Gotcha", false} , {"somestring","value2"} },
new Dictionary { {"Gotcha", true} , {"somestring","value3"} },
new Dictionary { {"Gotcha", false} , {"somestring","value2"} },
};
}
static public List> DataProviderExample2()
{
return new List>()
{
new Dictionary { {"Gotcha", false} , {"agag","value2"} },
new Dictionary { {"Gotcha", true} , {"agag","value3"} },
new Dictionary { {"Gotcha", false} , {"agag","value2"} },
};
}
[ClassInitialize()]
public static void SetupClass(TestContext testContext)
{
var d = new DataSourceAttribute("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\XMLFile1.xml", "row", DataAccessMethod.Sequential);
var de = new DeploymentItemAttribute("TestProject1\\XMLFile1.xml");
MethodInfo[] methodInfos = typeof(UnitTest1).GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach(var m in methodInfos)
{
var t = m.Attributes;
var a = m.GetCustomAttributes(false);
var UseDataProviderMethodList = (from dpm in a
where dpm.GetType() == typeof(DataProviderMethodName)
select dpm);
if (UseDataProviderMethodList.Count() > 0)
{
// Grab DataSourceAttribute
var DataSourceAttributeList = (from dsa in a
where dsa.GetType() == typeof(DataSourceAttribute)
select dsa);
if (DataSourceAttributeList.Count() > 0)
{
var DataProviderMethodName = ((DataProviderMethodName)UseDataProviderMethodList.First()).Name;
var DataProviderMethod = typeof(UnitTest1).GetMethod(DataProviderMethodName);
var dataset = (List>)DataProviderMethod.Invoke(null, new object[0]);
//todo add some hash logic here if we want to allow dataprovider to return non determanistic data.
var hash = DateTime.Now.GetHashCode();
_dataprovidercache[DataProviderMethodName + "_" + hash] = dataset;
var DataSource = (DataSourceAttribute)DataSourceAttributeList.First();
//todo better support for different location preferences.
var requestedFile = DataSource.ConnectionString.Replace("|DataDirectory|\\", "\\");
//todo use xml tools for htis
string output = "";
output += "\n";
output += "\n";
int index = 0;
foreach (Dictionary scenario in dataset)
{
output += "\n";
// record keeping
output += "" + (index++) + " ";
output += "" + DataProviderMethodName + " ";
output += "" + hash + " ";
foreach (KeyValuePair kv in scenario)
{
output += " <" + kv.Key.Replace("\"", "\\\"") + ">" + kv.Value.ToString().Replace("\"", "\\\"") + "" + kv.Key.Replace("\"", "\\\"") + ">\n";
}
output += " \n";
}
output += " \n";
System.IO.File.WriteAllText(testContext.DeploymentDirectory + requestedFile, output);
}
}
}
}
[TestMethod]
public void TestMethod1()
{
this.ToString();
bool k = true;
Assert.IsTrue(k);
}
//@todo Implement some way of preventing overwrites to the same DataSource.XML file ( preferably alter what is stored in the runner for xml name and
// just replace with testcase name to insure uniqueness.
[DataProviderMethodName("DataProviderExample")]
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestMethod1.xml", "scenario", DataAccessMethod.Sequential)]
public void UseRawDataProviderEntries()
{
int t = 3243;
System.Diagnostics.Trace.WriteLine("somestring = " + TestContext.DataRow["somestring"].ToString());
System.Diagnostics.Trace.WriteLine("Gotcha = " + TestContext.DataRow["Gotcha"].ToString());
Assert.IsTrue((bool)RawDataProviderContext["Gotcha"]);
}
[DataProviderMethodName("DataProviderExample2")]
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestMethod2.xml", "scenario", DataAccessMethod.Sequential)]
public void UseRegularTestContext()
{
int t = 3243;
System.Diagnostics.Trace.WriteLine("agag = " + TestContext.DataRow["agag"].ToString());
System.Diagnostics.Trace.WriteLine("Gotcha = " + TestContext.DataRow["Gotcha"].ToString());
Assert.IsTrue(TestContext.DataRow["agag"].ToString() == "value3");
}
}
}
. . .

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 