Search code examples
azure-devopsazure-pipelinesazure-pipelines-tasks

Azure Devops Pipeline Test step fails - incorrect path to data files


I have a Repo containing three solutions. Each solution has multiple projects, many of which are shared (including test projects).

I have a build pipeline along the following lines

  • Retrieve NuGet packages
  • Build Solution 1
  • Build Solution 2
  • Build Solution 3
  • Execute all tests

The step to execute all tests is as follows:

- task: VSTest@2
  displayName: 'Test'
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'

The vast majority of tests run perfectly. However, I get the odd and rather confusing error message for some tests:

[error]SetUp failed for test fixture TestProjectOne.A.B.GetSomethingTests

[error]SetUp : System.IO.DirectoryNotFoundException : Could not find a part of the path 'd:\a\1\s\Projects\TestProjectTwo\A\B\TestData\SomeFile.txt'.

So it's currently processing TestProjectOne but then saying it can't find a file under a path for TestProjectTwo.

The code within the test is as follows:

private const string RelativePath = @"..\..\A\B\TestData\";
...
var x = File.ReadAllText(RelativePath + "SomeFile.txt")

Needless to say, this works perfectly using Visual Studio 2019 using both the Visual Studio and ReSharper test runner.

Why would an Azure DevOps pipeline suffer this issue?


Solution

  • Why would an Azure DevOps pipeline suffer this issue?

    That because we use wildcard in the VS test task:

    - task: VSTest@2
      displayName: 'Test'
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: |
          **\*test*.dll
    

    Which will grab all *test*.dll files in the $(System.DefaultWorkingDirectory) folder, including the sub folder.

    Obviously, the great convenience brought by this method is that we do not have to grab the *test*.dll from the folder one by one. But one problem with it is that since we are using wildcards, it will lose the full path of the each *test*.dll file. In thise case, if we specify the relative path ..\..\A\B\TestData\ in the *test*.dll file, it will not get the correct path, because the current *test*.dll file lost its full path.

    That is reason why it execute the test dll from TestProjectOne.A.B.GetSomethingTests, but got the path from TestProjectTwo.

    To resolve this issue, we could specify the full path in the *test*.dll file instead of the relative path.

    Hope this helps.