-
Notifications
You must be signed in to change notification settings - Fork 32
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
Running all tests within a class on the same thread? #54
Comments
I think, this issue is related to #53. |
@eriove Why is it important to reuse a thread for all tests in a class? Are your tests sharing objects with each other? That tends to be discouraged so that one test doesn't discretely lead to another test's failure. |
I think the source of my problem is that WPF applications usually have one UI thread, hence there might be assumptions in the WPF code or WPF component code that relies on that fact. In this merge request you can see an example of that. I create a new This StackOverflow question seems to suggest that there is a caching mechanism for ContextMenus. I'm not quite sure where to solve this. When I'm back at work on Tuesday I will see if I can do some experiments with styles or just set the context menu explicitly to get around my particular problem. But this seems like a problem that could occur for other people and solving it here would solve it for everyone without having to track down what UI element that gets cached. But I'm not sure, maybe the libraries should be left alone and I should just dig further in my own tests... |
Aside: If you're testing WPF code, are you using "Fixing" this would mean that all tests have to run sequentially instead of in parallel, which could be a huge takeback for some folks with many tests. The test title qualifies it with "within a class" but if ContextMenu really is retaining some state behind the scenes, wouldn't fixing it require that all tests run on the same thread, whether they belong to the same test class? |
Sorry for taking so long to reply. It has taken a while to process this. I've been using You are right about needing to run all tests sequential to resolve the problem and that that would be a bad idea for projects with many UI dependent tests. I did not think about that. I'm lucky enough to only have 10 to 20 tests that needs to run on a STA-thread. I've created a minimal project that reproduces the problem and the problem is that a style contains a I imagine that this wouldn't be an unusual problem. Do you have any other suggests on how to solve the problem? |
Thanks for sharing the repro project. I think I need to re-familiarize myself both with this project and how xunit does thread management since this issue and #53 may have overlap so I'd want to look at them together. I don't have time now to investigate though, but I'll keep it active. |
No problem. My merge request for changing from static to dynamic resources was merged into AvalonDock today so the immediate problem is solved. If nothing else this thread might help others facing the same problem. |
Ok, so now that #53 is fixed and didn't impact this, I'm ready to revisit. Since so many other WpfFact tests don't hit this, and you have a workaround, we might entertain not "fixing" this here. If we did, I'd want to look for a way for a test project, class, or method to "opt in" so that tests that do not require sharing a single thread are not unnecessarily slowed down. One obvious idea is to add an optional named argument to the |
I like the idea of an optional argument to |
I would prefer to use an assembly or class-level attribute as an alternative to a optional parameter. ALL my tests require that they run on the same thread, so it would have been a pain in the ass to have to add a parameter to each test. |
I think at least class-level makes sense. If only a couple tests need it, people can put those in another class. I'm not against a test-level attribute as well, but maybe that can be based on demand. |
Just wanted to add to the discussion that I've also ran into this issue. I attempted to work around this using xunit.runner.json to use app domains, or to set number of threads to 1, but it still used different threads (one at a time). I'm not sure how to work around this. |
…and:
FWIW, I had the exact same problem, although with a
If you change the above to:
…, then the brush gets initialized once per thread. In my testing, Xunit.StaFact can then safely proceed. |
Would it be possible to add an option to run all tests within a class on the same thread? Right now all tests run on an STA thread but not on the same STA thread. I ran into problems where different UI components end up on different threads which causes failures.
See this merge request for an example of a workaround.
The behavior can be observed by making the following change to the constructor of StaFactTests.
Running the test will print the following lines (with varying numbers):
The text was updated successfully, but these errors were encountered: