Skip to content

Fix xUnit Theory serialization warnings #3186

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

Merged
merged 7 commits into from
Mar 18, 2025

Conversation

paulmedynski
Copy link
Contributor

@paulmedynski paulmedynski commented Feb 28, 2025

xUnit makes 2 passes over unit tests:

  1. Discover all of the tests, and for Theory tests, enumerate all of the test data.
  2. Run all of the tests, enumerating the test data again.

If the enumeration won't produce the same data in both steps, xUnit emits a warning. We have many test cases whose Theory data doesn't enumerate identically in both passes, for a variety of reasons. These tests will still run correctly, but they won't appear in test runner UIs like Visual Studio. Instead, xUnit collapses them into a single test for these UIs.

The warnings are clutter in the console (where I like to run tests), so xUnit provides a mechanism to disable enumeration completely in the discovery phase. I have updated the offending tests to use the MemberData DisableDiscoveryEnumeration flag. This doesn't affect how the tests run, and it doesn't change how UIs display them (collapsed). All it does is suppress the console warnings.

Further reading here: https://xunit.net/faq/theory-data-stability-in-vs

I also added 4 new targets to build.proj to make running the tests easier. The existing test running targets function as before.

Commit notes:

  • Suppressed xUnit discovery data enumeration warnings in Function and Manual test projects.
  • Fixed xUnit test data enumeration warnings specific to .NET Framework (enums in the GAC).
  • Added targets to run Windows and Unix tests.
  • Cleaned up test run command lines for improved maintenance and runtime logging.

<Target Name="RunFunctionalTestsWindows" Condition="'$(IsEnabledWindows)' == 'true'">
<PropertyGroup>
<TestCommand>
$(DotnetPath)dotnet test "@(FunctionalTestsProj)"
Copy link
Contributor Author

@paulmedynski paulmedynski Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatted for readability, and then squished back into a single line since <Exec> treats each line in the Command as a separate command!

--filter "category!=non$(TargetGroup)tests&amp;category!=failing&amp;category!=nonwindowstests"
--logger:"trx;LogFilePrefix=Functional-Windows$(TargetGroup)-$(TestSet)"
</TestCommand>
<TestCommand>$(TestCommand.Replace($([System.Environment]::NewLine), " "))</TestCommand>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modern dotnet tooling supports String.ReplaceLineEndings(), but we're using the old .NET Framework msbuild.exe tooling to build, so we can't have the nice toys :(

[MemberData(
nameof(CEKEncryptionReversalData)
#if NETFRAMEWORK
// .NET Framework puts system enums in something called the Global
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a comment once in each file where we're using DisableDiscoveryEnumeration, to make it clear why we're using that flag.

@@ -505,33 +519,25 @@ public override IEnumerable<Object[]> GetData(MethodInfo testMethod)
}
}

public class CEKEncryptionReversalParameters : DataAttribute
public static IEnumerable<object[]> CEKEncryptionReversalData()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since xUnit only supports DisableDiscoveryEnumeration on MemberData attributes, I have converted all of the data sources to be compatible with MemberData.

You will see similar changes throughout the tests, where data source classes become methods.

[CEKEncryptionReversalParameters]
[MemberData(
nameof(CEKEncryptionReversalData)
#if NETFRAMEWORK
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.NET doesn't have a GAC, so these tests will enumerate correctly for those targets. We only need to disable for .NET Framework targets.

@@ -4,6 +4,7 @@

using System;
using System.Runtime.InteropServices;
using System.Threading;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary.

#endif
)]
[MemberData(
nameof(ConstructorTextData)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds Text to the mix, which seems to have been missing from the InlineData.

@@ -565,7 +584,20 @@ private void ExecuteAndCheckForError(SqlCommand sqlCmd, bool expectError)
try
{
sqlCmd.ExecuteNonQuery();
Assert.Fail("We should have gotten an error but passed instead.");
StringBuilder builder = new(
Copy link
Contributor Author

@paulmedynski paulmedynski Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With broken (now disabled) enumeration, xUnit doesn't include the test data in any failure output, so we need to do it ourselves.

This test fails for me on main as well, so I wanted to see what data set was causing the problem. Turns out to be the Date data set (line 1450).

@@ -1,6 +1,13 @@
{
// Typical Windows SQL Server on the local host via TCP.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are breaking our CI "Update config.json" task because PowerShell's ConvertFrom-Json fails But as of PowerShell v6, ConvertFrom-Json handles comments:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.5

Are we really using ancient PowerShell 5.1 from 2016? I will have to dig into this further.

- Suppressed xUnit discovery data enumeration warnings in Function and Manual test projects.
- Fixed xUnit test data enumeration warnings specific to .NET Framework (enums in the GAC).
- Added targets to run Windows and Unix tests.
- Cleaned up test run command lines for improved maintenance and runtime logging.
@paulmedynski paulmedynski force-pushed the dev/paul/fix-test-theory-data-warnings branch from a04396b to ae09fcc Compare March 3, 2025 14:19
- Added installation of PowerShell Core (pwsh) as a dotnet global tool
  so we can use the ADO task PowerShell@2 in pwsh mode on all agents/images.
- Added installation of PowerShell Core (pwsh) as a dotnet global tool
  so we can use the ADO task PowerShell@2 in pwsh mode on all agents/images.
- Removed attempt to install PowerShell Core.  Adding it to the 1ES images instead.
- Trying pwsh (PowerShell Core 7.x) again for config file update job.
- Updating pipeline to use my new Win11 1ES image with pwsh.
Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.52%. Comparing base (b875e1d) to head (e928eb1).
Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3186      +/-   ##
==========================================
- Coverage   72.79%   72.52%   -0.27%     
==========================================
  Files         285      289       +4     
  Lines       59156    59543     +387     
==========================================
+ Hits        43060    43183     +123     
- Misses      16096    16360     +264     
Flag Coverage Δ
addons 92.58% <ø> (ø)
netcore 75.08% <ø> (-0.38%) ⬇️
netfx 71.15% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Reverting pipeline to use the newly rebuilt ADO-CI-Win11 image.
@paulmedynski paulmedynski marked this pull request as ready for review March 14, 2025 14:33
@paulmedynski paulmedynski requested review from Copilot and a team March 14, 2025 14:33
@paulmedynski paulmedynski added this to the 7.0-preview1 milestone Mar 14, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses xUnit Theory serialization warnings by replacing InlineData and ClassData attributes with MemberData attributes that disable discovery enumeration on .NET Framework. It also refactors several test data providers across multiple test files and updates pipeline configuration steps to use pwsh consistently.

  • Migrate test data attributes from InlineData/ClassData to MemberData with DisableDiscoveryEnumeration for improved xUnit behavior.
  • Refactor test data providers into static methods returning IEnumerable<object[]>.
  • Update pipeline YAML steps to use "pwsh" instead of "powershell" for configuration consistency.

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs Refactored xUnit test attributes for Constructor3, Constructor6, and Constructor7 tests using MemberData.
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/ConversionTests.cs Replaced ClassData with MemberData in multiple tests to suppress discovery warnings and enhanced error logging details.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlCommandSetTest.cs Introduced MemberData for CommandType tests with appropriate disable flags on .NET Framework.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTestWithTdsServer.cs Updated MemberData usage to disable discovery enumeration for connection tests on different platforms.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonBulkCopyTest.cs Converted JsonBulkCopyTestData from a class to a static method and updated test attributes accordingly.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/AlwaysEncryptedTests/SqlColumnEncryptionCertificateStoreProviderShould.cs Replaced custom data attribute with MemberData for CEK encryption reversal and valid certificate paths tests.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/AmbientTransactionFailureTest.cs Updated MemberData for exception test data to disable discovery enumeration.
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/ApiShould.cs Refactored multiple tests to use MemberData for connection string providers with disable discovery flags.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs Updated FactoryMethodTest to use MemberData with disable discovery enumeration for improved stability.
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/DatabaseHelper.cs Consolidated AE connection string providers into a static DataHelpers class with static test data methods.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderStreamsTest.cs Updated MemberData attributes for various DataReader stream tests with disable discovery flags on .NET Framework.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlDataRecordTest.cs Refactored test data providers for UDT and XXX value tests from ClassData to MemberData.
eng/pipelines/common/templates/steps/update-config-file-step.yml Replaced "powershell:" with "pwsh:" in the YAML steps for updating the config file.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlMetaDataTest.cs Refactored several tests to use MemberData with DisableDiscoveryEnumeration, and added new static test data providers for constructor tests.
Comments suppressed due to low confidence (2)

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonBulkCopyTest.cs:27

  • The parameter 'enableStraming' appears to be a typo. It is recommended to rename it to 'enableStreaming' for clarity and consistency.
public void TestJsonBulkCopy(CommandBehavior cb, bool enableStraming, int jsonArrayElements, int rows)

eng/pipelines/common/templates/steps/update-config-file-step.yml:129

  • [nitpick] The task key has been updated from 'powershell:' to 'pwsh:'. Please ensure that all related pipeline configuration files consistently use 'pwsh' as the identifier for PowerShell steps.
- pwsh: |

@cheenamalhotra cheenamalhotra added Area\Tests Issues that are targeted to tests or test projects Area\Engineering Use this for issues that are targeted for changes in the 'eng' folder or build systems. labels Mar 18, 2025
// xUnit can't consistently serialize the data for this test, so we
// disable enumeration of the test data to avoid warnings on the
// console.
DisableDiscoveryEnumeration = true)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a custom serializer help in this case? Not to say we should do it in this PR, just curious if you experimented with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes looked into that, but didn't want to figure out serialization for all of the affected tests as part of this PR. The changes here don't affect how the tests run, and they don't change how they're visualized in IDEs. They just suppress the warnings on the console. I've opened an ADO task to track fixing the serialization itself.

@paulmedynski paulmedynski requested a review from mdaigle March 18, 2025 18:51
@paulmedynski paulmedynski merged commit f3ae72a into main Mar 18, 2025
252 checks passed
@paulmedynski paulmedynski deleted the dev/paul/fix-test-theory-data-warnings branch March 18, 2025 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area\Engineering Use this for issues that are targeted for changes in the 'eng' folder or build systems. Area\Tests Issues that are targeted to tests or test projects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants