Azure Devops 管道测试步骤失败 - 数据文件路径不正确

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

我有一个包含三个解决方案的 Repo。每个解决方案都有多个项目,其中很多是共享的(包括测试项目)。

我有一个沿着以下几行的构建管道

执行所有测试的步骤如下:

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

绝大多数测试运行完美。但是,对于某些测试,我收到奇怪且相当混乱的错误消息:

[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\s\Projects\TestProjectTwo\A\B\TestData\SomeFile.txt'.

所以它当前正在处理 TestProjectOne 但随后说它无法在 TestProjectTwo.[=13= 的路径下找到文件]

测试内代码如下:

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

不用说,使用 Visual Studio 2019 使用 Visual Studio 和 ReSharper 测试 运行ner.

为什么 Azure DevOps 管道会遇到这个问题?

Why would an Azure DevOps pipeline suffer this issue?

那是因为我们在 VS 测试任务中使用了通配符:

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

这将抓取 $(System.DefaultWorkingDirectory) 文件夹中的所有 *test*.dll 文件,包括子文件夹。

很显然,这种方法带来的巨大便利就是我们不必从文件夹中一个一个地抓取*test*.dll但它的一个问题是,由于我们使用通配符,它​​会丢失每个 *test*.dll 文件的完整路径。在这种情况下,如果我们在 *test*.dll 文件中指定相对路径 ..\..\A\B\TestData\,它将无法获得正确的路径,因为当前 *test*.dll 文件丢失了完整路径。

这就是为什么它从 TestProjectOne.A.B.GetSomethingTests 执行测试 dll,但从 TestProjectTwo.

获得路径的原因

要解决此问题,我们可以在 *test*.dll 文件中指定完整路径而不是相对路径。

希望对您有所帮助。