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

STA Thread for WPF objects #103

Closed
cbuteau opened this Issue May 27, 2014 · 14 comments

Comments

Projects
None yet
5 participants
@cbuteau
Copy link

cbuteau commented May 27, 2014

Is there a RequireSTA type attribute for xUnit.Net like there is in NUnit.
I am building complex tests that involve WPF objects and running into ...
"The calling thread cannot access this object because a different thread owns it." exceptions.

@bradwilson

This comment has been minimized.

Copy link
Member

bradwilson commented May 31, 2014

This is currently unsupported in v2, because it makes extensive use of the Thread Pool (and thread pool threads are all MTA threads), so even attempting to start testing on an STA thread would not necessarily guarantee that tests would stay on the MTA thread.

The only solution for this today is to stick with 1.9.2.

@bradwilson bradwilson closed this May 31, 2014

@cbuteau

This comment has been minimized.

Copy link

cbuteau commented Jun 13, 2014

I am guess the Unit Test would spin up another thread that would set it as
STA. Then run test.

On Fri, Jun 13, 2014 at 1:41 PM, billumina notifications@github.com wrote:

How to you force STA threads with 1.9.x?

Thanks!


Reply to this email directly or view it on GitHub
#103 (comment).

@Haacked

This comment has been minimized.

Copy link

Haacked commented Nov 13, 2014

Here's how I'm solving this. Put this method in a static class.

public static Task StartSTATask(Action action)
{
    var tcs = new TaskCompletionSource<object>();
    var thread = new Thread(() =>
    {
        try
        {
            action();
            tcs.SetResult(new object());
        }
        catch (Exception e)
        {
            tcs.SetException(e);
        }
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    return tcs.Task;
}

Then call it from inside your unit test.

[Fact]
public async Task DefaultsToEmptyString()
{
    await ThreadHelper.StartSTATask(() =>
    {
        var passwordBox = new MyPasswordBox();

        Assert.Empty(passwordBox.Text);
        Assert.Empty(passwordBox.GetBaseText());
    });
}
@bradwilson

This comment has been minimized.

Copy link
Member

bradwilson commented Nov 13, 2014

Seems like a good candidate for an [STAFact] :)

@Haacked

This comment has been minimized.

Copy link

Haacked commented Nov 13, 2014

@bradwilson I wasn't sure if that would be possible. I need to be able to be the one calling the code that executes the test.

@Haacked

This comment has been minimized.

Copy link

Haacked commented Nov 13, 2014

I'd also need to write an [StaTheory] I'll look into it, but pointers are welcome. :)

@Haacked

This comment has been minimized.

Copy link

Haacked commented Nov 13, 2014

Hmm, looks like I could implement XUnitTestCase and override public override async Task<RunSummary> RunAsync(IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) to make this work.

@Haacked

This comment has been minimized.

Copy link

Haacked commented Nov 13, 2014

Ok, I got it working. Brad, would you like this as a contribution to xunit/samples?

@ivanrusso

This comment has been minimized.

Copy link

ivanrusso commented Jun 3, 2015

@bradwilson Any news to support STAFact and STATheory in xunit.core.
I'm working on a SpecFlow PR techtalk/SpecFlow#429 to support xunit2.
But the WPF-Tests (ViewModels) will throw an Exeption "must run on STA".
If xunit will support to define a test to run in STA (similar to Nunit http://nunit.org/index.php?p=requiresSTA&r=2.6.3), so i can support it too on Specflow Scenarios.

@bradwilson

This comment has been minimized.

Copy link
Member

bradwilson commented Jun 3, 2015

We have no intention of supporting this in the core framework. We do have a backlog item to provide the sample in NuGet form, for WPF and WinForms tests (need two separate libraries).

@JohanLarsson

This comment has been minimized.

Copy link

JohanLarsson commented Jan 31, 2016

Any reason this went into samples and not an xunit.wpf nuget?

@cbuteau

This comment has been minimized.

Copy link

cbuteau commented Jan 31, 2016

I guessing less work. I haven't worked on an OSS project but I imagine it is really hard prioritizing what should be a feature versus a workaround. Because no pay is involved.

@bradwilson

This comment has been minimized.

Copy link
Member

bradwilson commented Feb 1, 2016

The WPF version was picked up and released by @AArnott: https://www.nuget.org/packages/Xunit.StaFact/0.1.29-alpha

@JohanLarsson

This comment has been minimized.

Copy link

JohanLarsson commented Feb 1, 2016

ah, thank you sir!

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