c#-使用SpecFlow测试引用我的项目中的文件夹
作者:互联网
我试图编写一个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.
对于您的特定问题:
>在您的SpecFlow测试项目中创建一个Fixtures目录.在其中根据您的测试创建任意数量的子目录,以设置所需的目录和文件结构.
>添加一个< appSettings>在App.config条目中,为所有测试夹具定义根文件夹
<configuration>
...
<appSettings>
<!-- Path relative to the build output directory -->
<add name="FixturesRootDirectory" value="..\..\Fixtures" />
</appSettings>
...
</configuration>
>在[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));
}
}
}
>现在,您可以使用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
}
标签:specflow,c,net 来源: https://codeday.me/bug/20191028/1954739.html