【问题标题】:Specflow - Create Pre-defined data to be shared between all scenarios in test execution with parallel executionSpecflow - 创建预定义的数据,以便在并行执行的测试执行中的所有场景之间共享
【发布时间】:2020-12-19 11:10:26
【问题描述】:

我正在尝试重新创建我的 BeforeTestRun 步骤,以便在整个执行而不是每个线程中只运行一次我的设置。

我查看了自定义部署步骤,我已经实现了一些,但是对于我的设置,我需要从 app.config 文件中引入一些值,我正在尝试类似这样的操作

我的 Default.srprofile 文件包含:

 <DeploymentTransformation>
    <GlobalSteps>
      <Custom type="Test.CustomDeploymentStep, Test"></Custom>
    </GlobalSteps>
  </DeploymentTransformation>

还有我的 CustomDeploymentStep.cs:

public class CustomDeploymentStep : IDeploymentTransformationStep
{
    public static string baseUrl;

    public void Apply(IDeploymentContext deploymentContext)
    {
       
        baseUrl = ConfigurationManager.AppSettings["URL"];
    }
    public void Restore(IDeploymentContext deploymentContext)
    {
        
        DoSomething();
    }

}

我的应用配置包含以下内容:

<add key="URL" value="http://google.com" />

但这不起作用,ConfigurationManager.AppSettings 只返回一个键和一个值

"key" : "TestProjectRetargetTo35Allowed" "value":"true"

如何将我的配置从 app.config 加载到 CustomDeploymentStep 的 Apply() 方法中?

另外,如果有更好/更有效的方式来生成具有线程安全执行的规范流中的预定义数据,请告诉我

【问题讨论】:

  • 不是ConfigurationManager.AppSettings -- 复数吗?
  • 确实是错字
  • 愚蠢的问题,但是您使用的是 .NET Framework 还是 .NET Core?
  • @GregBurghardt NET 框架
  • 您可以在您的问题中添加更多 app.config 吗?

标签: c# .net specflow configurationmanager


【解决方案1】:

当我需要在一个大型解决方案中的多个项目中使用自定义部署步骤时,我遇到了同样的问题。这似乎是 TechTalk.SpecRun.Framework 中的一个错误。该错误可能是“应用全局部署步骤时出错。全局步骤不能包含测试程序集特定设置。”如果您在调试时查看 TestAssembly 内部,您将看到 TestAssemblyConfigFilePath 为 null 和/或吞下另一个异常。

它不注册项目特定的配置文件。我的解决方法是将配置文件保存到调试中并像这样访问我需要的内容:

      string appConfigFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\App.config";

      ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
      configMap.ExeConfigFilename = appConfigFilePath;
      var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
      var baseUrl = config.AppSettings.Settings["URL"].Value;

【讨论】:

  • 您能否查看我的回答并认为它有用?它确实解决了你的问题。
  • 你好抱歉回复晚了,这确实解决了上面的问题,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多