1

概要

Azure PipelinesのVSTestで実行する単体テストで、テストコード内で指定している相対パスのファイルにアクセスできません
Pipelinesのyamlファイルをどのように編集すれば相対パスでアクセスできるようになりますでしょうか?

詳細

Azure DevOpsで画像ファイルを使用した単体テストコードを作成中です
画像ファイルはGit LFSを利用してすでにAzure DevOpsにアップロードしており、Reposの画面から確認することが可能です

テストコードは下記の通りです

TEST(OpenCVTest, Empty) {
    cv::Mat img = cv::imread("..\\..\\images\\sample.bmp");
    bool result = img.empty();
    EXPECT_FALSE(result);
}

これをAzure Pipelinesで実行するために作成したyamlファイルの全体は下記の通りです

ローカルのVSで実行するとテストが成功しますが、Azure Pipelinesで実行すると失敗します

# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'x64'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: 'f39678c2-38da-4e06-831f-137e31f17251'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'


- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.exe
      !**\*TestAdapter.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

下記がAzure Pipelinesで確認できるログです

X OpenCVTest.Empty [1ms]
  Error Message:
Value of: result
  Actual: true
Expected: false
  Stack Trace:
  at test.cpp:14 in D:\a\1\s\Sample-Test1\test.cpp:line 14

試したこと

テストコード中に画像のサイズを出力する処理を挿入する

自作のOpenCVNuGetパッケージが適切に呼ばれていない可能性を考慮して下記のテストコードを作成しました

TEST(OpenCVTest, Empty) {
    cv::Mat img = cv::imread("..\\..\\images\\sample.bmp");
    bool result = img.empty();
    std::cout<<img.size()<<std::endl;
    EXPECT_FALSE(result);
}

ローカル環境上での結果は下記の通りでした

[----------] 1 test from OpenCVTest
[ RUN      ] OpenCVTest.Empty
[1280 x 640]
[       OK ] OpenCVTest.Empty (18 ms)
[----------] 1 test from OpenCVTest (24 ms total)

Azure Pipelines上での結果は下記のとおりでした

X OpenCVTest.Empty [1ms]
  Error Message:
   [0 x 0]
Value of: result
  Actual: true
Expected: false

このことからOpenCVのメソッドは適切に呼ばれていると判断しました

searchFolderにより深いフォルダを指定してみる

参考サイトにパスの指定方法に関する記述がありましたので、絶対パスで指定しました

- task: VSTest@2
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: |
          **\*test*.exe
          !**\*TestAdapter.dll
        searchFolder: '$(System.DefaultWorkingDirectory)\$(buildPlatform)\$(buildConfiguration)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'

テストが実行されませんでした
その時のログは下記の通りです

A total of 1 test files matched the specified pattern.
No test is available in D:\a\1\s\x64\Release\Sample-Test1.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
Results File: D:\a\_temp\TestResults\VssAdministrator_WIN-DVJJ2EGUBGP_2020-06-13_12_12_52.trx

フォルダの階層をAzure Pipelines上で表示してみる

dirコマンドを相対パスで使用して正しいフォルダが指定できているかどうか確認しました

- task: CmdLine@2
  inputs:
    script: 'dir $(System.DefaultWorkingDirectory)\$(buildPlatform)\$(buildConfiguration)\..\..\'

結果は下記の通りで、画像を保存しているフォルダが表示されていることを確認できました

06/13/2020  11:50 AM    <DIR>          .
06/13/2020  11:50 AM    <DIR>          ..
06/13/2020  11:50 AM             2,623 .gitattributes
06/13/2020  11:50 AM             6,084 .gitignore
06/13/2020  11:50 AM             1,221 azure-pipelines.yml
06/13/2020  11:50 AM                68 header.cpp
06/13/2020  11:50 AM               121 header.h
06/13/2020  11:50 AM    <DIR>          images ← 画像フォルダ
...

1 件の回答 1

0

自己解決しました
こちらのサイトに類似した質問があり、yamlにgit lfsを有効にする処理が必要でした

最終的に完成したyamlファイルは以下の通りです

# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'x64'
  buildConfiguration: 'Release'

# ここから
steps:
- checkout: self
  lfs: true
# ここまでを追加

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: 'f39678c2-38da-4e06-831f-137e31f17251'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'


- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.exe
      !**\*TestAdapter.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。