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

Posted · Add Comment

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


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

 
  • VSLive SF 2015: 'Better Unit Tests Through Design Patterns' Slides & CodeTweet !function(d s id){var js fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js fjs);}}(document "script" "twitter-wjs"); As [...]
  • VSLive SF 2015: Enterprise Git SlidesTweet !function(d s id){var js fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js fjs);}}(document "script" "twitter-wjs"); As [...]
  • VSLive SF 2015: TFS Automated Build, Test, & DeployTweet !function(d s id){var js fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js fjs);}}(document "script" "twitter-wjs"); As [...]
  • VSLive SF 2015: Load Testing Slides & CodeTweet !function(d s id){var js fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js fjs);}}(document "script" "twitter-wjs"); As [...]

© 2015 Benjamin Day Consulting, Inc.
Brookline, MA
617-645-0188
info@benday.com | |