【问题标题】:Referencing a folder in my project with a SpecFlow test使用 SpecFlow 测试引用我的项目中的文件夹
【发布时间】:2015-01-23 01:15:45
【问题描述】:

我正在尝试编写一个 SpecFlow 测试,以测试当我的应用程序读取特定结构的文件夹和文件时会发生什么。我想在我的项目中包含这些文件夹和文件,这样测试就不会只在我自己的计算机上运行。

例如,我的 Specs 项目中有两个文件夹。一个称为“SimpleTestModel”,另一个称为“ComplexTestModel”。如何在我的 SpecFlow 测试中引用这些文件夹?

【问题讨论】:

  • 您使用什么格式的测试?努尼特?测试? XUnit?基于此,答案会有所不同。
  • 我按照 SpecFlow 'Getting Started' 指南为 NUnit 添加了 SpecFlow。

标签: c# .net specflow


【解决方案1】:

你想要一个Test Fixture

来自Wikipedia

在软件测试中,测试夹具是被测软件的固定状态,用作运行测试的基线;也称为测试上下文。它也可以指为了使系统进入这种状态而执行的操作。

夹具示例:

  • 使用特定的已知数据集加载数据库
  • 擦除硬盘并安装已知干净的操作系统安装
  • 复制一组特定的已知文件
  • 输入数据的准备和假或模拟对象的设置/创建

用于对被测软件系统地运行可重现测试的软件称为测试工具;它的部分工作是设置合适的测试夹具。

针对您的具体问题:

  1. 在您的 SpecFlow 测试项目中创建一个 Fixtures 目录。在其中根据您的测试创建任意数量的子目录,以设置您需要的目录和文件结构。

  2. 在 App.config 条目中添加 <appSettings> 定义所有测试装置的根文件夹

    <configuration>
      ...
      <appSettings>
        <!-- Path relative to the build output directory -->
        <add name="FixturesRootDirectory" value="..\..\Fixtures" />
      </appSettings>
      ...
    </configuration>
    
  3. [BeforeScenario]钩子中,设置当前场景上下文中fixtures目录的绝对路径(参考:How do I get the path of the assembly the code is in?

    using System.Configuration;
    using System.IO;
    using TechTalk.SpecFlow;
    
    namespace Foo
    {
        [Binding]
        public class CommonHooks
        {
            [BeforeScenario]
            public void BeforeScenario()
            {
                InitFixturesPath();
            }
    
            private void InitFixturesPath()
            {
                if (ScenarioContext.Current.ContainsKey("FixturesPath"))
                    return;
    
                string codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)
                                + Path.DirectorySeparatorChar
                                + ConfigurationManager.AppSettings["FixturesRootDirectory"];
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
    
                ScenarioContext.Current.Set<string>("FixturesPath", Path.GetDirectoryName(path));
            }
        }
    }
    
  4. 现在您可以使用ScenarioContext.Current.Get&lt;string&gt;("FixturesPath") 获取所有灯具的根目录。你甚至可以编写自己的 Fixtures 助手类:

    public static class FixturesHelper
    {
        public static string Path { get; set; }
    
        // other methods and properties making it easier to use fixtures
    }
    

【讨论】:

  • +1。这会起作用,但可能有点矫枉过正。如果测试是 MSTest,那么您可以简单地添加一个与生成的测试同名的部分类,并使用 [DeploymentItem] 属性为这些测试部署文件/文件夹。 NUnit 和 XUnit 可能存在类似的选项。好主意。
  • 我试过这个解决方案,但我似乎无法让它工作。 'codeBase' 变量指向一个 DLL 文件,如下所示:“file:///C:/PancakesDevelopment/Product Builder/ProductBuilder/ProductBuilder.Specs/bin/Release/ProductBuilder.Specs.DLL\\Fixtures” t 似乎是对的。除非我误解或做错了什么。
  • 啊,没错。我需要一些方法来获取 DLL 的基本目录——相当于 Bash 中的dirname /some/file.txt...我会进行一些搜索并更新我的答案。
  • 答案已通过修复更新。我还必须更新 App.config 设置,所以请注意这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多