Skip to content

Apartment attribute ignored when .runsettings contains DefaultTimeout #4658

@dd-eg

Description

@dd-eg

I have a test fixure that uses the Apartment attribute to run tests in the STA. Below is a working simplification:

.csproj

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

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

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="NUnit" Version="4.1.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
  </ItemGroup>

</Project>

.cs

using NUnit.Framework;

namespace ApartmentTest
{
    [TestFixture, Apartment(ApartmentState.STA)]
    public class Tests
    {
        [Test]
        public void ApartmentStateShouldBeSTA()
        {
            Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
        }
    }
}

This test passed without issue (via Visual Studio 2022's Test Explorer, vstest.console command line, dotnet test) until I started using a .runsettings file that included the DefaultTimeout property:

.runsettings

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <NUnit>
    <DefaultTimeout>1200000</DefaultTimeout>
  </NUnit>
</RunSettings>

Now, the test fails. Below is the output shown when running dotnet test:

C:\Sandbox\ApartmentTest>dotnet test bin\debug\net8.0\ApartmentTest.dll -s .runsettings
Microsoft (R) Test Execution Command Line Tool Version 17.8.0 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
  Failed ApartmentStateShouldBeSTA [30 ms]
  Error Message:
     Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA))
  Expected: STA
  But was:  MTA

  Stack Trace:
     at ApartmentTest.Tests.ApartmentStateShouldBeSTA() in C:\Sandbox\ApartmentTest\UnitTest1.cs:line 11

1)    at ApartmentTest.Tests.ApartmentStateShouldBeSTA() in C:\Sandbox\ApartmentTest\UnitTest1.cs:line 11



Failed!  - Failed:     1, Passed:     0, Skipped:     0, Total:     1, Duration: 30 ms - ApartmentTest.dll (net8.0)

As a work-around, I will remove DefaultTimeout from the .runsettings file. I like having this property set though, as it helps to handle some tests that hang or otherwise cause our automated builds to run longer than they should.

It seems like this issue might be related to one logged a couple months ago: #4598

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions