Skip to content

Commit 3632fdb

Browse files
authored
Fix #436 Flaky test: UnitsNet.Tests.UnitSystemTests.PositiveInfinityFormatting (#438)
* Fix #436 Flaky test: UnitsNet.Tests.UnitSystemTests.PositiveInfinityFormatting Fixes #436 Added `UnitSystemFixture` class which is a `CollectionDefinition` class used to group test classes: 1. that rely on manipulating CultureInfo. See #436 2. to avoid accessing static prop DefaultToString in parallel from multiple tests: a. UnitSystemTests.DefaultToStringFormatting() b. LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit() Changes in this PR: - `UnitSystemTests.GetDefaultAbbreviationFallsBackToUsEnglishCulture`: Set CultureInfo properties back to their original values. - Specify `InvariantCulture` on the following `UnitSystemTests` tests: 1. `NegativeInfinityFormatting` 2. `PositiveInfinityFormatting` - Applied `[Collection(nameof(UnitSystemFixture)]` attribute to test classes that: - Sets `UnitSystem.DefaultCulture` - Originally uses `[Collection("DefaulToString")]` that also depends on `CultureInfo` - Only one `Collection` attribute per test class. - Updated to `xunit` and `xunit.runner.visualstudio` to v2.3.1 (was v2.3.0-beta4-build3742) - To have the [CollectionDefinition(DisableParallelization = true)] feature. See https://xunit.github.io/releases/2.3-beta5 * Removed `InvariantCulture` Removed `InvariantCulture` on the following `UnitSystemTests` tests: 1. `NegativeInfinityFormatting` 2. `PositiveInfinityFormatting` * Specified InvariantCulture in InfinityFormatting Tests
2 parents f54e381 + 538f972 commit 3632fdb

File tree

7 files changed

+53
-26
lines changed

7 files changed

+53
-26
lines changed

UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<!--Get the latest released version of UnitsNet.Serialization.JsonNet in Nuget-->
2121
<PackageReference Include="UnitsNet.Serialization.JsonNet" Version="*" />
2222
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
23-
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
24-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
23+
<PackageReference Include="xunit" Version="2.3.1" />
24+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
2525
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
2626
</ItemGroup>
2727

UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
<ItemGroup>
1616
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
17-
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
18-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
17+
<PackageReference Include="xunit" Version="2.3.1" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
1919
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
2020
</ItemGroup>
2121

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace UnitsNet.Tests.CustomCode
2828
// Avoid accessing static prop DefaultToString in parallel from multiple tests:
2929
// UnitSystemTests.DefaultToStringFormatting()
3030
// LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
31-
[Collection("DefaultToString")]
31+
[Collection(nameof(UnitSystemFixture))]
3232
public class LengthTests : LengthTestsBase
3333
{
3434
protected override double CentimetersInOneMeter => 100;
@@ -156,4 +156,4 @@ public void MinValueIsCorrectForUnitWithBaseTypeDouble()
156156

157157

158158
}
159-
}
159+
}

UnitsNet.Tests/QuantityTests.ToString.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace UnitsNet.Tests
2727
{
28+
[Collection(nameof(UnitSystemFixture))]
2829
public partial class QuantityTests
2930
{
3031
public class ToString
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Xunit;
2+
3+
namespace UnitsNet.Tests
4+
{
5+
[CollectionDefinition(nameof(UnitSystemFixture), DisableParallelization = true)]
6+
public class UnitSystemFixture : ICollectionFixture<object>
7+
{
8+
// This class has no code, and is never created. Its purpose is simply
9+
// to be the place to apply [CollectionDefinition] and all the
10+
// ICollectionFixture<> interfaces.
11+
12+
// Apply this collection fixture to classes:
13+
// 1. that rely on manipulating CultureInfo. See https://github.com/angularsen/UnitsNet/issues/436
14+
// 2. to avoid accessing static prop DefaultToString in parallel from multiple tests:
15+
// a. UnitSystemTests.DefaultToStringFormatting()
16+
// b. LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
17+
}
18+
}

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929

3030
namespace UnitsNet.Tests
3131
{
32-
// Avoid accessing static prop DefaultToString in parallel from multiple tests:
33-
// UnitSystemTests.DefaultToStringFormatting()
34-
// LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
35-
[Collection("DefaultToString")]
32+
[Collection(nameof(UnitSystemFixture))]
3633
public class UnitSystemTests
3734
{
3835
private readonly ITestOutputHelper _output;
@@ -412,21 +409,32 @@ public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified()
412409
[Fact]
413410
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
414411
{
415-
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
416-
// CurrentUICulture affects localization, in this case the abbreviation.
417-
// Zulu (South Africa)
418-
var zuluCulture = new CultureInfo("zu-ZA");
419-
UnitSystem zuluUnits = UnitSystem.GetCached(zuluCulture);
420-
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;
412+
CultureInfo oldCurrentCulture = CultureInfo.CurrentCulture;
413+
CultureInfo oldCurrentUICulture = CultureInfo.CurrentUICulture;
421414

422-
UnitSystem usUnits = UnitSystem.GetCached(AmericanCultureName);
423-
usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");
415+
try
416+
{
417+
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
418+
// CurrentUICulture affects localization, in this case the abbreviation.
419+
// Zulu (South Africa)
420+
var zuluCulture = new CultureInfo("zu-ZA");
421+
UnitSystem zuluUnits = UnitSystem.GetCached(zuluCulture);
422+
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;
423+
424+
UnitSystem usUnits = UnitSystem.GetCached(AmericanCultureName);
425+
usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");
424426

425-
// Act
426-
string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);
427+
// Act
428+
string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);
427429

428-
// Assert
429-
Assert.Equal("US english abbreviation for Unit1", abbreviation);
430+
// Assert
431+
Assert.Equal("US english abbreviation for Unit1", abbreviation);
432+
}
433+
finally
434+
{
435+
CultureInfo.CurrentCulture = oldCurrentCulture;
436+
CultureInfo.CurrentUICulture = oldCurrentUICulture;
437+
}
430438
}
431439

432440
[Fact]
@@ -441,7 +449,7 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati
441449
[Fact]
442450
public void NegativeInfinityFormatting()
443451
{
444-
Assert.Equal("- m", Length.FromMeters(double.NegativeInfinity).ToString());
452+
Assert.Equal("-Infinity m", Length.FromMeters(double.NegativeInfinity).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture));
445453
}
446454

447455
[Fact]
@@ -473,7 +481,7 @@ public void Parse_UnambiguousUnitsDoesNotThrow()
473481
[Fact]
474482
public void PositiveInfinityFormatting()
475483
{
476-
Assert.Equal(" m", Length.FromMeters(double.PositiveInfinity).ToString());
484+
Assert.Equal("Infinity m", Length.FromMeters(double.PositiveInfinity).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture));
477485
}
478486

479487
/// <summary>

UnitsNet.Tests/UnitsNet.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<ItemGroup>
1616
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
1717
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
18-
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
19-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
18+
<PackageReference Include="xunit" Version="2.3.1" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
2020
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
2121
</ItemGroup>
2222

0 commit comments

Comments
 (0)