Poser une questionPoser une question
 

TraitéeTesting MOSS 2007 Web Parts.

  • mardi 10 juin 2008 07:20jasear [MCTS SharePoint] Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hi,

     

    I need to write some unit tests for some Web Parts. However I have a few questions:

     

    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.

     

    Each Web Part is deployed as a feature. Below is an example implementation of the FeatureInstalled() method:

    Code Snippet

     

    public override void FeatureInstalled(SPFeatureReceiverProperties properties)

    {

    // Make sure we De-Activate the Feature by default

    using (SPSite site = new SPSite("http://localhost"))

    {

    using (SPWeb web = site.OpenWeb())

    {

    Guid _featureGuid = new Guid("5a6fbdb9-475a-4ea4-b25e-ef51f9f9e9e5");

    // Check to see if the feature is already De-Activated.

    SPFeature _feature = web.Features[_featureGuid];

    if (_feature != null)

    {

    // De-Activate the feature

    web.Features.Remove(_featureGuid);

    }

    }

    }

    }

     

     

    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.

Réponses

  • mardi 10 juin 2008 20:52Joe Allan Muharsky - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Hi,

     

    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 http://www.pseale.com/blog/SharePointNotUnitTesting.aspx.  Regarding your specific questions:

     

    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:

    * Test Feature Installation when it is not currently installed.

    * Test Feature Installation when it is already installed.

    * Test Feature Installation when it is installed and cannot be removed (such as by adding a dependent feature).

    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.

     

    Also, consider in the code above how you can validate the de-activation code.  It may make more sense to factor out the "deactivate if already installed" code into it's own method, so that you can test it in isolation from other feature installation.

     

    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 http://msdn.microsoft.com/en-us/library/ms916830.aspx.

     

    Hope this helps, and please let me know if you have additional questions/concerns.

     

    Joe Allan Muharsky

    SDET, Visual Studio Team Test

     

Toutes les réponses

  • mardi 10 juin 2008 20:52Joe Allan Muharsky - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Hi,

     

    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 http://www.pseale.com/blog/SharePointNotUnitTesting.aspx.  Regarding your specific questions:

     

    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:

    * Test Feature Installation when it is not currently installed.

    * Test Feature Installation when it is already installed.

    * Test Feature Installation when it is installed and cannot be removed (such as by adding a dependent feature).

    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.

     

    Also, consider in the code above how you can validate the de-activation code.  It may make more sense to factor out the "deactivate if already installed" code into it's own method, so that you can test it in isolation from other feature installation.

     

    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 http://msdn.microsoft.com/en-us/library/ms916830.aspx.

     

    Hope this helps, and please let me know if you have additional questions/concerns.

     

    Joe Allan Muharsky

    SDET, Visual Studio Team Test

     

  • jeudi 3 juillet 2008 12:00SoftProgrammerr Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hello Friends,

     

    Thanking you for these posts.

    Actually I do need help for automated testing on web parts any sites which you

    can introduce me I have googled alot but didn't find anything useful.

    I want to know with which testing tools I can do automated testing on web parts.

     

    Thanks in advance

  • lundi 4 août 2008 06:04Muhammad Tufail Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    hi,

    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.


  • lundi 8 septembre 2008 09:03SoftProgrammerr Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hello,

     

    What else is possible to test except Webparts in SharePoint Server 2007?

     

    Thanks
  • jeudi 30 avril 2009 15:42Seamus_m Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposée

    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 testing SharePoint in the link

    • Proposé comme réponseSeamus_m jeudi 30 avril 2009 15:43
    •  
  • jeudi 25 juin 2009 22:33humayun chughtai Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi Muhammad Tufail,

    Can you please drop me an email with your contact details on humayun@bluestormsolutions.net.