0

I read a lot of posts on Stackoverflow (the post is 12 years old) and in different blogs but usually they are very old. I'm using Visual Studio 2022 with Azure DevOps.

In my test project, I want to read the file _json1.json` under the jsontests folder.

enter image description here

In the test code I added the DeploymentItem annotation

[TestMethod]
[DeploymentItem(@"jsontest\json1.json", "jsontest")]
public void LoadJson_SystemTextJson_3Textbox_1radiobutton()
{
    // ...
    var path = @"jsontest\json1.json";
    string jsonFromFile = System.IO.File.ReadAllText(path);
    // ...
}

The property of this file is Copy to Output Directory on Copy Always. Also, in the project I added a file

enter image description here

In the root of the test project I added a .runsettings file

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

I tried to run the pipeline in Azure DevOps but I get always the same error

Test Run deployment issue: Failed to get the file for deployment item 'jsontest\json1.json' (output directory 'jsontest'): System.IO.FileNotFoundException: Could not find file '/home/vsts/work/1/s/PSC.Survey.Shared.Validation.Tests/bin/Release/net6.0/jsontest\json1.json'.

Enrico
  • 3,592
  • 6
  • 45
  • 102
  • 2
    Seems your hosted agent is a linux machine. Did you try to set your deployment item to `jsontest/json1.json` (beware of the slash instead of backslash)? Also take care for case sensivity. While windows doesn't care, in Linux this can also lead to such errors (but doesn't seem the problem in this case). – Oliver Aug 20 '22 at 20:46
  • Also I never used `DeploymentItem`. Did you try only `Copy to output directory` without deployment item? As a last resort you could add a resource file to your test project, add your file to the resource and load the content from there instead from file system. – Oliver Aug 20 '22 at 20:49
  • @Oliver you are totally right I haven't consider that. Thank you so much. I ran more that 40 tests.... – Enrico Aug 20 '22 at 20:58
  • No problem. Had similar issues in the past. Since then I always use `Path.Combine()` at runtime or use resource files for static content. I think this question can be closed. – Oliver Aug 21 '22 at 11:28

1 Answers1

2

From you issue output:

Could not find file '/home/vsts/work/1/s/PSC.Survey.Shared.Validation.Tests/bin/Release/net6.0/jsontest\json1.json

enter image description here

As Oliver mentioned, you are based on Linux machine. So please change the path, you should use slash not backslash. Otherwise, the internal structure is inconsistent.

The default agent of YAML definition on Azure DevOps is 'ubuntu-latest', it is a Linux agent. if you want to use windows environment(windows agent), your YAML definition should be like this:

trigger:
- none

#Use the Windows agent here.
pool:
  vmImage: windows-latest

steps:
- script: |
    echo Add other tasks to build, test, and deploy your project.
  displayName: 'Run a multi-line script'

All of the Microsoft Host agent:

https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#software

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10