liveBook · Manning

Test file system

As explained in Effective Behavior-Driven Development

A pattern that centralizes file paths and abstracts file-system interactions for test input, output, and temporary files.

The Test File System pattern puts file-path calculation behind a helper, so test automation can handle input, output, and temporary files without embedding environment-specific path logic. section 21.3

When cleanup misses the working directory

A high-privilege test run wrote files under C:\Windows\System32, outside the cleanup job’s watched location; accumulated files eventually filled the server disk. section 21.3

Listing 21.6 Helper class to access test file system details
*** Support/TestFileSystem.cs ***
public class TestFileSystem(IFeatureContext featureContext, 
    IScenarioContext scenarioContext)    #A
{
  public static readonly string TestRunTimestamp =    #B
      ToPath(DateTime.Now.ToString("s", CultureInfo.InvariantCulture));

  public string OutputFolder =>    #C
      EnsureFolderExists(
        Path.Combine(Directory.GetCurrentDirectory(), TestRunTimestamp));

  public string InputFolder =>    #D
      AppContext.BaseDirectory;

  public string FeatureInputFolder =>    #E
      Path.Combine(InputFolder, featureContext.FeatureInfo.FolderPath);

  public string TempFolder =>    #F
      EnsureFolderExists(
        Path.Combine(Path.GetTempPath(), "WIMP", TestRunTimestamp));

  public string GetScenarioSpecificFileName(    #G
      string extension = "")
  {
    string featureAsPath = ToPath(featureContext.FeatureInfo.Title);
    string scenarioAsPath = ToPath(scenarioContext.ScenarioInfo.Title);
    string baseFileName = $"{featureAsPath}_{scenarioAsPath}";
    if (scenarioContext.ScenarioInfo.Arguments is { Count: > 0 })
    {
      foreach (DictionaryEntry entry in 
               scenarioContext.ScenarioInfo.Arguments)
      {
        baseFileName += $"_{entry.Key}-{entry.Value}";
      }
    }
    return baseFileName + extension;
  }

  public static string ToPath(string s)    #H
  {
    ...
  }
  public static string EnsureFolderExists(string path)    #I
  {
    ...
  }
}

The helper exposes input, feature-input, output, and temporary folders; automation can receive it through scenario-context dependency injection. section 21.3

How Effective Behavior-Driven Development covers it

How deep it goes
Explains path calculation as an abstraction and shows sample input and output use.
Examples
Uses a high-privilege run that filled a server disk, plus sales CSV input and error-log output examples.
What it assumes
Assumes test automation can obtain helpers through dependency injection from a scenario context.
What you can do after
Apply the pattern by injecting a helper that supplies environment-appropriate input, output, and temporary-file paths.
How it connects
Connects path handling to dependency injection and the Shareable Scenario Context pattern, and cautions about real file-system behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.