quality coding with visual studio 2012

43
Quality Coding: What's New with Visual Studio 2012 The newest release of Visual Studio 2012 is rich with new tools that enhance standard developer activities. In this session, we'll review and demonstrate some of these new features, such as Unit Testing, Code Reviews, Code Clones, and other developer tools. Come join us for this free Webinar!

Upload: imaginet

Post on 11-May-2015

1.638 views

Category:

Technology


8 download

DESCRIPTION

The newest release of Visual Studio 2012 is rich with new tools that enhance standard developer activities. In this session, we'll review and demonstrate some of these new features, such as Unit Testing, Code Reviews, Code Clones, and other developer tools.

TRANSCRIPT

Quality Coding: What's New with Visual Studio 2012

The newest release of Visual Studio 2012 is rich with new tools that enhance standard developer activities. In this session, we'll review and demonstrate some of these new features, such as Unit Testing, Code Reviews, Code Clones, and other developer tools. Come join us for this free Webinar!

Agenda

• ALM and Quality

• Quality in Requirements

• Quality in Development– Unit Testing– Fakes– Code Reviews– Code Analysis– Code Clones

• Quality in Test– Manual Testing– Exploratory Testing– Automated Testing– Lab Environments

• Conclusion

Initial Project Portfolio Retire

Recognizing the Iterative Nature of Applications

Define• Requirements• Validation• Prioritization• Release Plan

Develop• Iteration Plan• Develop• Test

Operate• Monitor• Support• Upgrade

Plan

WorkDonePlan

WorkDone

Plan

WorkDone

Plan

WorkDone

Plan

WorkDone

New ALM Capabilities in Visual Studio 2012

Define• Requirements• Validation• Prioritization• Release Plan

Develop• Iteration Plan• Develop• Test

Operate• Monitor• Support• Upgrade

DefineIdeation

OperateDeployment to feedback

DevelopIdea to working software

• PowerPoint Storyboarding• Agile Planning

• Suspend & Resume• Code Reviews• Feedback Collection• Unit Testing• Exploratory Testing• Continuous Integrations• Continuous Deployments

• SCOM Integration• IntelliTrace in Production• PreEmptive Analytics

Quality in Requirements: StoryBoarding

• Tighter loop between the Business Stakeholders and Development Team

• Graphical design tools built in PowerPoint

• Embed other content including context slides

• Capture screen shots and create lightweight animations

• Store common elements within a shape library

• Create master templates to simplify multiple similar views

• Get feedback to others– Email, print and version control the

document– leverage collaborative tools– leverage web viewing tools– Integration with TFS

Agenda

• ALM and Quality

• Quality in Requirements

• Quality in Development– Unit Testing– Fakes– Code Reviews– Code Analysis– Code Clones

• Quality in Test– Manual Testing– Exploratory Testing– Automated Testing– Lab Environments

• Conclusion

Why invest in quality?

• Quality is an expensive (and painful) afterthought

RequirementsCoding

IntegrationBeta Test

Post-Release

5

10

15

20

25

30

Relative CostTo Fix Bugs...

Courtesy of the National Institute of Software and Technology (NIST)

Problems...

• It is expensive to find and fix bugs that get past daily development practices

• It is hard to diagnose errors at runtime• Why does an application run slowly?• Individual Developers and Testers need to know if

they are on track• Test and development are often out of synch• Final test phase for shipping is often ad-hoc• How much testing is enough?

Approach for Development Quality

• Use 'defence in depth' strategy– Unit testing– Code reviews– Continuous integration builds / Gated Check-ins– Static Code Analysis– Education / Patterns / Best Practices

Unit Testing Runner

New Test Runner:• Tests run in background• Run automatically on build• Support for multiple unit

testing frameworks:– MS Test– xUnit– nUnit– And More!

• Deep integration in the IDE• Supports native C++ code• Multiple run options

– Failed Tests– Not-run Tests– All Tests

• Easy code coverage access

public double MethodA() { ... }

Unit Testing

• Diagnostic checks during development– Automated test script for methods on a type– Basic sanity checking– Useful for regression testing

public void TestMethodA(){ SomeClass c = new SomeClass(); // Arrange double d = c.MethodA(); // Act Assert.AreEqual(expected, d); // Assert}

Each

meth

od

has

on

e o

r more

corre

sp

on

din

g

test m

eth

od

s

Use the framework you want to use

• In the box support for– .NET – Native C/C++

• Third party plugins– NUnit– xUnit.net– MbUnit– QUnit/Jasmine– SQL Server Unit Testing

(Under development)

MS-Test Improvements

• Many performance and scale improvements– Especially when you stick to “classic” unit testing

• Support for testing Async[TestMethod]public async Task MyAsyncTest(){ var result = await SomeLongRunningOperation(); Assert.IsTrue( result );}

• Proper support for 64-bit and .Net multi-targeting• Available in Express!

Continuous Testing

• “If you aren’t running your unit tests, you are just compiling. You are not building.”

• Chris PattersonProgram ManagerTeam Foundation Build

• Run Tests After Build option in Visual Studio 2012 will run your Unit Tests after each successful build of your solution

Code coverage in VS 2012

• Analyze your code coverage with a single click

• Analyze for selected tests to help find how specific tests are covering your system

• Supports all managed & native frameworks

DEMONSTRATION

• Unit Test Basics

• Framework Plug-Ins

• Unit Test Explorer

• Continuous Testing

• Code Coverage

What’s missing?

• Test Lists– Legacy mode only

• Test Impact– Works on the server, not in

the VS client

• Private Accessors– Deprecated in VS 2010,

removed in VS 2012

• Generate Unit Test– Didn’t actually generate a

unit test

Unit Testing and Isolation

• Unit Tests verify the smallest testable ‘unit’ of code• Target code should be isolated from external

influences• Unit Test frameworks can perform integration testing

Target Pseudo-Code:Function DoSomething(a) x = LookupInDatabase(a) y = ProcessWithWebService(x) LogToFile(“Processing complete”)End Function

Unit Test Pseudo-Code: T = new SomeClass() result = T.DoSomething(1) Assert.AreEqual(2,result)End Function

Response controlled by test

Response controlled by test

Response controlled by test

What is un-testable code?

Where do we find it?

• “Get ‘er done” code– Business logic in code-behind– Classes with too many

dependencies– Database logic mixed with

business logic

• Testability was not a consideration– Or was explicitly avoided

• Monolithic, highly-coupled designs

Common indicators

• Complex test setup and teardown• Environmental dependencies• Public static methods• Hidden object creation• Complex external frameworks• No tests at all!

Any system where the tests require complex setup or where the tests run very slowly is unsuitable for the kind of

developer testing we really care about.

void FileExistsTest() { File.Write("foo.txt", ""); var result = IsFileEmpty("foo.txt") Assert.IsTrue(result);}

File.Write("foo.txt", "");

A simple test setup example

The method we want to unit test

bool IsFileEmpty(string file) { var content = File.ReadAllText(file); return content.Length == 0;}

File.ReadAllText(file);

Is this a “good” test?

…this ugly setup code

This dependency forces…

Environmental Dependencies

Consider the following Y2K code:

public void ThrowIfEndOfTheWorld() { if (DateTime.Now == new DateTime(2000,1,1)) throw new Y2KBugException();}

How would you test it reliably?

[DllImport("kernel32.dll")]extern static bool SetSystemTime(ref SystemTime time);

[TestMethod]public void Y2KTest(){ SetSystemTime(2000,1,1,0,0,0); Assert.Throws( () => ThrowIfEndOfTheWorld() );}

Environmental Dependencies

How about this? Why is this bad?

Isolating code with Microsoft Fakes

• The new VS 2012 Fakes framework lets you isolate almost ANYTHING in .NET

• Fakes come in two flavors– Stubs – concrete

implementations of interfaces or abstract classes

– Shims – run-time interception lets you replace calls, even those from the .NET BCL

Visual Studio 2012 Shims – Be Cautious

• Runtime interception of any .NET method– Uses the profiler to detour calls– “Monkey patching” for .NET

• Use it when you…– Have external components that cannot be

refactored• SharePoint, ASP.NET, Office, etc.

– Need to override non-virtual methods• Static methods, constructors, sealed types,

properties

– Have legacy code with untestable designs

Code Reviews

Purpose:– Find and fix bugs early in the process

(much cheaper and easier than later)– Adherence to development standards– Improve consistency– Improve comments and maintainability– Share best practices across the team– Educate both experienced and new team

members

– Improve overall structural quality of the code and skillset of the team!

Integrated Code Review

• Provides feedback from other team members

• Shared knowledge across team

• Code reviews can be set as a quality gate

• Source changes highlighted and comments about the changes.

Automated Reviews?

• Static Code Analysis:– Analyze code (MSIL/SQL) based

on best practices (rules)– Rules created by and used at Microsoft– Rulesets:

• Selectable groups of rules allow tailoring to your environment• Rulesets can be further customized for the exact rules you need

– Can support both T-SQL and .NET– Can be ‘enforced’ using check-in policies– Can be reported during the build (including CI and Gated)

• Still not the same as manual/peer reviews

Code Clone Detection

• Reviews common code blocks exposing refactoring opportunities

• Detect code blocks with common structure and approach

• Search is semantic, not just literal

• Detects ‘copy and paste’ errors

• Detects code fragments with a common logical structure

• Review common code and decide how to proceed

DEMONSTRATION

Code Reviews

Code Comparison

Static Analysis

Agenda

• ALM and Quality

• Quality in Requirements

• Quality in Development– Unit Testing– Fakes– Code Reviews– Code Analysis– Code Clones

• Quality in Test– Manual Testing– Exploratory Testing– Automated Testing– Lab Environments

• Conclusion

Changes in MTM 2012

• Performance• Exploratory Testing• Multi-line test steps• Test data reduction through test settings configuration• Clone test suites via command line ("versioning")• Rich-text in test steps• Improved Test Step grid usability• Read-only test case access from within Test Runner• Pass/Fail test cases without using Test Runner• Enhanced view of results for Test Plan• Manual testing for Metro-style applications

Initiating Exploratory Testing

or no work item.

Explore based on specific work item(s)…

Performing Exploratory Testing

• Familiar Test Runner interface customized for exploratory testing

• Free-form comment area allows tester to record suggestions and problems encountered during the session

• Comment area allows for rich-text, attachments, and easy insertion of screenshot

• Session can be paused to perform activities outside of the Test Runner

• Bug and Test Case work items can readily be created during the exploratory testing session

Exploratory Testing Artifacts

During exploring, we found a bug. Let’s create a work item…

Our testing steps are captured and we can keep only those related to

our bug.

During exploring, we want to

capture actions as a test case for regression…

Here’s all our steps, we can change them to create the

test case exactly as we want.

Questions?

Want to know more...?

Imaginet’s New Visual Studio 2012 Website!

http://visualstudio.imaginet.com

Visit Imaginet’s new Visual Studio 2012 website, your one-stop hub for all your Visual Studio 2012 needs!

For attendees of today’s session that fill out the survey

ALM Assessment Workshop• One week on-site workshop• 25% discount when ordered in the next 2 weeks*

Free Web Training Subscription Offer• Receive 1 free Imaginet On Demand web training subscription • Good for 1 person for 1 month

* Only 1 discount allowed per customer per 6-month period

Upcoming Class – Tester Training with VS 2012

This four-day instructor-led course provides students with the knowledge and skills to use the latest testing tools provided by Visual Studio 2012 to support a variety of different testing needs (manual and automated).

Date: November 12-16, 2012Location: Dallas (Irving, TX)Price: $2,375

BONUS FOR WEBCAST ATTENDEESBuy one, get on for 50% off!

Registration link will be included in our follow-up email later today!

Testers Training Using Visual Studio 2012 ALM Tools

(4 Days Class)

TFS / Visual Studio 2012

• On-Demand Test Environments• October 25 (1:00-2:30pm CT)

• Quality Coding: What's New with Visual Studio 2012• October 29 (1:00-2:30pm CT)

• Branching and Merging and Bears, Oh My! • November 1(1:00-2:00pm CT)

• Managing Test Labs Without the Headaches• November 8 (1:00-2:30pm CT)• November 29 (1:00-2:30pm CT)

Upcoming Fall Workshops & Webcasts:• Lean, Kanban, and TFS

• December 3 (1:00-2:30pm CT)

• Approaches to Kanban with TFS• December 6 (1:00-2:30pm CT)• December 20 (1:00-2:30pm CT)

• Streamline Your Testing with Visual Studio 2012 Testing Tools• December 13 (1:00-2:30pm CT)

• Getting Started with Coded UI Testing: Building Your First Automated Test• December 17 (1:00-2:30pm CT)

Email us at:[email protected]

ALM Planning & Implementation ServicesALM Planning • ALM Assessment & Envisioning Workshops

(3 or 5 days)• VS & TFS Migration Planning Workshop

(5 days)• Microsoft Dev. Tools Deployment Planning

– TFS Deployment Planning (5 days)– Visual SourceSafe to TFS Migration Planning (3 Days)– Visual Studio Quality Tools Deployment Planning

(5 days)

TFS Adoption or Upgrade• TFS 2010 Adoption Quick Start

(5 or 10 days)• TFS 2012 Adoption Quick Start

(5 or 10 days)• TFS 2010 Upgrade Quick Start (10 days)• TFS 2012 Upgrade Quick Start (10 days)

Remote Support• Remote Support for TFS & Visual Studio

Lab• Visual Studio Lab Management Quick Start

(10 days)Testing

• Manual Testing with Test Manager Quick Start (5 days)

• Visual Studio Testing Tools Quick Start(10 days)

• Visual Studio Automated Testing Quick Start (5 days)

• Visual Studio Load Testing Quick Start (5 or 10 Days)

Builds• Automated Build & Release Management

Quick Start (5 days)• Automated Build Center of Excellence (CoE)

Database• Visual Studio Database Tools Quick Start (10

days)

Integrations• Team Foundation Server (TFS) & Project

Server Integration Quick Start (10 days)• TFS & Quality Center Integration/Migration

Quick Start (10 days)

For questions or more information,please contact us at:

[email protected] or (972)607-4830

http://www.imaginet.com