testes? mas isso não aumenta o tempo de projecto? não quero

72
Testes? Mas isso não aumenta o tempo de projecto? Não quero... Pedro Rosa http://netpont o.org 1ª Reunião em Coimbra - 09/04/2011

Upload: comunidade-netponto

Post on 13-May-2015

2.773 views

Category:

Technology


2 download

DESCRIPTION

Os Testes são cada vez mais uma necessidade nos projectos de desenvolvimento de software... Sejam eles unitários, de carga ou de "User Interface", uma boa framework de testes ajuda a resolver os problemas mais cedo, de forma mais eficaz e mais barata. No final da sessão vamos perceber não só para que servem, como são feitos e como o Visual Studio 2010 pode ajudar.

TRANSCRIPT

Page 1: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Testes? Mas isso não aumenta o tempo de projecto? Não quero...

Pedro Rosa

http://netponto.org1ª Reunião em Coimbra - 09/04/2011

Page 3: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Pedro Rosa

LinkedInhttp://pt.linkedin.com/in/pedrobarraurosa

Twitterhttp://twitter.com/pedrorosa

Page 4: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Agenda

1. Why?2. Code Coverage3. Static / Dynamic Analysis4. Unit Testing5. WebTesting6. Load (Performance) Testing

Page 5: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Também disponível em vídeo...

Assista!http://www.vimeo.com/22446417

Page 6: Testes? Mas isso não aumenta o tempo de projecto? Não quero

1996 1998 2000 2002 2004 20060%

50%

100%

150%

Percent OverrunFailed Succedded Challenged

Source: Standish Group, 2006 Third Quarter Research Report, CHAOS Research Results

The State of Application Development

• Only 1 in 3 projects succeed • Projects cost 50% more then budgeted• Lack of software quality cost US businesses $59B / year.• Bugs account for 55% of downtime

Page 7: Testes? Mas isso não aumenta o tempo de projecto? Não quero

on average professional coders make 100 to 150 errors in every 1000 lines of code they write

software bugs cost the US economy an estimated $59.5 billion every year

last year, canceled projects cost firms $67 billion; overruns on the other projects racked up another $21billion

Page 8: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Need something a little more concrete?

Page 9: Testes? Mas isso não aumenta o tempo de projecto? Não quero

USS Yorktown, SmartShip

• crew member entered 0 in a data entry field, caused a “divide by 0” error that shut down propulsion

• dead in the water for 2hrs 45mins

Page 10: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Ariane 5 Flight 501• re-used code from ariane 4, but

took a different flight path because of different booster design

• conversation from 64bit float to 16bit signed int caused overflow (exception handler was disabled for perf reasons)

• cost: > $370 million @ 1996 prices

Page 11: Testes? Mas isso não aumenta o tempo de projecto? Não quero

F-22 Raptor• deploying overseas to japan the

first time

• crossed international dateline, computers crashed losing all navigation and communications systems

• clear weather allowed them to follow tankers back to hawaii

Raptor is likely the most advanced manned aircraft that will ever fly…

Page 12: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Need something a little more visual?

Page 13: Testes? Mas isso não aumenta o tempo de projecto? Não quero
Page 14: Testes? Mas isso não aumenta o tempo de projecto? Não quero
Page 15: Testes? Mas isso não aumenta o tempo de projecto? Não quero
Page 16: Testes? Mas isso não aumenta o tempo de projecto? Não quero
Page 17: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Still Not Convinced?

Page 18: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Training

Conditioning

Climbing

Training Phase

Cost of Failure

Page 19: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Development

Test

Release

Software Phase

Cost of Bugs

Page 20: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Push Quality Upstream

Development

Test

Release

Software Phase

Cost of Bugs

Page 21: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Generalist SpecialistManual Testing Some scripting

Creates scripts to set up lab, create data

Strong scripting skills

Some coding skills

Strong coding

Develops fully automated testing procedures

Expert coding skills

Where does testing happen?

Black Box Testing

White Box Testing

API Testing

70% of testing happens here

Majority of test tools target here

Page 22: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio 2010

Page 23: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Test Manager 2010

Manual Testing

Layer Diagram

UML Modeling

Load Testing Web Testing

Test Case ManagementIntelliTrace™

Architecture Explorer Fast Forward for Manual Testing

Logical Class Designer

Cloud Development

Office Development

Windows Development

New WPF Editor Customizable IDE

Multi-core DevelopmentSilverlight Tools

Web Development

SharePoint Development

Generate from Usage

Static Code Analysis

Database Deployment

Code Metrics

Database Unit Testing Test Data Generation

Test Impact AnalysisUI Test Automation

Code Coverage

Performance Profiling

Database Change Mgmt

Page 24: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio 2010 Test Types Unit Test Database Unit Test Web Performance Test Load Test Manual Test (Test Case) Generic Test Ordered Test Coded UI Test

Page 25: Testes? Mas isso não aumenta o tempo de projecto? Não quero

What is Unit Testing?

Page 26: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Test – Definition

“Program testing can be used to show the presence of bugs, but never to show their absence!”

Edsger Dijkstra, [1972]

A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested.

Page 27: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Manual Testing You have already done unit testing

Manually, by hand

Manual tests are less efficient Not structured Not repeatable Not on all your code Not easy to do as it should be

Page 28: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Test Exampleint Sum(int[] array){ sum = 0; for (int i=0; i<array.Length; i++) sum += array[i]; return sum;}

void TestSum(){ if (Sum(new int[]{1,2}) != 3) throw new TestFailedException("1+2 != 3"); if (Sum(new int[]{-2}) != -2) throw new TestFailedException("-2 != -2"); if (Sum(new int[]{}) != 0) throw new TestFailedException("0 != 0");}

Page 29: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing Some Facts• Tests are specific pieces of code• Unit testing framework is needed– Visual Studio Team Test– NUnit– MbUnit

• Unit tests are written by developers, not by QA engineers

• Unit tests are released into the code repository along with the code they test

Page 30: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing – More Facts• All classes should be tested– Test anything that could have bugs

• All methods should be tested– Trivial code may be omitted – E.g. property getters and setters

• Ideally all unit tests should pass before check-in into the source control repository

Page 31: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Why Unit Tests?Why Unit Tests?• Unit tests dramatically decrease the number of

defects in the code • Unit tests improve design • Unit tests are good documentation• Unit tests reduce the cost of change• Unit tests allow refactoring• Unit tests decrease the defect-injection rate due to

refactoring / changes

Page 32: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Code and Test vs. Test Driven Development

Page 33: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing ApproachesUnit Testing Approaches

• "Code and Test" approach• Classical approach

• "Test First" approach• Test driven development (TDD)

Page 34: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Write code

Write unit test

Run and succeed

Code and Test ApproachCode and Test Approach

Time flow

Page 35: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Pick а test

Compile and fail

Write code to pass test

Write enough code to compile

Run test and fail

Create a test list

Test Driven DevelopmentTest Driven Development

Time flow

Write test

Remove duplication

Page 36: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Why Test Driven Development?

• Helps find design issues early and avoids rework

• Writing code to satisfy a test is a focused activity – less chance of error

• Tests will be a more comprehensive than when written after code

Page 37: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing Frameworks and Visual Studio Team Test

Page 38: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing Frameworks

• JUnit– The first popular unit testing framework– Based on Java

• Similar frameworks have been developed for a broad range of computer languages– NUnit – for C# and all .NET languages– cppUnit, jsUnit, PhpUnit, PerlUnit, ...

• Visual Studio Team Test (VSTT)– Developed by Microsoft, integrated in VS

Page 39: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio Team Test – Features• Team Test (TT) is very well integrated with

Visual Studio–Create test projects and unit tests– Execute unit tests–View execution results–View code coverage

• Located in the assembly Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll

Page 40: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio Team Test – Attributes• Test code is annotated using custom attributes

[TestClass] – denotes a class holding unit tests [TestMethod] – denotes a unit test method [ExpectedException] – test causes an exception [Timeout] – sets a timeout for test execution [Ignore] – temporary ignored test case [ClassInitialize], [ClassCleanup] – setup /

cleanup logic for the testing class [TestInitialize], [TestCleanup] – setup /

cleanup logic for each test case

Page 41: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Assertions

• Predicate is a true / false statement • Assertion is a predicate placed in the program

code – Indicates that the developer thinks that the predicate is

always true at that place– If an assertion fails, the method call does not return

and an error is reported– Example:Assert.AreEqual(expectedValue, actualValue, "Error message.");

Page 42: Testes? Mas isso não aumenta o tempo de projecto? Não quero

VSTT – Assertions• Assertions check condition and throw exception if condition is not satisfied• Comparing values

– AreEqual(expected value, calculated value [,message]) – compare two values for equality

• Comparing objects

– AreSame(expected object, current object [,message]) – compare object references

Page 43: Testes? Mas isso não aumenta o tempo de projecto? Não quero

VSTT – Assertions (2)• Checking for null value– IsNull(object [,message])– IsNotNull(object [,message])

• Conditions– IsTrue(condition)

– IsFalse(condition)

• Forced test fail– Fail(message)

Page 44: Testes? Mas isso não aumenta o tempo de projecto? Não quero

The 3A Pattern• Arrange all necessary preconditions and inputs• Act on the object or method under test• Assert that the expected results have occurred[TestMethod]public void TestDeposit(){ BanckAccount account = new BanckAccount(); account.Deposit(125.0); account.Deposit(25.0); Assert.AreEqual(150.0, account.Balance, "Balance is wrong.");}

Page 45: Testes? Mas isso não aumenta o tempo de projecto? Não quero

VSTT – Examplepublic class Account{ private decimal balance; public void Deposit(decimal amount) { this.balance += amount; } public void Withdraw(decimal amount) { this.balance -= amount; } public void TransferFunds( Account destination, decimal amount) { ... } public decimal Balance { ... }}

Page 46: Testes? Mas isso não aumenta o tempo de projecto? Não quero

VSTT – Example (2)using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]public class AccountTest{ [TestMethod] public void TransferFunds() { Account source = new Account(); source.Deposit(200.00M); Account dest = new Account(); dest.Deposit(150.00F); source.TransferFunds(dest, 100.00F); Assert.AreEqual(250.00F, dest.Balance); Assert.AreEqual(100.00F, source.Balance); }}

Page 47: Testes? Mas isso não aumenta o tempo de projecto? Não quero

VSTT – Screenshot

Page 48: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio Team Test

Live Demo

Page 49: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing Best Practices

Page 50: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Naming Standards for Unit Tests

• The test name should express a specific requirement that is tested–Usually prefixed with [Test]– E.g. TestAccountDepositNegativeSum()

• The test name should include– Expected input or state – Expected result output or state–Name of the tested method or class

Page 51: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Naming Standards for Unit Tests Example

• Given the method:

with requirement to ignore numbers greater than 100 in the summing process

• The test name should be:

Sum_NumberIgnoredIfGreaterThan100

public int Sum(params int[] values)

Page 52: Testes? Mas isso não aumenta o tempo de projecto? Não quero

When Should a Test be Changed or Removed?

• Generally, a passing test should never be removed– These tests make sure that code changes don’t break

working code• A passing test should only be changed to make it

more readable• When failing tests don’t pass, it usually means there

are conflicting requirements

Page 53: Testes? Mas isso não aumenta o tempo de projecto? Não quero

When Should a Test be Changed or Removed? (2)

• Example:

• New features allows negative numbers

[ExpectedException(typeof(Exception), "Negatives not allowed")]void Sum_FirstNegativeNumberThrowsException(){ Sum (-1,1,2);}

Page 54: Testes? Mas isso não aumenta o tempo de projecto? Não quero

When Should a Test be Changed or Removed?(3)

• New developer writes the following test:

• Earlier test fails due to a requirement change

void Sum_FirstNegativeNumberCalculatesCorrectly(){ int sumResult = sum(-1, 1, 2); Assert.AreEqual(2, sumResult);}

Page 55: Testes? Mas isso não aumenta o tempo de projecto? Não quero

When Should a Test be Changed or Removed? (4)

• Two course of actions:1. Delete the failing test after verifying if it’s valid2. Change the old test:

• Either testing the new requirement• Or test the older requirement under new settings

Page 56: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Tests Should Reflect Required Reality

• What’s wrong with the following test?

• A failing test should prove that there is something wrong with the production code– Not with the unit test code

int Sum(int a, int b) –> returns sum of a and b

public void Sum_AddsOneAndTwo(){ int result = Sum(1,2); Assert.AreEqual(4, result, "Bad sum");}

Page 57: Testes? Mas isso não aumenta o tempo de projecto? Não quero

What Should Assert Messages Say?

• Assert message in a test is one of the most important things– Tells us what we expected to happen but didn’t, and what

happened instead– Good assert message helps us track bugs and understand

unit tests more easily• Example:– "Withdrawal failed: accounts are not supposed to have

negative balance."

Page 58: Testes? Mas isso não aumenta o tempo de projecto? Não quero

What Should Assert Messages Say? (2)

• Express what should have happened and what did not happen– “Verify() did not throw any exception”– “Connect() did not open the connection before

returning it”• Do not:– Provide empty or meaningless messages – Provide messages that repeat the name of the test case

Page 59: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Avoid Multiple Asserts in a Single Unit Test

• Avoid multiple asserts in a single test case– If the first assert fails, the test execution stops for this test

case– Affect future coders to add assertions to test rather than

introducing a new one

void Sum_AnyParamBiggerThan1000IsNotSummed() { Assert.AreEqual(3, Sum(1001, 1, 2); Assert.AreEqual(3, Sum(1, 1001, 2); Assert.AreEqual(3, Sum(1, 2, 1001);}

Page 60: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Unit Testing – The Challenge

• The concept of Unit Testing has been around for many years

• New methodologies in particular XP, have turned unit testing into a cardinal foundation of software development

• Writing good & effective Unit Tests is hard!– This is where supporting integrated tools and suggested

guidelines enter the picture• The ultimate goal is tools that generate unit tests

automatically

Page 61: Testes? Mas isso não aumenta o tempo de projecto? Não quero

What is a load test?

• An automated collection of tests run in parallel to simulate multiple users or systems• Load tests run software unit tests, database unit tests,

and web performance tests• Visual Studio captures details metrics while the load test

runs

Page 62: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Load Test ConceptsScenario• A collection of load test parameters that

simulate real world scenario.Load Pattern• Determines the number of users the scenario

simulates. May be constant or stepped to simulate an increasing number of users

Test Mix• Determines the unit tests and web tests.Network Mix• Determines the connection speeds used by

virtual usersRun Settings• A set of properties that determine the way the

load test runs

Page 63: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Core Scenarios

• Single user performance testing• Create and debug Web

Performance Tests• Use Coded UI tests to measure

end user performance• Run performance tests• Analyze results• Report to stakeholders• Fix the problems using the

profiler

Page 64: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Network Emulation

• Productizing NEWT network device driver• Throttles bandwidth, introduces latency and errors

at the network layer• Scenarios• Single user testing over slow network• Automated performance testing over slow links

Page 65: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Web Performance Test Recorder

Visual Studio

Recorder Listener

Internet Explorer

Browser Control

Wininet Shim

Wininet

Browser Recorder

Wininet Recorder

Browser events

Intercepts wininet calls

Merging Recorder

Record events

Record eventsRecord events

Recorder BHO

Visual Studio Launches Internet Explorer

Page 66: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Web Test Recorder Plugin

Visual Studio

Recorder Listener

Record events

Internet Explorer

Browser Control

Wininet Shim

Wininet

Browser Recorder

Wininet Recorder

Browser events

Intercept s wininet calls

Merging Recorder

Record events

Record events

Recorder BHO

Visual Studio Launches Internet Explorer

WebTest RecorderPlugins

Recorder Result

Recorded Web Test

Page 67: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Load Test Agent ArchitectureVisual Studio Ultimate 2010

Test Controller

Test Controller manages test agents

Test agents run tests and collect data

Test agent #1

CollectorAgent

Asp.NetProfiler

Target Server

Test agent #2

Test AgentRun Tests

Test agent #3

Page 68: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Visual Studio Team Test

Live Demo

Page 69: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Questions?

??

? ? ??

?? ?

Page 71: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Próximas reuniões presenciais

• 09/04/2011 - Abril (Coimbra)• 16/04/2011 - Abril (Lisboa)• 21/05/2011 - Maio (Lisboa)• 18/06/2011 - Junho (Lisboa)

Reserva estes dias na agenda! :)

Page 72: Testes? Mas isso não aumenta o tempo de projecto? Não quero

Obrigado!

Pedro Rosahttp://pt.linkedin.com/in/pedrobarraurosahttp://twitter.com/pedrorosa