我通过VisualStudio2019创建了一个C# xUnit测试项目,并使用Selenium进行UI测试。这个'xUnit测试项目‘似乎创建了一个.netcore应用程序。
已安装的软件包(每个包的最新版本):
(2.4.1)
当通过Visual运行测试时,我在中获得了很好的输出,但没有得到任何XML输出。我尝试通过命令行运行并指定“-logger”,但这给了我一个XML (trx,重命名为xml)文件,它看起来与我所期望的标准xUnit v2 XML结构完全不同。
我希望获得标准的xUnit v2 XML输出,这样就可以将它导入到其他CI或测试管理工具(如Jenkins、xray等)中,或者为更高级的v2输出编写自定义XSL样式表。
我试着安装'xunit.runner.console‘,但是'xunit.console.exe’拒绝运行。在这里,我的工作笔记本电脑上可能有一些安全策略用于exe文件,因为xunit.console.exe似乎位于其他地方,离我的项目文件很远(我在过去创建的MVC项目中有项目包,.netcore似乎把它们放在了其他地方?)
跑步:
dotnet test --logger "trx;LogFileName=TestOutputResults.xml"或
dotnet test --filter FullyQualifiedName~XUnitTestProject1.TestClass1.Test1 --logger "trx;LogFileName=TestOutputResults.xml"结果显示了以下格式化的XML文件结构:
<?xml version="1.0" encoding="utf-8"?>
<TestRun id="" name="" runUser="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Times creation="" queuing="" start="" finish="" />
<TestSettings name="default" id="">
<Deployment runDeploymentRoot="" />
</TestSettings>
<Results>
<UnitTestResult executionId="" testId="" testName="" computerName="" duration="" startTime="" endTime="" testType="" outcome="Passed" testListId="" relativeResultsDirectory="">
<Output>
<StdOut>Info: Randomly generated test data</StdOut>
</Output>
</UnitTestResult>
</Results>
<TestDefinitions>
<UnitTest name="" storage="" id="">
<Execution id="" />
<TestMethod codeBase="" adapterTypeName="" className="" name="" />
</UnitTest>
</TestDefinitions>
<TestEntries>
<TestEntry testId="" executionId="" testListId="" />
</TestEntries>
<TestLists>
<TestList name="Results Not in a List" id="" />
<TestList name="All Loaded Results" id="" />
</TestLists>
<ResultSummary outcome="Completed">
<Counters total="1" executed="1" passed="1" failed="0" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
<Output>
<StdOut></StdOut>
</Output>
</ResultSummary>
</TestRun>但是,我可以在网上找到的标准xUnit v2格式(https://xunit.net/docs/format-xml-v2 & https://docs.getxray.app/display/XRAY/Taking+advantage+of+xUnit+XML+reports)是:
<?xml version="1.0" encoding="utf-8"?>
<assemblies timestamp="07/31/2018 14:58:48">
<assembly name="C:\Users\smsf\Documents\Visual Studio 2015\Projects\xUnitDemo\xUnitDemo\bin\Debug\xUnitDemo.DLL" environment="64-bit .NET 4.0.30319.42000 [collection-per-class, parallel (1 threads)]" test-framework="xUnit.net 2.3.1.3858" run-date="2018-07-31" run-time="14:58:47" config-file="C:\Users\smsf\Documents\Visual Studio 2015\Projects\xUnitDemo\packages\xunit.runner.console.2.4.0\tools\net452\xunit.console.exe.Config" total="15" passed="14" failed="1" skipped="0" time="0.257" errors="0">
<errors />
<collection total="2" passed="1" failed="1" skipped="0" name="Test collection for xUnitDemo.SimpleTests" time="0.070">
<test name="xUnitDemo.SimpleTests.PassingTest" type="xUnitDemo.SimpleTests" method="PassingTest" time="0.0636741" result="Pass">
<traits>
<trait name="test" value="CALC-2" />
<trait name="requirement" value="CALC-1" />
<trait name="labels" value="core UI" />
</traits>
</test>
<test name="xUnitDemo.SimpleTests.FailingTest" type="xUnitDemo.SimpleTests" method="FailingTest" time="0.0059474" result="Fail">
<failure exception-type="Xunit.Sdk.EqualException">
<message><![CDATA[Assert.Equal() Failure\r\nExpected: 5\r\nActual: 4]]></message>
<stack-trace><![CDATA[ at xUnitDemo.SimpleTests.FailingTest() in C:\Users\smsf\documents\visual studio 2015\Projects\xUnitDemo\xUnitDemo\SimpleTests.cs:line 30]]></stack-trace>
</failure>
</test>
</collection>
</assembly>
</assemblies>有人能解释我做错了什么吗,或者如果我太蠢了,错过了一些显而易见的事情?
我需要做什么/运行什么命令才能用测试结果生成标准的xUnit v2 XML文件?
我花了几天时间使用Google,DuckDuckGo,查看xunit.net和github代码,试图找出这一点,却找不到任何东西,这让我发疯了。
在此之前,非常感谢您。
发布于 2021-08-29 20:31:46
对于任何遇到这个问题的人,我都想通了。仍然不知道为什么dotnet测试会提供不同的输出,但是现在我可以获得标准的xUnit v2 XML格式化输出。
正如我在最初的问题中所说的,尽管在网上搜索了很多,但我还是找不到答案。
然而,问题是多方面的。
首先,工作中的安全策略意味着xunit.console.exe被阻止运行。我通过设置一个新的个人测试机器并在它上运行一个示例项目来发现这一点,它运行得很好。
一旦我能够运行这个,尝试运行我的测试给我一个".....System.IO.FileNotFoundException:无法加载文件或程序集……“错误。这让我想到了https://github.com/xunit/xunit/issues/1950,bradwilson在那里证实,尽管nuget文件夹中有一些文件夹,但xunit.console并不能与.net核心一起工作。它是现在废弃的'dotnet‘命令的剩余部分,但“应.NET团队的请求”留在包中。这就解释了为什么xunit.runner.console nuget包的核心文件夹中没有xunit.runner.console文件,而且,正如我曾经尝试过的那样,运行最后一个.net框架4.7.2 xunit.console.exe不能与.net核心测试项目一起工作。
因此,我使用与以前相同的名称空间创建了一个新的xUnit测试项目,将目标框架设置为.net 5,然后按照关于https://xunit.net/docs/getting-started/netfx/cmdline的说明替换.csproj文件内容以匹配:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference
Include="Microsoft.NETFramework.ReferenceAssemblies"
Version="1.0.0"
Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>然后,我将所有测试、页面对象模型等复制到新项目(因为名称空间相同),安装了所需的所有附加nuget包,并再次运行命令:
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe bin/Debug/net48/SampleProject.UITests.dll -xml "SampleProject.UITests.xml" -html "SampleProject.UITests.html"并得到了我需要的标准xUnit v2 XML输出。
我的下一步是遵循关于“设置单元测试MSBuild目标”的说明,因为我知道我们为MSTest使用的CI管道可以运行'dotnet……‘命令很好。
https://stackoverflow.com/questions/68928327
复制相似问题