用于TestContext.DeploymentDirectory的Azure管道单元测试Uri导致失败

时间:2020-03-10 05:44:56

标签: c# azure-pipelines mstest

我有一个Azure DevOps版本,正在执行一组单元测试。测试在本地通过,但由于使用TestContext.DeploymentDirectory的测试失败,因此在Hosted 2017代理上的构建失败。

失败的测试使用TestContext创建一个模拟设置对象。模拟设置的OutputPath设置为TestContext.DeploymentDirectory值。然后将模拟设置对象传递给构造函数,并使用Assert.AreEqual检查结果对象,如图所示;

[TestMethod]
public void Test_DiskStorageManager()
{
  var settings = new MockDataHandlerSettings(TestContext);
  var manager = new DiskStorageManager(settings);

  Assert.AreEqual(settings.OutputPath, manager.OutputPath);
}

在构造函数中如下;

if (Uri.TryCreate(settings.OutputPath, UriKind.Absolute, out Uri outputUri))
{
  OutputPath = outputUri.LocalPath;
  IsLocalOutput = outputUri.IsLoopback;
}
else
{
  throw new ArgumentException($"Output path is invalid {settings.OutputPath}");
}

测试在本地运行良好,但托管代理上发生以下测试失败;

Assert.AreEqual failed. 
Expected:<D:\a_temp..\Tests\TestResults\TestResults\Deploy_VssAdministrator 2020-03-10 03_09_31\Out>. 
Actual:<D:\a\Tests\TestResults\TestResults\Deploy_VssAdministrator 2020-03-10 03_09_31\Out>.

是什么原因导致从D:\a_temp..\更改为D:\a\并导致测试失败?

编辑

测试任务由以下YAML定义;

variables:
  BuildConfiguration: 'debug'
  BuildPlatform: 'any cpu'

steps:
- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\**\*Tests.dll
     !**\obj\**
    runSettingsFile: '$/MyProject/Tests/.runsettings'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

.runsettings包含

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <RunConfiguration>
        <MaxCpuCount>1</MaxCpuCount>
        <ResultsDirectory>..\Tests\TestResults</ResultsDirectory>
        <TargetPlatform>x64</TargetPlatform>
    </RunConfiguration>
  <DeploymentEnabled>True</DeploymentEnabled>
</RunSettings>

1 个答案:

答案 0 :(得分:0)

由于ResultsDirectory element of the .runsettings file中的相对路径,测试失败。测试用以下内容初始化:

TestResultsDirectory intialized to D:\a\_temp\..\Tests\TestResults\TestResults

删除<ResultsDirectory>..\Tests\TestResults</ResultsDirectory>;

TestResultsDirectory intialized to d:\a\_temp\TestResults

注意: 在失败的测试中显示的期望值未正确转义\。显示的值为D:\a_temp..\Tests,但任务日志中的失败消息为D:\a\_temp\..\Tests

这不能解释为什么相对路径在VS2019 16.4中而不是在Azure管道中完全解决。