unit testing video€¦ · test driven development (tdd) • coding and testing are done in...

54
Unit Testing Hans-Petter Halvorsen, M.Sc.

Upload: others

Post on 07-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTesting

Hans-PetterHalvorsen,M.Sc.

Page 2: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Contents

1.WhatisTesting?– ShortIntroductiontoTesting

2.WhatisUnitTesting?3. UnitTestinginVisualStudio

Page 3: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

IntroductiontoTesting

Hans-PetterHalvorsen,M.Sc.

Page 4: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

RequirementsAnalysis

Design

Implementation

Testing

Maintenance

PlanningDeployment

SRS

SDD

STD

Code

InstallationGuides

UserGuides

GanttChart

withERDiagram,UMLDiagrams,CADDrawings

TestDocumentation

SoftwareRequirementsSpecifications

SoftwareDesignDocumentsSystemDocumentation

SoftwareTestPlan(STP)

ProjectPlanning

End-UserDocumentation

SystemDocumentation

SoftwareTestDocumentation

SDPSoftwareDevelopment

Plan

GanttChart

TheSoftwareDevelopment

Lifecycle(SDLC)

Page 5: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

WhyFindBugsearly?

SoftwareDevelopmentLifeCycle(SDLC)

Page 6: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

ValidationTesting DefectTesting

Testing

DemonstratetotheDeveloperandtheCustomerthattheSoftwaremeetsitsRequirements.

I.Sommerville,SoftwareEngineering,10ed.:Pearson,2015.

CustomSoftware:Thereshould beatleastonetestforeveryrequirement intheSRSdocument.GenericSoftware:Thereshould betestsforallofthesystemfeaturesthatwillbeincluded intheproductrelease.

Find inputsorinput sequenceswherethebehaviorofthesoftwareisincorrect,undesirable,ordoesnotconformtoitsspecifications.Thesearecausedbydefects(bugs) inthesoftware.

Page 7: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestCategoriesBlack-boxvs.White-boxTesting

White-boxTesting:Youneedtohaveknowledgeofhow(DesignandImplementation)thesystemisbuilt

Black-boxTesting:Youneednoknowledgeofhowthesystemiscreated.

TypicallydonebyDevelopers,etc

Page 8: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

LevelsofTesting

UnitTesting

IntegrationTesting

SystemTesting

AcceptanceTesting

Anymodule,program,objectseparatelytestable

Interfacebetweencomponents; interactionswithothersystems(OS,HW,etc)

Thebehaviorofthewholeproduct(system)asdefinedbythescopeoftheproject

Istheresponsibility ofthecustomer– ingeneral.Thegoalistogainconfidence inthesystem;especiallyinitsnon-functional characteristics

Page 9: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

LevelsofTestingUnitTesting:Testeachpartsindependentlyandisolated

IntegrationTesting:Makesurethatdifferentpiecesworktogether.TesttheInterfacesbetweenthedifferentpieces.Interactionwithothersystems(Hardware,OS,etc.)

SystemTesting:Testthewholesystem

RegressionTesting:Testthatitstillworksafterachangeinthecode,i.e.,runallUnitTests,etc.

Page 10: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

LevelsofTesting

UnitTesting

RegressionTesting

IntegrationTesting

System/ValidationTesting

AcceptanceTesting

Start

Finish

Requirements&DesignStartDevelopment

UnitTestsarewrittenbytheDevelopersaspartoftheProgramming.EachpartisdevelopedandUnittestedseparately(EveryClassandMethod inthecode)

TheCustomerneeds totestandapprove thesoftwarebeforehecantakeitintouse.FAT/SAT.

SystemtestingistypicallyBlack-boxTeststhatvalidatetheentiresystemagainstitsrequirements, i.eCheckingthatasoftwaresystemmeetsthespecifications

Integrationtestingmeansthesystemisputtogetherandtestedtomakesureeverythingworkstogether.

Regressiontestingistestingthesystemtocheckthatchangeshavenot“broken”previouslyworkingcode.BothManually&Automatically(Re-runUnitTests)

Page 11: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTesting

Hans-PetterHalvorsen,M.Sc.

Page 12: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

WhatareUnitTests• UnitTesting(orcomponenttesting)referstoteststhatverifythefunctionalityofaspecificsectionofcode,usuallyatthefunctionlevel.

• Inanobject-orientedenvironment,thisisusuallyattheclassandmethodslevel.

• UnitTestsaretypicallywrittenbythedevelopersaspartoftheprogramming

• Automaticallyexecuted(e.g.,VisualStudioandTeamFoundationServerhavebuilt-infunctionalityforUnitTesting)

Page 13: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestDrivenDevelopment(TDD)• CodingandTestingaredoneinparallel• TheTestsarenormallywrittenbeforetheCode• IntroducedaspartofeXremeProgramming(XP)(anAgilemethod)

• UnitTestsareimportantpartofSoftwareDevelopmenttoday– eitheryouareusingTDDornot

Page 14: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTestsFrameworksUnitTestsFrameworkareusuallyintegratedwiththeIDE• VisualStudioUnitTestFramework.UnitTestsarebuilt intoVisualStudio (noadditional

installationneeded)Others:• JUnit (Java)

– JUnitisaunittestingframeworkfortheJavaprogramminglanguage.• NUnit (.NET)

– NUnit isanopensourceunittestingframeworkforMicrosoft.NET.ItservesthesamepurposeasJUnitdoes intheJavaworld

• PHPUnit (PHP)• LabVIEWUnitTestFrameworkToolkit• etc.

Allofthemworkinthesamemanner– butwewillusetheVisualStudioUnitTestFramework

http://en.wikipedia.org/wiki/Visual_Studio_Unit_Testing_Framework

Page 15: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

BasicConceptinUnitTesting

...

Assert.AreEqual(expected, actual, 0.001, ”Test failed because...");

ThebasicconceptinUnitTestingistoCompare theresultswhenrunningtheMethodswithsomeInputData(“Actual”)withsomeKnownResults(“Expected”)

Example:

CompareErrormargin Errormessageshown if

theTestfails

AllUnitTestsFrameworkhavetheAssertClass

TheAssertClasscontainsdifferentMethods thatcanbeusedinUnitTesting

Page 16: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTests– BestPractice• AUnitTestmustonlydoonething• UnitTestmustrunindependently• UnitTestsmustnotbedependontheenvironment• TestFunctionalityratherthanimplementation• Testpublicbehavior;privatebehaviorrelatestoimplementation

details• AvoidtestingUIcomponents• UnitTestsmustbeeasytoreadandunderstand• CreaterulesthatmakesureyouneedtorunUnitTests(andthey

needtopass)beforeyouareallowedtoCheck-inyourCodeintheSourceCodeControlSystem

http://www.uio.no/studier/emner/matnat/ifi/INF5530

Page 17: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTestinginVisualStudio

Hans-PetterHalvorsen,M.Sc.

Page 18: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UnitTestinginVisualStudio• VisualStudiohavebuilt-infeaturesforUnitTesting

• Weneedtoincludea“TestProject”inourSolution

Page 19: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestMethodRequirements

Atestmethodmustmeetthefollowingrequirements:• Themethodmustbedecoratedwiththe[TestMethod]attribute.

• Themethodmustreturnvoid.• Themethodcannothaveparameters.

Page 20: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Example

Hans-PetterHalvorsen,M.Sc.

UnitTestinginVisualStudio

Page 21: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

ConverttoFahrenheit

𝑇" =95𝑇& + 32

AsimplesketchoftheUserInterface:

ConversionFormula:

CreatethefollowingApplication (e.g.,WinForm ApporASP.NETApp)

Celsius:22 ℃ Convert 71,6 ℉

Fahrenheit:

Page 22: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

UserInterface

Page 23: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CreateyourGUI

Page 24: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 25: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Add Class

Page 26: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Class

𝑇" =95𝑇& + 32

Page 27: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

public class TemperatureConvert{

public double CelciusToFahrenheit(double Tc){

double Tf;

Tf = 9/5 * Tc + 32;

return Tf;}

}

Page 28: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 29: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CreateyourCode

Page 30: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

using System;using System.Windows.Forms;

namespace TemperatureApp{

public partial class Form1 : Form{

public Form1(){

InitializeComponent();}

private void btnConvert_Click(object sender, EventArgs e){

TemperatureConvert temperature = new TemperatureConvert();

double temperatureFahrenheit;double temperatureCelcius;

temperatureCelcius = Convert.ToDouble(txtCelsius.Text);

temperatureFahrenheit = temperature.CelciusToFahrenheit(temperatureCelcius);

txtFahrenheit.Text = temperatureFahrenheit.ToString();

}}

}

Page 31: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 32: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestApplication

𝑇" =95𝑇& + 32

𝑇" =95 , 22 + 32

=71.6WegetwrongAnswer!

Page 33: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 34: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CreateUnitTestProject

Page 35: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Youhavenow2ProjectsinyourSolutionExplorer

Page 36: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

AddReferencetotheCodeunderTest

Page 37: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 38: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CreatetheUnitTestCode

Page 39: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CreatetheUnitTestCode

Page 40: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

using Microsoft.VisualStudio.TestTools.UnitTesting;using TemperatureApp;

namespace UnitTestTemperature{[TestClass]public class UnitTestTempConvert{[TestMethod]public void TestFahrenheitConversion(){TemperatureConvert temperature =new TemperatureConvert();doubletemperatureCelcius =22;doubletemperatureFahrenheitActual;doubletemperatureFahrenheitExpected =71.6;

temperatureFahrenheitActual =temperature.CelciusToFahrenheit(temperatureCelcius);

Assert.AreEqual(temperatureFahrenheitExpected,temperatureFahrenheitActual,0.001,"Temperature conversionnotcorrectly");

}}

}

Page 41: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 42: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestExplorer

Page 43: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

StartRunningtheUnitTest

Page 44: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

TestResults

Page 45: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 46: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Debuggingpublic class TemperatureConvert{

public double CelciusToFahrenheit(double Tc){

double Tf;

Tf = 9/5 * Tc + 32;

return Tf;

}

}

𝑇" =95𝑇& + 32

ProbablyErrorinFormula?Whatiswrong?

Page 47: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

FixingBugspublic class TemperatureConvert

{

public double CelciusToFahrenheit(double Tc){

double Tf;

Tf = Tc*9/5 + 32;

return Tf;

}

}

𝑇" =95𝑇& + 32

Page 48: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Re-runUnitTest

EverythingWorks!TheTestPassed!

Page 49: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 50: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CheckingCodeCoverage

Page 51: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CodeCoverage• Codecoverageisameasureusedinsoftwaretesting.Itdescribes

thedegreetowhichthesourcecodeofaprogramhasbeentested.

• Dependingontheinputarguments,differentpartsofthecodewillbeexecuted.UnitTestsshouldbewrittentocoverallpartsofthecode.

Page 52: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

CodeCoverageResults

InthiscasetheUnitTestcovered100%ofthecode.IfweuseIf…Else…orsimiliar,wetypicallyneedtowriteUnitTestforeachIf…Else…inordertocoveralltheCode

Page 53: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme
Page 54: Unit Testing Video€¦ · Test Driven Development (TDD) • Coding and Testing are done in parallel • The Tests are normally written before the Code • Introduced as part of eXreme

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/