Conflicts between [CodedUITest] and [UnitTest] attributes cause Coded UI tests to fail with NullReferenceException.

August 26, 2011

File this under “Programmer Error / Duh.”  I was refactoring a bunch of Coded UI tests to use a base class for common functionality.  I did something dumb and managed to break all my Coded UI tests in a way that was difficult to diagnose.

Symptom: NullReferenceExceptions coming from Coded UI test code in UIMap.designer.cs where previously there were no problems.

Test method Benday.CodedUiSamples.Tests.CheckboxFixture.ChangeCheckboxValue threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.

Microsoft.VisualStudio.TestTools.UITest.Framework.UITestService.TechnologyManagerByName(String technologyName)
Microsoft.VisualStudio.TestTools.UITesting.UITestControl.ValidateSearchProperties()
Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary()
Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Click(MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinates)
Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementation(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
Microsoft.VisualStudio.TestTools.UITesting.Mouse.ClickImplementationWrapper(UITestControl control, MouseButtons button, ModifierKeys modifierKeys, Point relativeCoordinate)
Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(UITestControl control, Point relativeCoordinate)
Benday.CodedUiSamples.Tests.UIMap.CloseTheApp() in c:codebendaytfs2010BDC_1MainBlogSamplesBenday.CodedUiSamplesBenday.CodedUiSamples.TestsUIMap.Designer.cs: line 94
Benday.CodedUiSamples.Tests.CheckboxFixture.ChangeCheckboxValue() in c:codebendaytfs2010BDC_1MainBlogSamplesBenday.CodedUiSamplesBenday.CodedUiSamples.TestsCheckboxFixture.cs: line 33

Cause: The UIMap class was not being initialized properly because the test fixture was decorated with the [CodedUITest] attribute and the base class for the test was decorated with the [TestClass] attribute.  The [CodedUITest] attribute is correct.  The Visual Studio test framework detects the [TestClass] attribute on the base class before it detects the [CodedUITest] attribute and therefore executes the [TestMethod] as an ordinary unit test rather than a Coded UI test.

Solution: Remove the [TestClass] attribute from the base class.

SNAGHTMLc03d9f

I hope this helps.

-Ben

Tags: coded-ui