Testing MOSS 2007 Web Parts.<p align=left>Hi,</p> <p align=left> </p> <p align=left>I need to write some unit tests for some Web Parts. However I have a few questions:</p> <p align=left> </p> <p align=left>What exactly do I test? Most of the Web Parts I am testing are consuming a Web Service and retrieving some data. In the render method this data is displayed. In the childcontrol method the controls are initialised. Most of the methods return void. What exactly can I write a unit test for in this scenario.</p> <p align=left> </p> <p align=left>Each Web Part is deployed as a feature. Below is an example implementation of the FeatureInstalled() method:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left> </p><font size=2> <p align=left></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>override</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> FeatureInstalled(</font><font color="#2b91af" size=2>SPFeatureReceiverProperties</font><font size=2> properties)</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// Make sure we De-Activate the Feature by default</p></font><font size=2> <p align=left></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#2b91af" size=2>SPSite</font><font size=2> site = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SPSite</font><font size=2>(</font><font color="#a31515" size=2>&quot;http://localhost&quot;</font><font size=2>))</p> <p align=left>{</p> <p align=left></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#2b91af" size=2>SPWeb</font><font size=2> web = site.OpenWeb())</p> <p align=left>{</p> <p align=left></font><font color="#2b91af" size=2>Guid</font><font size=2> _featureGuid = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>Guid</font><font size=2>(</font><font color="#a31515" size=2>&quot;5a6fbdb9-475a-4ea4-b25e-ef51f9f9e9e5&quot;</font><font size=2>);</p> <p align=left></font><font color="#008000" size=2>// Check to see if the feature is already De-Activated.</p></font><font size=2> <p align=left></font><font color="#2b91af" size=2>SPFeature</font><font size=2> _feature = web.Features[_featureGuid];</p> <p align=left></font><font color="#0000ff" size=2>if</font><font size=2> (_feature != </font><font color="#0000ff" size=2>null</font><font size=2>)</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// De-Activate the feature</p></font><font size=2> <p align=left>web.Features.Remove(_featureGuid);</p> <p align=left>}</p> <p align=left>}</p> <p align=left>}</p> <p align=left>}</p></font> <p align=left> </p></div></div> <p align=left> </p> <p align=left>Can I write a unit test for this method? If so what exactly would I be testing? Thanks for your help in advance. Rest assured I have spent a substantial amount of time googling this subject. I believe your help here will be of help to many not just me.</p> <p></p><font size=2> <p align=left></p></font>© 2009 Microsoft Corporation. All rights reserved.Thu, 25 Jun 2009 22:33:40 Ze261b4ec-9f41-484d-b355-0d80dbfb65aahttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#e261b4ec-9f41-484d-b355-0d80dbfb65aahttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#e261b4ec-9f41-484d-b355-0d80dbfb65aajasear [MCTS SharePoint]http://social.msdn.microsoft.com/Profile/en-US/?user=jasear%20%5bMCTS%20SharePoint%5dTesting MOSS 2007 Web Parts.<p align=left>Hi,</p> <p align=left> </p> <p align=left>I need to write some unit tests for some Web Parts. However I have a few questions:</p> <p align=left> </p> <p align=left>What exactly do I test? Most of the Web Parts I am testing are consuming a Web Service and retrieving some data. In the render method this data is displayed. In the childcontrol method the controls are initialised. Most of the methods return void. What exactly can I write a unit test for in this scenario.</p> <p align=left> </p> <p align=left>Each Web Part is deployed as a feature. Below is an example implementation of the FeatureInstalled() method:</p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left> </p><font size=2> <p align=left></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>override</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> FeatureInstalled(</font><font color="#2b91af" size=2>SPFeatureReceiverProperties</font><font size=2> properties)</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// Make sure we De-Activate the Feature by default</p></font><font size=2> <p align=left></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#2b91af" size=2>SPSite</font><font size=2> site = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SPSite</font><font size=2>(</font><font color="#a31515" size=2>&quot;http://localhost&quot;</font><font size=2>))</p> <p align=left>{</p> <p align=left></font><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#2b91af" size=2>SPWeb</font><font size=2> web = site.OpenWeb())</p> <p align=left>{</p> <p align=left></font><font color="#2b91af" size=2>Guid</font><font size=2> _featureGuid = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>Guid</font><font size=2>(</font><font color="#a31515" size=2>&quot;5a6fbdb9-475a-4ea4-b25e-ef51f9f9e9e5&quot;</font><font size=2>);</p> <p align=left></font><font color="#008000" size=2>// Check to see if the feature is already De-Activated.</p></font><font size=2> <p align=left></font><font color="#2b91af" size=2>SPFeature</font><font size=2> _feature = web.Features[_featureGuid];</p> <p align=left></font><font color="#0000ff" size=2>if</font><font size=2> (_feature != </font><font color="#0000ff" size=2>null</font><font size=2>)</p> <p align=left>{</p> <p align=left></font><font color="#008000" size=2>// De-Activate the feature</p></font><font size=2> <p align=left>web.Features.Remove(_featureGuid);</p> <p align=left>}</p> <p align=left>}</p> <p align=left>}</p> <p align=left>}</p></font> <p align=left> </p></div></div> <p align=left> </p> <p align=left>Can I write a unit test for this method? If so what exactly would I be testing? Thanks for your help in advance. Rest assured I have spent a substantial amount of time googling this subject. I believe your help here will be of help to many not just me.</p> <p></p><font size=2> <p align=left></p></font>Tue, 10 Jun 2008 07:20:54 Z2008-07-01T07:43:36Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#3bf78094-201b-4672-a015-8cd1adf803f8http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#3bf78094-201b-4672-a015-8cd1adf803f8Joe Allan Muharsky - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Joe%20Allan%20Muharsky%20-%20MSFTTesting MOSS 2007 Web Parts.<p>Hi,</p> <p align=left> </p> <p align=left>The nature of SharePoint and Web Parts, as you have no doubt found, can make them difficult to unit test in the conventional sense.  A good general article reviewing real-world findings and pitfalls when testing in SharePoint can be found at <a title="http://www.pseale.com/blog/SharePointNotUnitTesting.aspx" href="http://www.pseale.com/blog/SharePointNotUnitTesting.aspx">http://www.pseale.com/blog/SharePointNotUnitTesting.aspx</a>.  Regarding your specific questions:</p> <p align=left> </p> <p align=left>1. A test that uses the Object Model to manually install the feature in question would be the right place to start;  You could consider the following:</p> <p align=left>* Test Feature Installation when it is not currently installed.</p> <p align=left>* Test Feature Installation when it is already installed.</p> <p align=left>* Test Feature Installation when it is installed and cannot be removed (such as by adding a dependent feature).</p> <p align=left>The above test, and all tests for SharePoint, will need to be configured for remote execution on the web server, as all OM calls must execute locally on the server.</p> <p align=left> </p> <p align=left>Also, consider in the code above how you can validate the de-activation code.  It may make more sense to factor out the &quot;deactivate if already installed&quot; code into it's own method, so that you can test it in isolation from other feature installation.</p> <p align=left> </p> <p align=left>2. For testing Web Parts, you're not likely to get obvious method results for your test validation, but you can apply validation (via regex matches and the like) to the resulting output in the web part.  You may find that Web Tests work better than Unit Tests for some of these, in terms of the testing framework used.  A good checklist of things you'll want to test (automated if possible) in SharePoint can be found at <a title="http://msdn.microsoft.com/en-us/library/ms916830.aspx" href="http://msdn.microsoft.com/en-us/library/ms916830.aspx">http://msdn.microsoft.com/en-us/library/ms916830.aspx</a>.</p> <p align=left> </p> <p align=left>Hope this helps, and please let me know if you have additional questions/concerns.</p> <p align=left> </p> <p align=left>Joe Allan Muharsky</p> <p align=left>SDET, Visual Studio Team Test</p> <p align=left><font face=Arial size=2></font> </p>Tue, 10 Jun 2008 20:52:37 Z2008-07-01T07:43:36Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#f8aa3b79-9908-4946-b9ce-d16cd266f6fbhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#f8aa3b79-9908-4946-b9ce-d16cd266f6fbSoftProgrammerrhttp://social.msdn.microsoft.com/Profile/en-US/?user=SoftProgrammerrTesting MOSS 2007 Web Parts.<p align=left><font face=Arial size=2>Hello Friends,</font></p> <p align=left> </p> <p align=left>Thanking you for these posts.</p> <p align=left>Actually I do need help for automated testing on web parts any sites which you </p> <p align=left>can introduce me I have googled alot but didn't find anything useful.</p> <p align=left>I want to know with which testing tools I can do automated testing on web parts.</p> <p align=left> </p> <p align=left>Thanks in advance</p>Thu, 03 Jul 2008 12:00:22 Z2008-07-03T12:00:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#59866107-fdae-4bcf-8bd4-bd3b49edf6adhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#59866107-fdae-4bcf-8bd4-bd3b49edf6adMuhammad Tufailhttp://social.msdn.microsoft.com/Profile/en-US/?user=Muhammad%20TufailTesting MOSS 2007 Web Parts.<font size=2><span style="font-family:Arial">hi,<br><br>You can test the MOSS Application on MS VSTS. but you will face some errors while running the test script of MOSS. googled it you will find how to fix that errors. <br><br><br></span></font>Mon, 04 Aug 2008 06:04:52 Z2008-08-04T06:04:52Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#5715a770-ef69-4b5b-99ad-31f616f65491http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#5715a770-ef69-4b5b-99ad-31f616f65491SoftProgrammerrhttp://social.msdn.microsoft.com/Profile/en-US/?user=SoftProgrammerrTesting MOSS 2007 Web Parts.<p>Hello,</p> <p align=left> </p> <p align=left>What else is possible to test except Webparts in SharePoint Server 2007?</p> <p align=left><font face=Arial size=2></font> </p><em>Thanks</em>Mon, 08 Sep 2008 09:03:33 Z2008-09-08T09:03:33Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#0108816d-3aac-4cb0-ba21-9a2c216236edhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#0108816d-3aac-4cb0-ba21-9a2c216236edSeamus_mhttp://social.msdn.microsoft.com/Profile/en-US/?user=Seamus_mTesting MOSS 2007 Web Parts.<p class=MsoNormal style="margin:0in 0in 10pt;direction:ltr;unicode-bidi:embed;text-align:left"><span style="font-size:8pt;color:black;font-family:'Verdana','sans-serif'">You can use Typemock Isolator to fake the SharePoint objects in your tests, and run your tests even on an XP machine. And you can do that with as much as a single line of code. Read more on <a title="Testing SharePoint" href="http://www.typemock.com/sharepointpage.php"></a><a title="testing SharePoint" href="http://www.typemock.com/sharepointpage.php">testing SharePoint</a> </span>in the link</p>Thu, 30 Apr 2009 15:42:47 Z2009-04-30T15:43:30Zhttp://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#9e9df2cd-093a-4f26-be58-517ec2ea66f0http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/e261b4ec-9f41-484d-b355-0d80dbfb65aa#9e9df2cd-093a-4f26-be58-517ec2ea66f0humayun chughtaihttp://social.msdn.microsoft.com/Profile/en-US/?user=humayun%20chughtaiTesting MOSS 2007 Web Parts.Hi Muhammad Tufail, <br/> <br/> Can you please drop me an email with your contact details on humayun@bluestormsolutions.net.<br/>Thu, 25 Jun 2009 22:33:40 Z2009-06-25T22:33:40Z