spca2013 - test-driven development with sharepoint 2013 and visual studio

41

Upload: nccomms

Post on 07-May-2015

2.770 views

Category:

Technology


0 download

DESCRIPTION

Test-driven Development with SharePoint 2013 and Visual Studio

TRANSCRIPT

Page 1: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio
Page 2: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Test-driven Development with SharePoint 2013 and Visual Studio

Bill Ayers

Page 3: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Consultant currently specialising in SharePoint Development and Architecture for Web

Content Management

MCM/MCSM SharePointMCTS, MCITP, MCSD, MCAD, MCSA, MCDBA, MCT

etc.

Professional Scrum Master (PSM I)

Blog: www.SPDoctor.netE-mail: [email protected]

Twitter: @SPDoctor

Page 4: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Agenda:

Introduction to TDD Server-side tests Client-side tests ATDD Conclusions

Page 5: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Pre-historic Software Development

FORTRAN/COBOL Wish lists Ad-hoc development No source control! FORMAT statement

Page 6: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Waterfail

Big design up-front Write-only documentation Analysis paralysis Wrong product Over budget/time Project failure

Requirements

Design/Arch

Coding

QA

Deployment

Maintenance

Page 7: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Agile

• Individuals and interactions over processes and tools

• Working software over comprehensive documentation

• Customer collaboration over contract negotiation

• Responding to change over following a plan

http://agilemanifesto.org/

Page 8: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

The patterns and practices tag cloud

MVP

TDDIO

C

NUnit

SOLID

POCOS

Mock

s

Reposit

ory

Serv

ice

Loca

tor

Dependency Injection

Lambda Expressions

Separatio

n of

Concerns

MVV

M

Best

PracticeDesign Patterns

Busin

ess

Logic

Entity Framework

XP

Scrum

ATDD

BDD

MVC

Page 9: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

SharePoint is Different

Need some up-front architecture decisions, certain types of artefact very difficult to back out once in production (e.g. list schema)

Tight integration with the underlying platform can make code inherently difficult to test

Page 10: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Extreme Programming - XP

Test-first development Pair programming Refactoring Continuous integration Frequent releases Coding standards Sustainable pace

Page 11: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

What is TDD?

Goes beyond Test-first Tests drive the low-level design Supports refactoring Just a developer tool

Page 12: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

TDD Cycle

write a failing test

make test pass

refactor

requirements

Page 13: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

TDD rulebook

No code unless failing test Write just enough code to pass test No new test until green Refactoring must not add functionality Tests must be fast Tests must be easy to run (i.e. automated) Purpose of test should be clear Tests should be independent (run in any order) Test code is a first-class citizen

Page 14: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

How does TDD work?

TDD is counter-intuitive Tests facilitate refactoring Refactoring facilitates incremental design Failing tests pinpoint problems quickly Fast (continuous) tests give immediate

feedback Tests should ideally isolate the code you are

working on Rapid feedback Less bugs

Page 15: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Refactoring

Clean up variable and function names for clarity

Make code more concise Remove spaghetti code Remove duplicate code Extract methods to avoid oversize

methods Keep re-running the tests as you re-factor If test fails then Ctrl-Z, Ctrl-Z, Ctrl-Z …

Page 16: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Shopping List Story

Page 17: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Shopping List Story

As a team site user I want my shopping list visible on the

home page So that I don’t forget stuff

Acceptance Criteria:

• Does a shopping list panel appear on the home page?

• Does the panel show all items in the shopping list?

• Are the items sorted alphabetically

Page 18: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Demo: TDD – Introduction

Page 19: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Benefits of TDD?

Less bugs Better productivity (once the technique is learned) Safe refactoring results in better code design Cleaner design because enforces KISS and YAGNI Improved code quality and confidence Tests document the design Easier to handle interruptions to “flow” Incremental development Test coverage Enables future enhancement by ensuring existing functionality

is maintained (tests) Strangely hypnotic, especially if you do it while listening to

progressive dance music

Page 20: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Drawbacks

Can be difficult to maintain discipline under pressure Difficult in brownfield development Difficult without management support Excessive and trivial tests False sense of security Cost of maintaining tests Not all development is equally suited to TDD Not a silver bullet Not appropriate for spikes or other non-production

code Note all code is easy to test (e.g. SharePoint)

Page 21: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Test type

Speed

RequireCode Access

Need to run on SP Box

SuitTDD

SuitDev

SuitQA

SuitAccept

NeedIsolation f/w

Tools

Unit FAST * * * * * MSTest, NUnit

Integration (shallow)

MED * * * * MSTest, NUnit

Integration (deep)

SLOW

* * * MSTest, NUnit

UI SLOW

? * * (ATDD)

VS (Coded UI), Selenium, Watin

Manual GLACIAL

*

Perfor-mance

SLOW

* ? VS Load Test

Types of tests

Page 22: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Unit tests with SharePoint SSOM – possible approaches (1)

Public methods, overrides of virtual methods, etc.

Isolate SharePoint calls within a Repository class Service Locator pattern – P&P guidance Composite design patterns e.g. MVC/MVP, +

conventional mocks and stubs

Page 23: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Beware: Trivial Test Anti-pattern

Presenter:

string ShowMessage(string message, string name){ string messageresult = message + “ “ + name; Return messageresult;}

View:

Void RenderContents(..){String message = ShowMessage(“Hello, “, SPContext.Current.User.Name); textwriter.write(“<h1>” + message + “</h1>”);}

Test class:[TestMethod]Void ShowMessage_returns_correct_message_string(){ var presenter = new Presenter(_fakeView); string actual = presenter.ShowMessage(“Hello,”, “Bill”); Assert.AreEqual(“Hello, Bill”, actual);}

Page 24: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Unit tests with SharePoint SSOM – possible approaches (2)

Proprietary Mocking frameworks (Typemock Isolator, JustMock, Microsoft Fakes f/w)

Fake SharePoint framework (v. diff.)

Page 25: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Code Coverage (and other dubious metrics)

Your Custom Code

SharePoint .NET

SharePoint Unmanaged Code

ASP.NET

Windows OS

SQL

Page 26: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Unit tests with SharePoint SSOM – possible approaches (3)

Accept shallow integration tests in lieu (“unigration tests”)

Page 27: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Demo: Server-side testing

Page 28: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Client-side testing

The new model – SharePoint-hosted Apps

Unit testing JavaScript Faking the _api Shallow integration (“unigration”) tests

Surely this is the Java logo?

Page 29: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Options for client-side JavaScript testing

Ad-hoc tests (e.g. console logging) QUnit (qunitjs.com)

TDD style Jasmine

BDD style Mocha

BDD or TDD style PYO assertion library

UI tests (e.g. Selenium, VS UI tests)

Page 30: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Running tests

Can run tests in a browser window Chutzpah test runner

Command line and Visual Studio integration Only supports Jasmine and Qunit

For Continuous Integration use PhantomJS Headless browser Chrome browser engine (WebKit) Phantomjs.org

Use a cloud test service for “cross-browser test” Includes device browsers

Page 31: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Demo: Client-side testing

Page 32: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Tips for Testing JavaScript

Keep JavaScript modular in files, not scattered amongst HTML

To be able to Unit Test you need units

In JavaScript the unit of code encapsulation is the function

Very difficult to test an anonymous function

Very difficult to test a hidden function – may need to compromise encapsulation to make testable

Isolate tests from mark-up to avoid fragility

Page 33: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

User Story

Write Acceptance

Testswrite test

pass

refactorAcceptance

Tests Pass

Demo to PO

QA

The ATDD Cycle

Page 34: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

How does ATDD work? Similar to BDD A DSL for business users to define

automated acceptance tests Tooling such as Fitnesse,

SpecFlow Developer creates generic test

fixtures Can write tests first to drive

development (outer loop – inner loop is TDD)

Gives test team something to do at start of sprint

Page 35: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

How do we do acceptance tests? End-to-end testing No requirement for tests to be fast (within reason) Tools like SpecFlow can be used to define tests (but is

it worth it?) More likely a QA professional will manually build

automated QA tests Tools:

Selenium WebDriver Visual Studio UI tests

Page 36: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

End-to-end Testing

Best suited for acceptance tests Drives application through the UI Selenium WebDriver, Watin, Visual Studio Can target different browsers Fully tests DOM manipulation, JavaScript, interaction

with back-end (i.e. SharePoint) These tests often written by a dedicated tester Can be used for TDD but only if they are fast enough

Page 37: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Conclusions

Test Driven Development continues to gain traction in the development community as a whole and brings many benefits

SharePoint challenges us to find new ways of testing server-side code

The SharePoint 2013 App model brings with it a new world of client-side testing

Good news: client-side code is easier to test than server-side SharePoint code

Some end-to-end testing will be necessary particularly for acceptance tests

Page 38: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Recommended Reading:

“The Art of Unit Testing”, Roy Osherove www.extremeprogramming.org “Clean Code – A Handbook of Agile

Software Craftsmanship”, Robert C Martin

Lean-Agile Acceptance Test-Driven Development, Ken Pugh

Page 39: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio

Contact me:

Blog: www.spdoctor.net Twitter: @spdoctor Sharepoint.stackexchange.com

(moderator) Email: [email protected]

Page 40: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio
Page 41: SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio