Assert apartment state instead of using Clipboard in UI tests - #16270
Merged
Conversation
The apartment-state acceptance tests (ExecutionThreadApartmentStateTests) run SimpleTestProject3's UITestMethod and UITestWithSleep1, which called Clipboard.SetText as a stand-in for an STA-only API. On the shared CI agents that clipboard call intermittently throws ExternalException "Requested Clipboard operation did not succeed", and reddens the Windows integration test leg with a single flaky failure on UITestShouldPassIfApartmentStateIsSTA. It is a flake, not a regression, the same commit passes and fails across different builds. Replace the clipboard call with Assert.AreEqual(ApartmentState.STA, Thread.CurrentThread.GetApartmentState()), which is exactly what these tests are checking, STA passes and MTA fails, just without touching the OS clipboard. Remove the now unused System.Windows.Forms using and reference. Verified locally: SimpleTestProject3 builds for net481, and ExecutionThreadApartmentStateTests runs green (UITestShouldPassIfApartmentStateIsSTA passes, UITestShouldFailWhenDefaultApartmentStateIsMTA still fails as expected). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: eac7b544-a9bb-483d-9fee-d443a2a467bb
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces flakiness in the Windows apartment-state acceptance coverage by removing reliance on the OS clipboard from SimpleTestProject3 UI test methods, while preserving the intended STA/MTA pass/fail behavior those acceptance tests validate.
Changes:
- Replace
Clipboard.SetText(...)usage with an explicitApartmentState.STAassertion on the executing test thread. - Remove the now-unneeded
System.Windows.Formsusing and project reference from the test asset.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/TestAssets/SimpleTestProject3/UnitTest1.cs | Replaces clipboard usage with an STA apartment-state assertion in UI test methods under #if NETFRAMEWORK. |
| test/TestAssets/SimpleTestProject3/SimpleTestProject3.csproj | Removes the conditional System.Windows.Forms reference since it’s no longer required. |
Jakub Jareš (nohwnd)
enabled auto-merge (squash)
July 20, 2026 11:02
Amaury Levé (Evangelink)
approved these changes
Jul 20, 2026
Amaury Levé (Evangelink)
left a comment
Member
There was a problem hiding this comment.
The direct apartment-state assertions preserve the intended STA-pass/MTA-fail behavior while removing the flaky clipboard dependency. The Windows Forms reference removal is safe.
This was referenced Sep 9, 2026
This was referenced Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The apartment-state acceptance tests (
ExecutionThreadApartmentStateTests) run SimpleTestProject3'sUITestMethodandUITestWithSleep1, which calledClipboard.SetTextas a stand-in for an STA-only API. On the shared CI agents that clipboard call intermittently throwsExternalException: Requested Clipboard operation did not succeed, and reddens the Windows integration test leg with a single flaky failure onUITestShouldPassIfApartmentStateIsSTA. It is a flake, not a regression, the same commit passes and fails across different builds.Replace the clipboard call with
Assert.AreEqual(ApartmentState.STA, Thread.CurrentThread.GetApartmentState()), which is exactly what these tests check, STA passes and MTA fails, just without touching the OS clipboard. Also remove the now unusedSystem.Windows.Formsusing and reference.Verified locally:
SimpleTestProject3builds for net481, andExecutionThreadApartmentStateTestsruns green (UITestShouldPassIfApartmentStateIsSTApasses,UITestShouldFailWhenDefaultApartmentStateIsMTAstill fails as expected).