testing and debugging sharepoint with visual studio 2012

Upload: balouchi26

Post on 02-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    1/36

    Hands-On Lab

    Testing and Debugging SharePoint 2010Applications with Visual Studio 2012

    Lab version: 11.0.60315.01 Update 2

    Last updated: 4/9/2013

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    2/36

    CONTENTS

    OVERVIEW ................................................................................................................................................... 3

    EXERCISE 1: CREATING UNIT TESTS USING THE SHAREPOINT EMULATOR ................................... 5

    EXERCISE 2: INTELLITRACE SUPPORT FOR SHAREPOINT ............................................................... 17

    EXERCISE 3: WEB AND LOAD TESTING SHAREPOINT APPLICATIONS ........................................... 25

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    3/36

    Overview

    In this lab, you will learn about new features provided in Update 1 and 2 that make testing and

    debugging SharePoint applications easier, thereby helping to improve the quality and scalability of yourproduct. Although this lab walks through testing and debugging a SharePoint 2010 instance, the steps

    generally apply to SharePoint 2013 also.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    4/36

    Prerequisites

    In order to complete this lab you will need the Visual Studio 2012 virtual machine provided by Microsoft.

    For more information on acquiring and using this virtual machine, please seethis blog post.

    Lab Updates

    This lab highlights features and enhancements introduced with Visual Studio 2012 Update 1, although

    some lab steps may also highlight features that were in place prior to the update. Update 1 specific

    features and enhancements are specifically called out as they are introduced.

    Exercises

    This hands-on lab includes the following exercises:

    Creating Unit Tests using the SharePoint Emulator

    IntelliTrace Support for SharePoint

    Web and Load Testing SharePoint Applications

    Estimated time to complete this lab: 60 minutes.

    http://aka.ms/VS11ALMVMhttp://aka.ms/VS11ALMVMhttp://aka.ms/VS11ALMVMhttp://aka.ms/VS11ALMVM
  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    5/36

    Exercise 1: Creating Unit Tests using the

    SharePoint Emulator

    In this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit tests in

    order to help remove unnecessary dependencies to SharePoint and the SharePoint API, thereby isolating

    your tests and making sure that you are testing the code you want.

    Log in as Adam. All user passwords are P2ssw0rd.

    Open the AppointmentsWebPartsolution file from

    C:\SharePointDemos\AppointmentsWebPartin Visual Studio 2012.

    Figure 1

    Loading sample solution

    This solution contains a web part that allows a user to book an appointment. Lets take a look at

    it in action to get an idea of what it does. Right-clickon the AppointmentsWebPartproject in

    Solution Explorer and select Deployto deploy and activate the feature on the local SharePoint

    server.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    6/36

    Figure 2

    Deploying the web part

    Open Internet Explorer and navigate tohttp://vsalm/sites/team.

    Figure 3

    SharePoint demo site

    Select the Editbutton to edit the page.

    Figure 4

    Edit button location

    Place the cursor on the page between the paragraph of text and the Shared Documents section.

    http://vsalm/sites/teamhttp://vsalm/sites/teamhttp://vsalm/sites/teamhttp://vsalm/sites/team
  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    7/36

    Figure 5

    Selecting insert location

    Select the Inserttab.

    Figure 6

    Insert tab location

    Select the Web Part button to add in the custom web part.

    Figure 7

    Web Part button

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    8/36

    Select the Customcategory, the AppointmentsWebPartand then select the Addbutton to add

    it to the page.

    Figure 8Adding in the web part

    Select the Save & Closebutton.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    9/36

    Figure 9

    Save and close

    The web part expects there to be a list named Appointments, so go ahead and click on the Lists

    link to create one.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    10/36

    Figure 10

    Navigate to Lists

    Select the Createbutton.

    Figure 11

    Create new list button

    Select the Appointmentstype, enter a name of Appointments, and select the Createbutton.

    Figure 12

    Creating Appointments list

    Select the Browsebutton and then select the Teamlink in the navigation bar to return to the

    default page.

    Figure 13

    Return to home page

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    11/36

    Manually test the appointment web part by entering some test data into the fields and then

    select the Submitbutton.

    Figure 14

    Testing the Appointments web part

    Return to the Appointmentslist and note that the appointment has been added as expected.

    Figure 15

    New appointment

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    12/36

    Now lets take a look at how we can take advantage of the SharePoint Emulatorand Microsoft

    Fakes Frameworkto develop and execute unit tests. The SharePoint Emulator code base is

    installed via NuGet. Return to Visual Studio, right-clickon the AppointmentsWebPart.Tests

    project and select the Manage NuGet Packagesoption.

    Figure 16

    Manage NuGet packages

    You should see that the Microsoft.SharePoint.Emulatorspackage is already installed for the

    test project. The package downloads needed assemblies and adds the appropriate references to

    the project for you.

    Figure 17

    Microsoft.SharePoint.Emulators already added to project

    Closethe Manage NuGet Packageswindow.

    Load the UnitTest1.csfile from the AppointmentsWebPart.Testsproject and navigate to the

    first test method that starts with ScheduleAppointment This test method uses the

    SharePoint Emulator to create a test list, add fields to it, and use the web part to test the

    scheduling of an appointment.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    13/36

    Figure 18

    Unit test definition

    Most of what you see looks identical to normal SharePoint code, however it is all wrapped in a

    Usingstatement with an instance of SharePointEmulationScope. This is responsible for

    redirecting normal SharePoint calls to the shims provided by the emulator.

    Figure 19

    Use of SharePointEmulationScope

    Right-clicksomewhere within the test definition and select Run Tests.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    14/36

    Figure 20

    Run unit test

    Note that this test reports success.

    Figure 21

    Successful unit test run with SharePoint Emulator

    Note: If the unit test unexpectedly fails, please restart Visual Studio and try again.

    Scroll down to the second test method and take a look at what it does. It creates a list, inserts

    test appointments into the list, and then calls the GetAppointmentsForToday method from the

    web part under test to ensure that appointments for the current date are returned. It also uses

    the SharePointEmulationScopeas before.

    Figure 22

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    15/36

    Unit test definition

    As you work with the SharePoint Emulator, you may run into circumstances where features

    have not yet been implemented. In these cases, expect to see aNotSupportedExceptionoccur

    during test runs with information about the unimplemented shim. It turns out that theGetAppointmentsForTodaymethod on the web part under test makes use of the

    SPList.GetItemsmethod, which will throw a NotSupportedException when running the version

    of the SharePoint Emulator installed on this VM.

    Fortunately, it is straightforward to specify an implementation of the missing shim, as

    evidenced by the following code from the test method. Note that there is an explicit test to run

    this code block when running in emulation mode.

    Figure 23

    Implementing a shim

    Note: This shim implementation simply grabs the first item from the list for the purposes of

    this demo, regardless of the query passed in. If you would like to learn more about the

    Microsoft Fakes Framework, please see the MSDN documentationhere.

    Right-clicksomewhere within the test definition and select Run Teststo make sure it works as

    expected. The result should be that the unit test passes.

    Figure 24

    Unit test results

    http://aka.ms/MSFakeshttp://aka.ms/MSFakeshttp://aka.ms/MSFakeshttp://aka.ms/MSFakes
  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    16/36

    You can also run your unit tests against a real SharePoint instance by changing the emulation

    mode to bypass all shims and directly call into the original SharePoint assembly. Scroll to the top

    of the GetAppointments test method and change EmulationMode.Enabledto

    EmulationMode.Passthrough.

    Figure 25

    Configuring unit test to pass through the emulator

    Note: In a real-world scenario, you would likely want to re-use your unit test code in both

    emulated and passthrough modes. To do this, you could define the emulation mode to use at

    the test class level and use test initialize and cleanup to create and destroy the scope.

    Automation of the selected emulation mode could then be implemented using preprocessor

    directives, with the definition provided in the test project file or via the build command line.

    For more information, please see the MSDN SharePoint Emulator articlehere.

    Right-clicksomewhere within the test definition and select Run Testsonce again. This time the

    test will run against a real SharePoint instance.

    Figure 26

    Unit test passing through the emulator (note the time difference)

    http://aka.ms/SPEmulatorhttp://aka.ms/SPEmulatorhttp://aka.ms/SPEmulatorhttp://aka.ms/SPEmulator
  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    17/36

    Exercise 2: IntelliTrace Support for

    SharePoint

    In this exercise, you will learn about Update 1 improvements made to IntelliTrace that improve the

    debugging experience of SharePoint applications.

    Log in as Adam. All user passwords are P2ssw0rd.

    Open the SharePointProject1solution file from c:\SharePointDemos\SharePointProject1in

    Visual Studio 2012.

    Figure 27

    Loading sample solution

    Open WebPart1.csfrom Solution Explorer. Note that this simple web part simply identifies the

    current user name and renders some output HTML.

    Figure 28

    Sample web part code

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    18/36

    Right-clickon the SharePointProject1project and select Deployto deploy and activate the

    feature on the local SharePoint server.

    Lets go ahead and take a look at this web part in action. Open Internet Explorer and navigate to

    http://vsalm/sites/team.

    Select the Site Pageslink.

    Figure 29

    Navigating to Site Pages

    Select the Site Actionsdrop-down and then select New Page.

    Figure 30

    Creating a new test page

    Use IntelliTrace Demo for the new page name and then select Create.

    http://vsalm/sites/teamhttp://vsalm/sites/teamhttp://vsalm/sites/team
  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    19/36

    Figure 31

    Creating a new test page

    Select the Inserttab.

    Figure 32

    Inserting the web part

    Select the Web Partbutton to add in the custom web part.

    Figure 33

    Inserting the web part

    Select the Customcategory, the SharePointProject1web part and then select the Addbutton

    to add it to the page.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    20/36

    Figure 34

    Inserting the web part

    Select the Save and Closebutton.

    Figure 35

    Save changes

    Here we can see that the web part is deployed and functional.

    Figure 36

    Custom web part in action

    Now lets assume that this web part has been working for some time but that an additional

    feature has been added in and users are now reporting intermittent errors. Return to Visual

    Studio and uncommentthe line that throws an exception.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    21/36

    Figure 37

    Artificially introducing an exception for demo

    Right-clickon the SharePointProject1project and select Deployto deploy the updated web

    part.

    At this point, you can use IntelliTraceto collect diagnostic data for SharePoint using the

    IntelliTrace PowerShell module. Start IntelliTrace by executing the StartIntelliTraceDemo.cmd

    script found in C:\SharePointDemos.

    Figure 38

    Start IntelliTrace session

    Note: If you would like to learn more about how to use IntelliTrace in a production

    environment, including the details involved with these scripts, please see the Diagnosing

    Issues in Production with IntelliTrace and Visual Studio 2012 lab.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    22/36

    After IntelliTrace has started, return to Internet Explorer and refresh the IntelliTrace Demo

    page. An error page should now be displayed by SharePoint. This is something that an end user

    would likely see and then report back to the development team. Select the Correlation IDand

    then press Ctrl + Cto copy it to the clipboard.

    Figure 39

    Typical SharePoint error showing correlation ID

    Execute the StopIntelliTraceDemo.cmdscript to stop IntelliTrace.

    Figure 40

    Stop IntelliTrace session

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    23/36

    The scripts that executed IntelliTrace collection were configured to output data to

    C:\LogFileLocation. Double-clickon the IntelliTracefile found at that location to load it in Visual

    Studio.

    Figure 41IntelliTrace log file

    In the IntelliTrace Summaryview, note that there is an Analysissection at the top that shows

    an unhandled exception. We could start debugging the unhandled exception by clicking on the

    Debug Exception button, but lets start by pasting the Correlation IDwe received from our end

    user in to the blank text box (use Ctrl+V) to review matching web requests. Select theView

    Detailsbutton.

    Figure 42

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    24/36

    Viewing web request details associated with Correlation ID

    Here we can see request information associated with the SharePoint Correlation ID. We can see

    the target URL, user agent, and so on. Select the Start Debugging button.

    Figure 43

    Web request details

    Closethe web request details window and return to the IntelliTrace file.

    Select the Debug Exceptionbutton to the right of the unhandled exception.

    Figure 44

    Start debugging the unhandled exception

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    25/36

    Once Visual Studio is in debug mode, you should see that the location of the unhandled

    exception is highlighted in the WebPart1.cssource file and that you have the normal IntelliTrace

    debugging tools at your disposal.

    Figure 45

    IntelliTrace debugging

    Exercise 3: Web and Load Testing

    SharePoint Applications

    In this exercise, you will learn about how Update 1 has improved the Visual Studio 2012 web and load

    testing tools. To learn more about web and load testing in general, please see the Introduction to Web

    Performance and Load Testing with Visual Studio Ultimate 2012.

    Log in as Adam. All user passwords are P2ssw0rd.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    26/36

    Open the SP_Web_LoadTest_Demo solution file from

    c:\SharePointDemos\WebAndLoadTestProject1in Visual Studio 2012.

    Figure 46

    Loading sample solution

    This solution contains a test project with two web tests and a load test. We will take a closer

    look at these in a moment.

    Figure 47

    Sample web and load test project

    Prior to Update 1, it was certainly possible to record web tests against SharePoint, but it

    required you to significantly hand edit the recording to do things such as remove extraneous

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    27/36

    requests and parameterize the site name. Update 1 performs more of these tedious tasks

    automatically, and this is configurable. View the available options by selecting Tools | Options |

    Web Performance Test Tools | Web Test | SharePointin the main menu of Visual Studio.

    Figure 48

    SharePoint web test options (showing defaults)

    After taking a look at the default options for SharePoint web tests, press Escapeto exit the

    Options window.

    Open Upload.webtestin the web test editor. This web test was recorded by navigating to a

    SharePoint site, loading the Shared Documents library, and uploading a document.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    28/36

    Figure 49

    Upload web test definition

    Note that the site name was automatically parameterized for us in all of the recorded requests.

    This makes it easy to data drive the site name if desired.

    Figure 50

    Site name was automatically parameterized

    Expand the second request in the list and note that a SharePoint specific extraction rule was

    automatically added in to grab the list ID and store it as a context parameter.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    29/36

    Figure 51

    SharePoint extraction rule to find list ID

    Figure 52

    Properties of extraction rule

    The list ID context parameter is then used in subsequent requests, for example in the request to

    Upload.aspx.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    30/36

    Figure 53

    Use of list ID context parameter

    The actual file that is uploaded during the test run is parameterized as well. Expand the secondweb request to Upload.aspx (there are two) and scroll to the bottom of the Form Post

    Parametersand select last option, which is a File Upload Parameter. This shows that when we

    run the web test, a unique filename will be used when uploading the Doc2.doc file.

    Figure 54

    File Upload Parameter

    In addition to a couple of new validation rules, Update 1 also provides a number of useful

    extraction rules that you can use with your SharePoint web requests. Right-clickon one of the

    web requests and select the Add Extraction Ruleoption.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    31/36

    Figure 55

    Location of Add Extraction Rule option

    The additional SharePoint extraction rules allow you to do things like find specific list and

    document IDs, calendar dates, values of text boxes, workflow instance IDs, and so on.

    Figure 56

    New SharePoint extraction rules

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    32/36

    Press the Escapekey to close the Add Extraction Rule window.

    Double-clickon Download.webtestin Solution Explorer to load it in the web test editor. This

    web test was recorded by navigating to a SharePoint site, loading the Shared Documents library,

    and downloading a specific document. After the test was recorded, a SharePoint extraction rulewas added to find the document item and store it as a context parameter.

    Figure 57

    Use of a SharePoint extraction rule to find a document item

    In the web request to Download.aspx, note that one of the query string parameters refers to

    the document previously extracted.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    33/36

    Figure 58

    Use of document reference previously extracted

    Double-clickon SPLoadTest1.loadtestfrom Solution Explorer to load it in the load test editor.

    Figure 59

    Load test definition

    This is a basic load test definition that will spend half of the time on the Download web test and

    the other half on the Upload web test. Note that Update 1 provides a new counter set

    definition, SharePoint 2012 WFE.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    34/36

    Figure 60

    SharePoint Counter Set

    Go ahead and run the load test to see it in action by selecting the Run Load Testbutton. The

    load test is configured to run for 1 minute.

    Figure 61

    Location of Run Load Test button

    After the results summary displays, you can see the individual page result details if you scroll

    down. To visualize the performance details over time, you can also try the Graphs view.

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    35/36

    Figure 62

    Summary results of load test

  • 8/10/2019 Testing and Debugging SharePoint With Visual Studio 2012

    36/36

    Figure 63

    Graph view of load test results

    To give feedback please write [email protected]

    Copyright 2014 by Microsoft Corporation. All rights reserved.

    mailto:[email protected]:[email protected]:[email protected]:[email protected]