SpecFlow:ClassInitialize和的TestContext [英] SpecFlow: ClassInitialize and TestContext

查看:769
本文介绍了SpecFlow:ClassInitialize和的TestContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我是新来SpecFlow。



我有我/想用MSTest的运行作为一个功能测试涉及到自动化的特征文件完全设置服务器,数据访问...
为此我必须配置与在SpecFlow的给定块中的数据服务器,然后再启动它。我也有一些文件复制到测试的输出目录



在我所用的ClassInitialize属性来从的TestContext的TestDeploymentDir非SpecFlow功能测试;是这样的:

  [ClassInitialize] 
公共静态无效ClassSetup(的TestContext上下文)
{
TargetDataDeploymentRoot = context.TestDeploymentDir;
}

现在与SpecFlow我不能了,因为它由使用该属性SpecFlow本身。
一些新的属性确实存在,如 BeforeFeature 这同样的作用,但它不会对的TestContext作为参数传递。



我只需要获得访问的TestContext的TestDeploymentDir为了那里复制一些文件确实空空我的功能测试服务器之前 - 轻松可行的,而不SpecFlow但SpecFlow几乎是不可能的。



如何解决这个问题?



是否有可能呢?



非常感谢您的咨询!



罗伯特






环境:




  • 的Visual Studio 2012

  • SpecFlow 1.9.0.77


< DIV CLASS =h2_lin>解决方案

为了能够访问值中的TestContext你必须为你在其中添加各种方案文件中创建部分类。

 使用Microsoft.VisualStudio.TestTools.UnitTesting; 
使用TechTalk.SpecFlow;

///<总结>
///为支撑的TestContext部分类。
///< /总结>
公共部分类DistributionFeature
{
///<总结>
///测试执行上下文。
///< /总结>
私人的TestContext的TestContext;

///<总结>
///获取或设置测试执行上下文。
///< /总结>
公众的TestContext的TestContext
{
得到
{
返回this.testContext;
}


{
this.testContext =价值;

//看到https://github.com/techtalk/SpecFlow/issues/96
this.TestInitialize();
FeatureContext.Current [的TestContext] =值;
}
}
}



然后,你可以访问部署利用你的脚步目录

  VAR的TestContext =(的TestContext)FeatureContext.Current [的TestContext]; 
VAR deploymentDir = testContext.TestDeploymentDir;

如果你有太多的情况下,那么你可能必须用T4自动此类文件的创建。


first of all I'm new to SpecFlow.

I have a feature file which I have / want to automate using MSTest to run as a functional test involving a fully set up server, data access ... For this purpose I have to configure the server with the data in the SpecFlow's 'Given' blocks and start it afterwards. I also have to copy some files to the test's output directory.

In the non-SpecFlow functional tests I was using the ClassInitialize attribute to get the TestDeploymentDir from the TestContext; something like this:

[ClassInitialize]
public static void ClassSetup(TestContext context)
{
  TargetDataDeploymentRoot = context.TestDeploymentDir;
}

Now with SpecFlow I can't use this attribute anymore as it is used by SpecFlow itself. Some new attributes do exist, like BeforeFeature which acts similarly BUT it doesn't pass on the TestContext as a parameter.

I just need to get access to the TestContext's TestDeploymentDir in order to copy some files there before really lauching my functional test server - easily doable without SpecFlow but almost impossible with SpecFlow.

How to deal with this issue?

Is it possible at all?

Thanks a lot for advice!

robert


Environment:

  • Visual Studio 2012
  • SpecFlow 1.9.0.77

解决方案

In order to have access to values in the TestContext you have to create partial class for each scenario file you have in which you add the .

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;

/// <summary>
/// Partial class for TestContext support.
/// </summary>
public partial class DistributionFeature
{
    /// <summary>
    /// Test execution context.
    /// </summary>
    private TestContext testContext;

    /// <summary>
    /// Gets or sets test execution context.
    /// </summary>
    public TestContext TestContext
    {
        get
        {
            return this.testContext;
        }

        set
        {
            this.testContext = value;

            //see https://github.com/techtalk/SpecFlow/issues/96
            this.TestInitialize();
            FeatureContext.Current["TestContext"] = value;
        }
    }
}

Then you could access the deployment directory from your steps using

var testContext = (TestContext)FeatureContext.Current["TestContext"];
var deploymentDir = testContext.TestDeploymentDir;

If you have too many scenarios, then you probably has to automate creation of such files with T4.

这篇关于SpecFlow:ClassInitialize和的TestContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆