0

为什么我的班级在编码的 UI 测试中执行清理方法时出现 STA 错误,但具体的测试方法似乎工作正常。我的代码看起来像 ff:

    [CodedUITest]
    public class ConcreteCUIT : TestBase 
    {
        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
          //do init
        }
        [ClassCleanup]
        public static void CleanUp()
        {
          //do clean-up - STA error!!!
        }
        [TestMethod]
        public void Concrete1CRUDTest()
        {
          //do UI test
        }
        [TestMethod]
        public void Concrete2CRUDTest()
        {
          //do UI test
        }
    } 

我在下面收到以下错误:

Class Cleanup method SampleCUIT.CleanUp failed. Error Message:Microsoft.VisualStudio.TestTools.UITest.Extension.UITestException: The Coded UI Test is running in Single Thread Apartment (STA) mode of COM.  In this mode, all the playback calls should happen from the TestMethod thread only and UITestControl should not be shared across TestMethods.. 
Stack Trace:
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.ThrowExceptionIfCrossThreadAccess(IScreenElement uiElement)
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId, Int32 depth, Boolean singleQueryId, Boolean throwException)
   at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId, Int32 depth)
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Int32 maxDepth, Int32& timeLeft)
   at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg)
   at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg)
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary()
   at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Click(MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinates)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementation(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementationWrapper(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
   at Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(UITestControl control)
4

1 回答 1

1

堆栈跟踪的第一行说明了问题:“......所有播放调用都应该从 TestMethod 线程发生”。堆栈跟踪的后面几行显示了mouse.click来自清理例程的调用。

不要从ClassCleanup方法中调用 UI 操作。

于 2013-05-21T12:39:46.820 回答