Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue] [AssemblyInitialize], [AssemblyCleanup], [ClassInitialize] and [ClassCleanup] are not listed as individual entries in the VS Test Explorer and TRX log #3785

Closed
Vincezhaoc opened this issue Sep 9, 2024 · 8 comments

Comments

@Vincezhaoc
Copy link

Vincezhaoc commented Sep 9, 2024

Describe the bug

From change log and PR 2904 change log 3.5.0 and #2904. I am aware of MSTest should support [AssemblyInitialize], [AssemblyCleanup], [ClassInitialize] and [ClassCleanup] as separate entries in Visual Studio's Test Explorer and in TRX log. However, after upgrading MSTest.TestFramework, MSTest.TestAdapter and MSTest.Sdk to version 3.5.2, my local Visual Studio (Microsoft Visual Studio Enterprise 2022 (64-bit) - Current; Version 17.10.6) Test Explorer still only displays the test methods and not the four entries mentioned.

IDE information

Microsoft Visual Studio Enterprise 2022
Version 17.11.2
VisualStudio.17.Release/17.11.2+35222.181
Microsoft .NET Framework
Version 4.8.09032

Installed Version: Enterprise

Steps To Reproduce

csproj

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

	<PropertyGroup>
		<ImplicitUsings>disable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<TargetFrameworks>net8.0;net472</TargetFrameworks>
		<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
		<IsPackable>false</IsPackable>
		<OutputType>Library</OutputType>
	</PropertyGroup>

	<!-- Set C# 8.0 for net472 to support Nullable -->
	<PropertyGroup Condition="'$(TargetFramework)' == 'net472'">
		<LangVersion>8.0</LangVersion>
	</PropertyGroup>

	<ItemGroup>
		<!--<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />-->
		<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
		<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
		<PackageReference Include="MSTest.Sdk" Version="3.5.2" />
		<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
		<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
		<PackageReference Include="MSTest.Analyzers" Version="3.5.2" />
		<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.3.2" />
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>

</Project>

UnitTest1.cs

namespace mstestfeatures_demo
{

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;

    [TestClass]
    public class UnitTest1
    {

        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            Console.WriteLine("Library init!");
        }

        [ClassInitialize]
        public static void ClassInit(TestContext context)
        {
            Console.WriteLine("Class UnitTest1 init!");
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            Console.WriteLine("Class UnitTest1 do cleaning!");
            throw new Exception("Class UnitTest1 Cleanup failed.");
        }

        [TestMethod]
        public void TestMethod1()
        {
            Console.WriteLine("This test method1");
        }

        [TestMethod]
        public void TestMethod2()
        {
            Console.WriteLine("This test method2");
        }
    }
}

Expected behavior

  • [AssemblyInitialize], [AssemblyCleanup], [ClassInitialize] and [ClassCleanup] could be listed as individual entries in VS log and TRX log as mentioned from:
  • PR1698
  • PR2904
  • change log 3.5.0

Actual behavior

image
image
testResults_net472_20240909111456.trx.txt

Additional context

Purpose for finding this feature.

If an exception is thrown from [AssemblyInitialize], [AssemblyCleanup], [ClassInitialize] and [ClassCleanup], it will be bound to the test methods' information view. I am trying to find any features support by MSTest to mark the four attributes as separate entries.

@Evangelink
Copy link
Member

Hi @Vincezhaoc,

Thanks for your interest! I have indeed not done my job correcty there and we are missing some proper documentation for this feature.

At the moment, this feature is disabled by default and needs to be enabled through runsettings using <ConsiderFixturesAsSpecialTests>true</ConsiderFixturesAsSpecialTests>.

I am keeping the ticket open to know that we need to fix the documentation.

@Vincezhaoc
Copy link
Author

Vincezhaoc commented Sep 10, 2024

Hi @Evangelink ,

Thanks for the prompt reply!

[AssemblyInitialize], [AssemblyCleanup], [ClassInitialize] and [ClassCleanup] can display in vs Test Explorer and the .trx log after add <ConsiderFixturesAsSpecialTests>true</ConsiderFixturesAsSpecialTests> inside <MSTest></MSTest label section.

This setting effects

.runsettings

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <!-- Configurations that affect the Test Framework -->
  <RunConfiguration>
    <!-- Use 0 for maximum process-level parallelization. This does not force parallelization within
    the test DLL (on the thread-level). You can also change it from the Test menu; choose "Run tests
    in parallel". Unchecked = 1 (only 1), checked = 0 (max). -->
    <MaxCpuCount>1</MaxCpuCount>
    <!-- Path relative to directory that contains .runsettings file-->
    <ResultsDirectory>.\TestResults</ResultsDirectory>
    <!-- true or false -->
    <!-- Value that specifies the exit code when no tests are discovered -->
    <TreatNoTestsAsError>true</TreatNoTestsAsError>
  </RunConfiguration>


  <!-- Adapter Specific sections -->

  <!-- MSTest adapter -->
  <MSTest>
    <TreatClassAndAssemblyCleanupWarningsAsErrors>true</TreatClassAndAssemblyCleanupWarningsAsErrors>
    <ConsiderFixturesAsSpecialTests>true</ConsiderFixturesAsSpecialTests>
    <AssemblyResolution>

    </AssemblyResolution>
  </MSTest>

</RunSettings>

Test Explorer

image

TRX Log

image

Addition

Here I kindly forked from MicrosoftDocs and add some content in my forked repo Vincezhaoc/visualstudio-docs/configure-unit-tests-by-using-a-dot-runsettings-file.md. May I know if it is appropriate to raise a PR and merge the additonal content into MicrosoftDocs?

Please kindly let me know. 😄

Thanks.

@Evangelink
Copy link
Member

Yes please do file a PR! I am slowly trying to move all MSTest doc over to this doc https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-configure#mstest-element. Would you mind also fixing it there?

Feel free to ping me on both the doc PR for review.

@Vincezhaoc
Copy link
Author

Vincezhaoc commented Sep 11, 2024

Hi @Evangelink,

Here, I've created two PRs to update the usage instructions for ConsiderFixturesAsSpecialTests

Here's the breakdown:

Please let me know if there are any mistakes.

Addition

Ping me additionally to assign some common tasks, as I will be using the MSTest framework extensively in both my learning and work, if you're open to it.

Feel free to reach out.

Thank you!

@Evangelink
Copy link
Member

Thank you so much @Vincezhaoc! I have approved with a small suggestion.

@Vincezhaoc
Copy link
Author

Hi @Evangelink ,

I have updated the content based on the suggestions.

Thanks! 👍

@Evangelink
Copy link
Member

I'll close this ticket, the doc team will review and merge your PR soon. Note that there is a bit of delay (few days) between the moment your PR is merged and the public website being updated.

@Vincezhaoc Vincezhaoc removed their assignment Sep 11, 2024
@Vincezhaoc
Copy link
Author

Many thanks! I accidentally removed the assignment by mistake.

It's great to join this awesome repo.

My co-workers will know this features from official documentation soon. 😄

Appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants