Jose Escrich

Extensibility Thoughts

Testing T4 templates using the GAX host


Download Sample Package


I guess that there are not a best approach to do the tests without a built in support in the T4 template host. Nevertheless in the Web Service Software Factory we did a very first approach of how you could write test units for t4 templates.

Suppose that we have a template which the output is a simple class, in the following example I did a simple package which contains a T4 template called Programs.cs.t4 so, the idea is, run the transformation and then try to compile the class to get its Type in order to make asserts using reflection.

In the attached sample fixture it has an ExecuteTemplate helper method which has the logic to run the transformation and then try to compile it. (considering that the output is a .cs class, if not, it could return an XmlDocument in case that the transformation results in an xml file).



In the next picture you've the TestMethod which runs the transformation, gets the returned type and then perform different asserts over it.


Notice that you should have created a link to the T4 template file and set the *Copy to Output* property as "Copy if newer" as showing in the following image, this is an important thing because the TestMethod is marked with the DeploymentItemAttribute in order to have the t4 file in the test context. Also you have to reference the Microsoft.Practices.RecipeFramework.VisualStudio.Library and Microsoft.VisualStudio.TextTemplating assemblies which are located in the PublicAssemblies folder.




Download Sample Package

Comments

steve said:

Just wanted to say thanks for this and your other posts on GAX/GAT, I have just started working with these tools and these posts are very informative!

# October 30, 2007 10:07 PM

jakew said:

Microsoft has a template engine bundled in with its DSL tools and with GAT (Guidance Automation Toolkit).

# December 27, 2007 4:30 PM

Oleg Sych - » How to generate multiple outputs from single T4 template said:

Pingback from  Oleg Sych  -   » How to generate multiple outputs from single T4 template

# March 28, 2008 8:34 AM

Oleg Sych - » Test runner for T4 unit tests said:

Pingback from  Oleg Sych  -   » Test runner for T4 unit tests

# June 19, 2008 8:06 PM

VS2010学习 said:

Update: A new version of the code described in this article is available in T4 Toolbox . For details

# June 5, 2009 6:59 AM

VS2010学习 said:

I noticed a clear trend of increasing complexity in the T4 text templates I wrote over the last few months

# June 5, 2009 7:06 AM

Learningcyberchic said:

I am currently having problems with running this code to pass me template file with an input parameter.

When  it hits the following line of code:

engine.ProcessTemplate(template, host);

it throws up the following error:

 System.Exception occurred

 Message="The method or operation is not implemented."

 Source="Microsoft.Practices.RecipeFramework.VisualStudio.Library"

 StackTrace:

      at Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TemplateHost.GetHostOption(String optionName)

 InnerException:

within my template file I have set the following attributes:

hostspecific="True" debug="True"

I am new to T4 so does anyone have a clue where I may be going wrong :-()

# December 4, 2009 11:11 AM

Eric said:

Hello.

I'have two problems.

Firstly; the sample package linked on this page is not available. (but it's not really important)

Secondly; I do not arrive to declare PropertyDate and HostTemplate in my code; and this is really important. Have you an idea ?

I'm using VS 2010 pro beta 2; and i add Microsoft.Practives.RecipeFramework.VisualStudio.Library as reference.

Thanks by advance.

# January 6, 2010 8:42 AM

police car auctions Weymouth said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# October 31, 2010 7:08 PM

playstation 2 repair parts said:

Very informative post. Thanks for taking the time to share your view with us.

# November 6, 2010 1:14 PM

Sucherubre said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# November 23, 2010 12:35 PM

Squashing your old sql scripts into migrations using T4 templates. at Simbu said:

Pingback from  Squashing your old sql scripts into migrations using T4 templates. at Simbu

# November 23, 2010 8:44 PM

ipad accessories review said:

The man who has made up his mind to win will never say "impossible ".

-----------------------------------

# December 19, 2010 10:15 PM

meadlemaGip said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# January 12, 2011 1:26 PM

product reviews said:

I prefer to take breaks throughout the my working day and look by way of some blogs to find out what individuals are stating. This webpage appeared in my searches and that i could not aid but clicking on it. I'm happy I did since it had been a extremely fulfilling read.

--------------------------------------------------------------------    

African and African American Studies

# January 17, 2011 9:17 AM

ChafterBrar said:

You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

# January 22, 2011 3:27 AM

Poummadaurn said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# February 6, 2011 5:36 PM

georgia auto auctions said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# February 20, 2011 2:31 AM

Uriliopsili said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# February 26, 2011 7:07 PM

anirwayjady said:

You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

# February 27, 2011 5:21 PM

bahglyday said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# March 3, 2011 3:21 PM

fouradarams said:

I’ve been visiting your blog for a while now and I always find a gem in your new posts.  Thanks for sharing.

# March 6, 2011 3:31 AM

TerreFappaply said:

You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

# March 28, 2011 8:23 PM

vehicles said:

Awesome Blog. I add this Post to my bookmarks.

# May 1, 2011 8:35 PM

twingigma said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# July 13, 2011 3:20 PM

twingigma said:

Great Blog. I add this Blog to my bookmarks.

# July 15, 2011 3:05 PM

twingigma said:

You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

# July 16, 2011 4:50 PM

Allerblauntee said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# July 17, 2011 3:09 PM

Allerblauntee said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# July 18, 2011 3:40 PM

Allerblauntee said:

You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

# July 19, 2011 2:29 PM

OEM software online said:

Xwx0k3 Cool! You have answered. I'll take a quote! The meaning of life and everything else. Decided. No kidding!...

# September 24, 2011 7:22 AM

Cheap oem software said:

hFRwQS Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...

# October 29, 2011 4:34 AM

Bob Molding said:

Quite Interesting Weblog! Thanks For This Post!

# November 2, 2011 10:30 PM

hostgator black friday said:

Digital earth is no exemption to be able to Black Friday. In inescapable fact Black Friday might be far more identified over the internet compared with retail stores. Hostgator the main web hosting organization in the world isn't a exemption to be able to it. Hostgator Black Friday appeared to be another almost all dug into name about Google throughout 2010. People moved upset along with obtaining its shared web hosting plans. No small with available 80% OFF bargain for your confined point in time and after that 50% OFF full Black Friday.

You Can Check Out Hostgator Black Friday 2011 Hot Offer HERE

<a href=www.care2.com/.../3026464>black friday hostgator2011</a>

# November 25, 2011 4:38 AM

spider repellent said:

Heya are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be greatly appreciated!

# January 4, 2012 9:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)