Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Thursday, February 7, 2013

Powershell Test Data for Record Center

Scenario:
Wanted to do a load test on Record Center with my custom solution. I wanted to have 100 record libraries with 100 folder each.

Solution:
Powershell script can do it in few minutes

Code:

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

[Void][System.Diagnostics.Stopwatch] $sw;
# New Stopwatch object
$sw = New-Object System.Diagnostics.StopWatch;
# Stop any watches that might be running
$sw.Stop();                                         
$sw.Start();

# Initialization
$webUrl = 'http://recordcenter'
$libCount = 1;
$folderCount = 1;
$libraryName = "Record Library" + $libCount
$frontSlash = "/"
$web = Get-SPWeb $webUrl;
$listTemplate = $web.ListTemplates | Where-Object {$_.Name -eq "Record Library"}
#Create folders 
(1..100)|%{
      $libraryName = "Contract Library" + $libCount                         
      write-host "Creating Record library: " $libraryName                                      
      $web.Lists.Add($libraryName,"Record Library",$listTemplate);
       $libCount  = $libCount + 1;
       [string]$howlong0 = $sw.Elapsed.ToString();  # How long
       Write-Host "Library Ready (Time ): " $howlong0 -F Green;
        $spDocumentLibrary = $web.Lists.TryGetList($libraryName)
                    (1..1000)|%{
                    write-host "Adding Folder in library: " $libraryName
                    $folderName = "Folder" + $folderCount
                 $spFolder = $spDocumentLibrary.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folderName) 
                 $spFolder.Update()
                    $folderCount = $folderCount+1
                   [string]$howlong0 = $sw.Elapsed.ToString();  # How long
                    Write-Host "Folder Ready (Time ): " $howlong0 -F Green;
                    }
}

$web.Dispose()
$sw.Stop();    # Stop the timer
[string]$howlong = $sw.Elapsed.ToString();   # How long
write-host "Done (Time ): " $howlong -F Green;

Friday, September 14, 2012

SharePoint Powershell Test Data script

Scenario:
Working on a project and we need to test the software limit of SharePoint 2010.
MS Documentation is not clear about things/ community blogs have different experience.
So without taking chance, here a small script to create test items

Solution:
Powershell script

Code:

cls
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$webUrl = Read-Host 'Please specify site url'
$web = Get-SPWeb $webUrl;
$list = $web.Lists.TryGetList("Announcements");
(1..50000)|%{
$item = $list.Items.Add();
$item["Title"]="Test";
$item.Update();
# Optional 
# $item.BreakRoleInheritance($True);
$item.ID
}
$web.Dispose()

Wednesday, October 8, 2008

Testing code in sharepoint context

Scenario:
You want to test the webpart code in sharepoint context
Solution:
You can use the Visual Studio Test classes. Code sample shows both using Context and without context.

Code(Without Context):

public static string GetSiteTitle(string url) { 
SPSite site = new SPSite(url);
string title = site.RootWeb.Title;
site.RootWeb.Dispose();
site.Dispose();
return title;
}

[TestMethod()]
public void GetSiteTitleTest() {
string url = http://localhost;
string expected = "Home";
string actual = MyClass.GetSiteTitle(url);
Assert.AreEqual(expected, actual);
}
Code(With Context):
public static string GetSiteTitle() { 
SPSite site = SPContext.Current.Site;
string title = site.RootWeb.Title;
return title;
}

[TestMethod()]
[HostType("ASP.Net")]
[UrlToTest("http://localhost/default.aspx")]
public void GetSiteTitleTest() {
string expected = "Home";
string actual = MyClassName.GetSiteTitle();
Assert.AreEqual(expected, actual);
}
Article:
http://blogs.msdn.com/mcsnoiwb/archive/2008/10/08/unit-testing-sharepoint-code.aspx

Saturday, September 6, 2008

Test Case for a Feature receiver

Scenario:
You want to write a test case for the Feature Receiver

Sample Feature Code:

namespace TrainingSamples
{
public class SampleFeatureReceiver:SPFeatureReceiver
{

[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties == null)
{
return;
}

SPWeb web = (SPWeb)properties.Feature.Parent;
this.OnActivationCode(web);
}

public void OnActivationCode(SPWeb web)
{
//Code

}
}
}
Sample Test Case Code:
namespace TrainingSamples
{
[TestMethod()]
public void FeatureRecieverOnActivationTest()
{
SampleFeatureReceiver target = new SampleFeatureReceiver();

SPSite site;
SPWeb web;
//Bind to the site where you are testing provisioning code.

site = new SPSite("http://servername");
using (site)
{
web = site.RootWeb;
using (web)
{
target.OnActivationCode(web);
}
}

Assert.IsTrue(true,
"Feature Activation did not throw any exceptions.");
}
}
}