Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created January 20, 2011 16:34
Show Gist options
  • Select an option

  • Save mgroves/788140 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/788140 to your computer and use it in GitHub Desktop.
T4 test generation
<#@ template language="C#" hostspecific="true" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#
string TestXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<WebTest Name=""{0}"" Id=""{2}"" Owner="""" Priority=""0"" Enabled=""True"" CssProjectStructure="""" CssIteration="""" Timeout=""0"" WorkItemIds="""" xmlns=""http://microsoft.com/schemas/VisualStudio/TeamTest/2006"" Description="""" CredentialUserName="""" CredentialPassword="""" PreAuthenticate=""True"" Proxy="""" StopOnError=""False"">
<Items>
<Request Method=""GET"" Version=""1.1"" Url=""{{{{webserver}}}}/freecache.aspx"" ThinkTime=""0"" Timeout=""300"" ParseDependentRequests=""True"" FollowRedirects=""True"" RecordResult=""True"" Cache=""False"" ResponseTimeGoal=""0"" Encoding=""utf-8"" ExpectedHttpStatusCode=""0"" ExpectedResponseUrl="""" />
<Request Method=""GET"" Version=""1.1"" Url=""{{{{webserver}}}}/clearbuffers.aspx"" ThinkTime=""0"" Timeout=""300"" ParseDependentRequests=""True"" FollowRedirects=""True"" RecordResult=""True"" Cache=""False"" ResponseTimeGoal=""0"" Encoding=""utf-8"" ExpectedHttpStatusCode=""0"" ExpectedResponseUrl="""" />
<Request Method=""GET"" Version=""1.1"" Url=""{{{{webserver}}}}/clearwebcache.aspx"" ThinkTime=""0"" Timeout=""300"" ParseDependentRequests=""True"" FollowRedirects=""True"" RecordResult=""True"" Cache=""False"" ResponseTimeGoal=""0"" Encoding=""utf-8"" ExpectedHttpStatusCode=""0"" ExpectedResponseUrl="""" />
<Request Method=""GET"" Version=""1.1"" Url=""{{{{webserver}}}}/Default.aspx/{0}{1}"" ThinkTime=""0"" Timeout=""300"" ParseDependentRequests=""True"" FollowRedirects=""True"" RecordResult=""True"" Cache=""False"" ResponseTimeGoal=""0"" Encoding=""utf-8"" ExpectedHttpStatusCode=""0"" ExpectedResponseUrl="""" />
</Items>
<ContextParameters>
<ContextParameter Name=""webserver"" Value=""http://happy-halloween.dalapp001.corp.telligent.com"" />
<ContextParameter Name=""SiteId"" Value=""1"" />
<ContextParameter Name=""DateRange"" Value=""/custom/PreviousYear"" />
<ContextParameter Name=""GroupId"" Value=""-1"" />
</ContextParameters>
</WebTest>";
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("BrowserReport1","/Firefox");
dict.Add("BrowserSummaryReport","");
// ...etc...
foreach (KeyValuePair<string,string> kvp in dict)
{
GenerateFile(string.Format(TestXml, kvp.Key, kvp.Value, Guid.NewGuid()));
SaveOutput(kvp.Key + ".webtest");
}
#>
<#+
void GenerateFile(string fileContents)
{
#>
<#= fileContents #>
<#+
}
#>
<#+
void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment