[英]Consuming files in UnitTests running in Azure DevOps Pipeline
I am running a collection of unit tests in an Azure DevOps Pipeline.我正在 Azure DevOps 管道中运行一组单元测试。 The unit testing framework in use is MSTest.
使用的单元测试框架是MSTest。
Some unit tests load files that are inside the repo.一些单元测试加载存储库中的文件。 Therefore the Property
Copy to Output Directory
is set to Copy Always
.因此,属性
Copy to Output Directory
设置为Copy Always
。
Down below you can see the property being set inside the csproj
file.在下方,您可以看到在
csproj
文件中设置的属性。
<Project Sdk="Microsoft.NET.Sdk">
<!-- shortened csproj file -->
<ItemGroup>
<None Update="TestFiles\sample.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Inside the test, the file gets loaded as seen in the next code block.在测试中,文件被加载,如下一个代码块所示。
var path = Path.Join("TestFiles", "sample.xml");
var fileStream = File.OpenRead(path);
I set the
verbosity
todetailed
in order to see if the file gets copied or not.我将
verbosity
设置为detailed
,以查看文件是否被复制。
Running do.net test.\Project\Project.csproj --logger trx --verbosity detailed
locally, everything works as expected with the test results.运行
do.net test.\Project\Project.csproj --logger trx --verbosity detailed
locally,一切都按预期工作,测试结果。
Same applies for running the tests in Visual Studio.这同样适用于在 Visual Studio 中运行测试。
I execute the tests inside an Azure DevOps Pipeline with the DotNetCoreCLI
task.我使用
DotNetCoreCLI
任务在 Azure DevOps 管道内执行测试。
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: DotNetCoreCLI@2
displayName: Run Unit Tests
inputs:
command: test
projects: Project/Project.csproj
arguments: --verbosity detailed
I extracted the generated command from the pipeline logs: /usr/bin/do.net test /home/vsts/work/1/s/Project/Project.csproj --logger trx --results-directory /home/vsts/work/_temp --verbosity detailed
.我从管道日志中提取了生成的命令:
/usr/bin/do.net test /home/vsts/work/1/s/Project/Project.csproj --logger trx --results-directory /home/vsts/work/_temp --verbosity detailed
。 I don't see any significant differences between my local command compared to the one being generated by the pipeline.与管道生成的命令相比,我的本地命令之间没有任何显着差异。
Indeed inspecting the local and Azure DevOps Pipeline logs, the file gets copied as expected.实际上检查本地和Azure DevOps 管道日志,文件按预期被复制。
Copying file from "/home/vsts/work/1/s/Project/TestFiles/sample.xml" to "/home/vsts/work/1/s/Project/bin/Debug/net6.0/TestFiles/sample.xml".
The path matches the one from the MSBuild log /home/vsts/work/1/s/Project/bin/Debug.net6.0/TestFiles/sample.xml
.该路径与 MSBuild 日志
/home/vsts/work/1/s/Project/bin/Debug.net6.0/TestFiles/sample.xml
中的路径匹配。 Then again, I get a FileNotFoundException
:再一次,我得到一个
FileNotFoundException
:
System.IO.FileNotFoundException: Could not find file '/home/vsts/work/1/s/Project/bin/Debug/net6.0/TestFiles/sample.xml'.
I tried to list all files inside the output directory adding the continueOnError
key to the test task.我试图列出 output 目录中的所有文件,将
continueOnError
键添加到测试任务中。
# ...
tasks:
# ...
- script: ls -la /home/vsts/work/1/s/Project/bin/Debug/net6.0
- script: ls -la /home/vsts/work/1/s/Project/bin/Debug/net6.0/TestFiles
The first script action already fails with the following error message:第一个脚本操作已经失败并显示以下错误消息:
ls: cannot access '/home/vsts/work/1/s/Project/bin/Debug/net6.0': No such file or directory
At this point I'm just lost and don't know what to do about this seemingly simple problem... Any suggestions how I get the files containing test data to the unit test?
在这一点上,我只是迷路了,不知道如何处理这个看似简单的问题......关于如何将包含测试数据的文件获取到单元测试的任何建议? Where is my mistake?
我的错误在哪里?
I tried to switch to xUnit.我试图切换到 xUnit。 But it gave me the same
FileNotFoundException
as before.但它给了我和以前一样的
FileNotFoundException
。 I don't think it's a problem with the test itself but with the Azure DevOps Pipeline environment.我认为这不是测试本身的问题,而是 Azure DevOps Pipeline 环境的问题。
After some further debugging of the pipeline, I found a mistake I caused myself.在进一步调试管道后,我发现了一个我自己造成的错误。
When debugging the pipeline I used the - script: ls -la /home/vsts/work/1/s/Project/...
task.在调试管道时,我使用了
- script: ls -la /home/vsts/work/1/s/Project/...
任务。 The path I specified was the one from this post (which I abstracted from the real thing cuz its from work yk) NOT the actual one of my test project.我指定的路径是这篇文章中的路径(我从真实的东西中抽象出来,因为它来自工作 yk)而不是我的测试项目的实际路径。
Listing a non existing directory does not work for obvious reasons...列出一个不存在的目录是行不通的,原因很明显……
Comparing the paths from the MSBuild log message and the actual test run showed that they were almost identical.比较 MSBuild 日志消息中的路径和实际测试运行表明它们几乎相同。 The actual file had a lowercase extension and the file name inside the test project was uppercase.
实际文件的扩展名是小写的,而测试项目中的文件名是大写的。
Running the pipeline in the ubuntu-latest
environment means that we are working with a case sensitive file system (unlike on my local machine which is a Windows 11 machine).在
ubuntu-latest
环境中运行管道意味着我们正在使用区分大小写的文件系统(与我的本地机器 Windows 11 机器不同)。
tl;dr - Everything was correct, I just got to match the actual file name and file path inside the consuming test 100% correctly or run it in a windows environment.
tl;dr - 一切都是正确的,我只是必须 100% 正确地匹配消费测试中的实际文件名和文件路径,或者在 windows 环境中运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.