1

I have some SpecFlow tests in a "SpecFlowTests" project in VS to run and they need some test data files. I was using a .testsettings file to deploy the files and it worked. However, the new MsTest agent does not support .testsettings anymore so I need to find another solution to deploy the files.

The test data files have the "Copy to Output Directory" set to "Copy Always". They are copied in the output directory and I can reference them in the code by getting the output directory in the following way:

public static string AssemblyLocation()
        {
            var assembly = Assembly.GetExecutingAssembly();
            var codebase = new Uri(assembly.CodeBase);
            var path = codebase.LocalPath;
            return path;
        }

This approach works when running the tests locally or when running them as part of the build from TFS, it looks for the files in the following location:

...\SpecFlowTests\bin\Debug\Helpers\TestDataFile.txt

However, when trying to run them as part of the release in TFS, it looks for the test data files in the Out folder of the TestResults directory and it cannot find them there.

C:\buildagent1\_work\r1\a\TestResults\Out\Helpers\TestDataFile.txt

I have then tried to set a DeploymentItem attribute on the BeforeFeature method in the tests, but it looks like it does not work, it should deploy the test data files to the Out folder in TestResults, but it does not.

So I need a common solution to work in all three situations (run tests locally, run them from TFS build, run them from TFS release).

I am relatively new at this, so any advice is appreciated.

Thanks!

cris
  • 171
  • 3
  • 14
  • Which version of TFS do you use? How did you run the them in release? Just set the the build definition as the artifacts? In TFS 2017, you can try to set the Source (repository) directly as the Artifacts source in Release, then check if that works. – Andy Li-MSFT Aug 31 '17 at 08:00
  • TFS 2017.Yes, the release definition has two build definitions as artifact source.The build definition contains an environment with a task for running the SpecFlow tests on the agent.However, when the tests are run, it tries to look for the test data in `C:\buildagent1\_work\r1\a\TestResults\Deploy_SerTFSBUILD 2017-09-04 20_01_06\Out\Hekpers\TestDataFile.txt`, but there is nothing deployed there.I tried to add a "Copy Files" task before the task of running the tests, but I don't know what to select as target folder because the "TestResults\Deploy_SerTFSBUILD.." folder is created when tests run – cris Sep 04 '17 at 18:45
  • When I could use a ".testsettings" file, I just selected what files I needed to be deployed and, when the tests run, the files were deployed to this "TestResults\Deploy...\out" folder automatically. – cris Sep 04 '17 at 18:46

1 Answers1

1

[DeploymentItem] attributes do not work on the BeforeFeature method. They have to put on the real testmethod/testclass.

Since SpecFlow 2.2 you can specify the deploymentitems in your feature files to get the attributes generated at the correct location. Put an @MsTest:DeploymentItem:Helpers\TestDataFile.txt to your feature.
Examples from our test suite: https://github.com/SpecFlowOSS/SpecFlow/blob/master/Tests/TechTalk.SpecFlow.Specs/Features/UnitTestProviderSpecific/MsTest/DeploymentItem.feature

Andreas Willich
  • 5,665
  • 3
  • 15
  • 22