How to add additional attributes to generated test methods

969 views
Skip to first unread message

Shrike

unread,
Jul 28, 2010, 8:48:01 AM7/28/10
to SpecFlow
Hi.
I'm going to use SpecFlow to create tests for VS packages
(specifically based on DSL Tools).
To write tests for VS itself I need to use "VS Host" as test host
(feature of MsTest)

[TestClass]
public class IntegrationTests
{
[TestMethod]
[HostType("VS IDE")]
public void test1() {}
}

SpecFlow generates c# code of test in "feature.cs" files. So I need to
add HostTypeAttribute("VS IDE") to generated test methods.
How can I do this?

Also I'd like to add some logic into ClassInitialize-method.
Thanks.

Carl Hörberg

unread,
Jul 28, 2010, 10:06:30 AM7/28/10
to spec...@googlegroups.com
maybe a partial class would work for you? 

Shrike

unread,
Jul 28, 2010, 11:44:09 AM7/28/10
to SpecFlow
Thanks for the suggestion,
but partial class doesn't allow adding an attribute to an existing
method

Carl Hörberg

unread,
Jul 28, 2010, 12:09:55 PM7/28/10
to spec...@googlegroups.com
Yes, that's right.. sent the answer without thinking far enough.. (although you can have partial methods as well (http://msdn.microsoft.com/en-us/library/wa80x488.aspx#sectionToggle3) but in this situation i don't think it's advisable)

Gáspár Nagy

unread,
Jul 29, 2010, 6:46:32 AM7/29/10
to SpecFlow
You can implement a custom generator class (deriving from the MsTest
generator:
TechTalk.SpecFlow.Generator.UnitTestProvider.MsTestGeneratorProvider)
that you can configure in the app.config:

<unitTestProvider name="MsTest" generatorProvider="your-class-name,
your-assembly" />

the code should be something like (not tested):

public class MySpecialGenerator: MsTestGeneratorProvider
{
public override void SetTest(CodeMemberMethod memberMethod,
string title)
{
base.SetTest(memberMethod, title);

memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new
CodeTypeReference("Microsoft.VisualStudio.TestTools.UnitTesting.HostTypeAttribute"),
new CodeAttributeArgument(new
CodePrimitiveExpression("VS IDE"))));
}
}

Br,
Gaspar


On Jul 28, 6:09 pm, Carl Hörberg <carl.hoerb...@gmail.com> wrote:
> Yes, that's right.. sent the answer without thinking far enough.. (although
> you can have partial methods as well (http://msdn.microsoft.com/en-us/library/wa80x488.aspx#sectionToggle3) but in
> this situation i don't think it's advisable)
>

Shrike

unread,
Jul 29, 2010, 8:49:40 AM7/29/10
to SpecFlow
Wow, that's what I need, thanks!

p.s. SpecFlow rocks!
Reply all
Reply to author
Forward
0 new messages