อยู่ๆ Test Report จาก dotnet test ก็หาย

เรื่องของเรื่องคือว่าอยู่ๆ HTML Test Report ที่มาจาก dotnet test ก็หายไป แต่ตัว Jenkins มันแจ้งว่า Test Fail แต่ Report สรุป ดันบอกว่า 0 fail ซะงั้น (อันนี้เขียน Helper เองนะรวมเองนะ ว่าจะย้ายไปใช้ AllureReport แต่ยังไม่ Support MSTest สักที 55)

อ้าวมันเกิดอะไรขึ้น

  • ปกติแล้วตัว HTML Test Report จะอยู่ใน Folder TestResults เดียวกับ . csproj ของ Project Test
ds.invs.service.impl.MSTest
|-> ds.invs.service.impl.MSTest.csproj
|-> TestResults 
     |-> htmlResults_net8.0_<<your_timestamp>>
  • แต่ Test Project ที่เกิดปัญหามันมาจาก Folder TestResults ดันย้ายไปอยู่ใน Folder bin
ds.invs.service.impl.MSTest
|-> bin
    |-> Debug
        |-> net8.0
            |-> TestResults 
                |-> htmlResults_net8.0_<<your_timestamp>>

มันเลยทำให้ Script ที่เขียนมันกวาดไม่เจอครับ เลยเป็นที่มาตัว Jenkins มันแจ้งว่า Test Fail แต่ Report สรุป ดันบอกว่า 0 fail เพราะมันหาไฟล์ report ของ project ที่ fail ไป merge ไม่เจอครับ

สาเหตุ

เวลาเราสร้าง Test Project ใหม่ ด้วย Visual Studio 2022 (17.12.xx) ใน cs project มันจะแถมของมาด้วย และปรับ Default Lib ด้วย

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!-- Enable the MSTest runner, this is an opt-in feature -->
    <EnableMSTestRunner>true</EnableMSTestRunner>
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!--
      Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
      For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
      -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

    <OutputType>Exe</OutputType>

    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <!--
      ของเดิมจะแยก 
     <PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
     <PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
    -->
    <PackageReference Include="MSTest" Version="3.8.0" />

    <!--
      Coverlet collector isn't compatible with MSTest runner, you can
      either switch to Microsoft CodeCoverage (as shown below),
      or switch to be using coverlet global tool
      https://github.com/coverlet-coverage/coverlet#net-global-tool-guide-suffers-from-possible-known-issue
    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage"
                      Version="17.10.1" />
    -->
    <PackageReference Include="coverlet.collector" Version="6.0.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

จากตรงนี้พบว่า เค้าเพิ่ม EnableMSTestRunner / OutputType

ผมสงสัยตัวนี้แหละ ที่ทำให้ Test Result มันไปโผล่ใน bin หลังจากลอง Comment ออก เออใช่จริงๆด้วยตัว EnableMSTestRunner กับ OutputType เป็นสาเหตุที่ Test Result อยู่ผิดที่ผิดทาง

แต่ Test รันได้นะ แต่ Coverage ไม่ออกนะ มันออกแหละ แต่เป็นของ Microsoft.Testing.Extensions.CodeCoverage ซึ่งจะต่างกับส่วนใหญ่ที่ใช้ coverlet.collector พอไปดูใน Sonar เลย Result ไม่ขึ้น ก็เลยปรับให้กลับมาใช้ coverlet.collector ก่อนครับ

หลังจากรู้ที่มาแล้ว ลองไปอ่าน Doc เค้ามี MSTestV3 แล้วนะ น่าจะมาตอนปลายปีที่แล้ว เราใช้ตั้งแต่ V1 > V2 มาเลย 56555 ต้องไปอ่านต่อก่อน ว่าคุ้มจะเปลี่ยนไหม MSTest v3 migration guide

ว่าแต่ยังมีใครใช้ MSTest ไหมนะ ?


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.