Skip to content

Add support for static test classes - #174

Merged
AArnott merged 5 commits into
mainfrom
copilot/add-support-for-static-tests
Jan 28, 2026
Merged

AArnott merged 5 commits into
mainfrom
copilot/add-support-for-static-tests

Conversation

Copilot AI commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor

Fix static test class support in Xunit.StaFact

Plan:

  • Explore repository and understand the issue
  • Identify the root cause: xUnit v3 calls .Single() on GetConstructors() for test class instantiation
  • Fix GetSettingsAttributes method in Utilities.cs to handle static test classes
  • Override CreateTestClassInstance in UITestRunner to handle static test classes
  • Add test cases for static test classes with UIFact, UITheory, StaFact, StaTheory attributes
  • Verify the fix works by running tests
  • Run code review
  • Run security scan
  • Fix all build warnings

Root Cause:

The issue was in xUnit v3's XunitTestRunnerBase.CreateTestClassInstance() method which calls GetConstructors().Single(). For static test classes, GetConstructors() returns an empty array, causing .Single() to throw "Sequence contains no elements".

Fix Applied:

  1. Refactored GetSettingsAttributes in Utilities.cs to use try-catch blocks around GetCustomAttributes calls with FirstOrDefault() instead of SingleOrDefault()
  2. Override CreateTestClassInstance in UITestRunner.cs to detect static test classes (Abstract + Sealed) and return null instance instead of trying to instantiate them
  3. Added comprehensive test coverage with 11 new tests covering:
    • Static UIFact tests (sync and async)
    • Static UITheory tests (sync and async)
    • Static StaFact tests
    • Static StaTheory tests
    • Static test classes with UISettings attributes (class-level and method-level)
  4. Fixed IDE0008 warning by using explicit Type instead of var for @Class variable

Test Results:

  • Total tests increased from 158 to 168 (10 new passing tests)
  • All static test class tests are passing (StaFact/StaTheory skip on non-Windows as expected)
  • No regressions in existing tests
  • Build: ✅ 0 warnings, 0 errors
  • Code Review: ✅ Passed
  • Security Scan: ✅ No vulnerabilities found

Security Summary:

No security vulnerabilities were introduced or discovered during this change. The CodeQL security scan found 0 alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Does not work with static test class</issue_title>
<issue_description>Xunit.StaFact does not work with static test class. Here is a repro:

https://github.com/weifenluo/StaFactNotWorking

Running the test will produce the following error:

Message: 
System.InvalidOperationException : Sequence contains no elements

  Stack Trace: 
ThrowHelper.ThrowNoElementsException()
Enumerable.Single[TSource](IEnumerable`1 source)
--- End of stack trace from previous location ---
```</issue_description>

<agent_instructions>Add support for `static` test classes, and add a test to demonstrate that it works.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@AArnott</author><body>
Thanks for the report and repro.
TBH I never understood why people bothered with `static` tests. But I have no objection to supporting that.</body></comment_new>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 27, 2026 17:11
Changed GetSettingsAttributes to use try-catch blocks with FirstOrDefault
to handle cases where xUnit's GetCustomAttributes throws InvalidOperationException
for static test classes. Also added test cases for static test classes.

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Added override in UITestRunner to handle static test classes by checking
if the class is both Abstract and Sealed (which indicates a static class).
For static classes, returns null instance instead of trying to instantiate.
Tests now pass for static test classes.

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for static test classes in Xunit.StaFact Add support for static test classes Jan 27, 2026
Copilot AI requested a review from AArnott January 27, 2026 17:25
@AArnott
AArnott marked this pull request as ready for review January 27, 2026 19:37
@AArnott

AArnott commented Jan 27, 2026

Copy link
Copy Markdown
Owner

@copilot fix all build warnings. Use dotnet build -t:rebuild to ensure you see all warnings.

Changed var to Type for @Class variable in CreateTestClassInstance
to comply with project's editorconfig rule csharp_style_var_elsewhere = false:warning

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>

Copilot AI commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix all build warnings. Use dotnet build -t:rebuild to ensure you see all warnings.

Fixed in commit 49a99ad. Changed var to explicit Type for the @Class variable to comply with the project's editorconfig rule csharp_style_var_elsewhere = false:warning. Build now succeeds with 0 warnings and 0 errors.

Copilot AI requested a review from AArnott January 27, 2026 20:01
@codecov-commenter

codecov-commenter commented Jan 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.52%. Comparing base (2c212b8) to head (49a99ad).

Files with missing lines Patch % Lines
src/Xunit.StaFact/Sdk/Utilities.cs 71.42% 4 Missing ⚠️
src/Xunit.StaFact/Sdk/UITestRunner.cs 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #174      +/-   ##
==========================================
- Coverage   71.60%   71.52%   -0.09%     
==========================================
  Files          36       36              
  Lines         729      748      +19     
  Branches       62       64       +2     
==========================================
+ Hits          522      535      +13     
- Misses        186      191       +5     
- Partials       21       22       +1     
Flag Coverage Δ
Linux 71.16% <71.42%> (-0.09%) ⬇️
Windows 68.58% <71.42%> (-0.01%) ⬇️
macOS 71.16% <71.42%> (-0.09%) ⬇️

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does not work with static test class

3 participants