Adding Resource Files for SpecFlow Test Runs

Sometimes you want to reference other files in your SpecFlow tests. You could be comparing expected to actual data for tables, testing file upload/download, etc.

 

  1. Add the file to your solution.

  2. Be sure to select Copy if newer for the Copy to Output Directory option. This will ensure that the file is included in the build output.

     

  3. Reference the file using System.AppDomain.CurrentDomain.BaseDirectory.

    [Then(@"the page title matches file contents")] public void ThenThePageTitleMatchesFileContents() { String expectedTitle = File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "Resources/GoogleTitle.txt"); Assert.That(driver.Title, Is.EqualTo(expectedTitle)); }

 

If you get a DirectoryNotFoundException, double check the path and that you’ve added the files to the build output (see Step 2).