Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access MSTest's TestContext property via ScenarioContext #882

Merged
merged 3 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class MsTestGeneratorProvider : IUnitTestGeneratorProvider

protected const string TESTCONTEXT_TYPE = "Microsoft.VisualStudio.TestTools.UnitTesting.TestContext";

protected const string TESTCONTEXT_FIELD_NAME = "_testContext";
protected const string TESTCONTEXT_PROPERTY_NAME = "TestContext";

protected CodeDomHelper CodeDomHelper { get; set; }

public virtual UnitTestGeneratorTraits GetTraits()
Expand All @@ -44,11 +47,32 @@ private void SetProperty(CodeTypeMember codeTypeMember, string name, string valu
public virtual void SetTestClass(TestClassGenerationContext generationContext, string featureTitle, string featureDescription)
{
CodeDomHelper.AddAttribute(generationContext.TestClass, TESTFIXTURE_ATTR);

// Add a TestContext field
generationContext.TestClass.Members.Add(new CodeMemberField(TESTCONTEXT_TYPE, TESTCONTEXT_FIELD_NAME));

// Add a TestContext property
var testContextProperty = new CodeMemberProperty
{
Attributes = MemberAttributes.Public,
Name = TESTCONTEXT_PROPERTY_NAME,
HasGet = true,
HasSet = true,
Type = new CodeTypeReference(TESTCONTEXT_TYPE)
};
testContextProperty.GetStatements.Add(new CodeMethodReturnStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), TESTCONTEXT_FIELD_NAME)));
testContextProperty.SetStatements.Add(new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), TESTCONTEXT_FIELD_NAME), new CodePropertySetValueReferenceExpression()));

generationContext.TestClass.Members.Add(testContextProperty);
}

public virtual void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
{
//MsTest does not support caregories... :(
//MsTest does not support categories... :(
}

public void SetTestClassIgnore(TestClassGenerationContext generationContext)
Expand All @@ -58,7 +82,18 @@ public void SetTestClassIgnore(TestClassGenerationContext generationContext)

public virtual void FinalizeTestClass(TestClassGenerationContext generationContext)
{
// by default, doing nothing to the final generated code
// testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<TestContext>(TestContext);
generationContext.ScenarioInitializeMethod.Statements.Add(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeFieldReferenceExpression(null, generationContext.TestRunnerField.Name),
"ScenarioContext"),
"ScenarioContainer"),
"RegisterInstanceAs",
new CodeTypeReference(TESTCONTEXT_TYPE)),
new CodeVariableReferenceExpression(TESTCONTEXT_PROPERTY_NAME)));
}

public void SetTestClassParallelize(TestClassGenerationContext generationContext)
Expand Down
20 changes: 20 additions & 0 deletions Tests/TechTalk.SpecFlow.Specs/Features/MsTestProvider.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ Scenario: Should handle scenario outlines
| Succeeded |
| 2 |

Scenario: Should be able to access TestContext in Steps
Given there is a SpecFlow project
And the project is configured to use the MsTest provider
And a scenario 'Simple Scenario' as
"""
When I do something
"""
And the following step definition
"""
[When(@"I do something")]
public void WhenIDoSomething()
{
System.Console.WriteLine(ScenarioContext.Current.ScenarioContainer.Resolve<Microsoft.VisualStudio.TestTools.UnitTesting.TestContext>().TestName);
}
"""
When I execute the tests with MsTest
Then the execution summary should contain
| Succeeded |
| 1 |

@config
Scenario: Should be able to specify MsTest provider in the configuration
Given there is a SpecFlow project
Expand Down