gpt4 book ai didi

c# - 使用 SpecFlow 测试引用我项目中的文件夹

转载 作者:太空狗 更新时间:2023-10-30 01:18:16 25 4
gpt4 key购买 nike

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

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

最佳答案

你想要一个 Test Fixture .

来自 Wikipedia :

In software testing, a test fixture is a fixed state of the software under test used as a baseline for running tests; also known as the test context. It may also refer to the actions performed in order to bring the system into such a state.

Examples of fixtures:

  • Loading a database with a specific, known set of data
  • Erasing a hard disk and installing a known clean operating system installation
  • Copying a specific known set of files
  • Preparation of input data and set-up/creation of fake or mock objects

Software used to systematically run reproducible tests on a piece of software under test is known as a test harness; part of its job is to set up suitable test fixtures.

针对您的具体问题:

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

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

    <configuration>
    ...
    <appSettings>
    <!-- Path relative to the build output directory -->
    <add name="FixturesRootDirectory" value="..\..\Fixtures" />
    </appSettings>
    ...
    </configuration>
  3. [BeforeScenario] 中hook,设置当前场景上下文中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<string>("FixturesPath")获取所有灯具的根目录。您甚至可以编写自己的 Fixtures 辅助类:

    public static class FixturesHelper
    {
    public static string Path { get; set; }

    // other methods and properties making it easier to use fixtures
    }

关于c# - 使用 SpecFlow 测试引用我项目中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28089508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com