create unit tests

Post on 02-Jan-2017

235 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CreateUnitTests

Hans-PetterHalvorsen,M.Sc.

StepbystepExercises

LevelsofTestingUnitTesting:Testeachpartsindependentlyandisolated

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

SystemTesting:Testthewholesystem

RegressionTesting:Testthatitstillworksafterachangeinthecode

WhatareUnitTests?• UnitTesting(orcomponenttesting)referstoteststhatverifythefunctionalityofaspecificsectionofcode,usuallyatthefunctionlevel.

• Inanobject-orientedenvironment,thisisusuallyattheclassandmethodslevel.

• UnitTestsaretypicallywrittenbythedevelopersaspartoftheprogramming

• Itisalsoknownascomponenttesting.• TypicallyAutomaticallyexecuted(e.g.,VisualStudioandTeam

FoundationServerhavebuilt-infunctionalityforUnitTesting

Theory

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

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

• UnitTestsareimportantpartofSoftwareDevelopmenttoday– eitheryouareusingTDDornot

Theory

UnitTestsFrameworksUnitTestsFrameworkareusuallyintegratedwiththeIDE• VisualStudioUnitTestFramework.UnitTestsarebuilt intoVisualStudio (noadditional

installationneeded)Others:• JUnit (Java)

– JUnitisaunittestingframeworkfortheJavaprogramminglanguage.• NUnit (.NET)

– NUnit isanopensourceunittestingframeworkforMicrosoft.NET.ItservesthesamepurposeasJUnitdoes intheJavaworld

• PHPUnit (PHP)• LabVIEWUnitTestFrameworkToolkit• etc.Allofthemworkinthesamemanner– butwewillusetheVisualStudioUnitTestFramework

Theory

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

ExercisesVisualStudio:1. CreateUnitTestsinVisualStudio2. CodeCoverage

Project:4. CreateUnitTestsforyourCodeinyourProject

CreateUnitTestsinVisualStudio• VisualStudiohavebuilt-infeaturesforUnitTesting

• InthisexamplewewillcreateasimpleClassLibrary

• ThenwewillcreateaTestClassinordertotestourcode(includeaTestProjectinourSolution)

http://msdn.microsoft.com/en-us/library/ms182532.aspx

CreatetheCodeyouwanttoTest

Bank

ClassLibrary

CreateNewProject

StepbystepExample

9

Rename“Class1.cs”->“BankAccount.cs”

using System;namespace BankAccountNS{

public class BankAccount{

private string m_customerName;

private double m_balance;

public BankAccount(string customerName, double balance){

m_customerName = customerName;m_balance = balance;

}

public double Balance{

get { return m_balance; }}

public void Debit(double amount){

if (amount > m_balance){

throw new ArgumentOutOfRangeException("amount");}

if (amount < 0){

throw new ArgumentOutOfRangeException("amount");}

m_balance += amount;}

}}

CreatethefollowingClassandMethods:

ThenBuildtheProjecttomakesureitworkswithouranyerrors.

AddNewProject:UnitTestProject

CreateaproperNameforyourProject

11

YourSolutionshouldnowhave2ProjectsAddReferencetotheCodeunderTest

CreateTestClass“BankAccountTests.cs”WeneedaTestClassforverifying the“BankAccount”class.WecanusetheUnitTest1.csthatwasgeneratedbytheprojecttemplate,butweshouldgivethefileandclassmoredescriptivenames.Wecando thatinonestepbyrenaming thefileinSolutionExplorer.

using System;using Microsoft.VisualStudio.TestTools.UnitTesting;

using BankAccountNS;

namespace BankTest{

[TestClass]public class BankAccountTests{

[TestMethod]public void TestMethod1(){}

}}

Makesuretoaddreferencetothecodeunder test

BankAccountTests.cs

Note!TheTestClassshouldbelabeled[TestClass]andtheTestMethodsneedtobelabeled[TestMethod]

ReferencetoTestFramework

TestMethodRequirementsAtestmethodmustmeetthefollowingrequirements:• Themethodmustbedecoratedwiththe[TestMethod]attribute.• Themethodmustreturnvoid.• Themethodcannothaveparameters.

Theory

BasicConceptinUnitTesting

...

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

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

Example:

CompareFeilmargin Feilmelding somvises

hvistestenfeilerAssert:Hevde/bedyre

AllUnitTestsFrameworkhavetheAssertClass

TheAssertClasscontainsdifferentMethods thatcanbeusedinUnitTesting

Theory

Assert:Hevde/bedyre

CreatethefirstTestMethodWewillwriteunittestmethods toverify thebehaviorofthe“Debit”methodofthe“BankAccount”class

[TestMethod]public void Debit_WithValidAmount_UpdatesBalance(){

// arrangedouble beginningBalance = 11.99;double debitAmount = 4.55;double expected = 7.44;

BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

// actaccount.Debit(debitAmount);

// assertdouble actual = account.Balance;

Assert.AreEqual(expected, actual, 0.001, "Account not debited correctly");}

Debit_WithValidAmount_UpdatesBalance()CreatethefollowingMethodinthe“BankAccountTests.cs”Class:

Expected=Balance- debitAmount=11.99- 4.55=7.44

RunningtheUnitTestusingthe“TestExplorer”

17

IfTestExplorerdoesnotappearafterasuccessfulbuild, chooseTest onthemenu, thenchooseWindows,andthenchooseTestExplorer.

Attheendofthetestrun,thebarturnsgreenifallthetestmethodspass,orredifanyofthetestsfail.

Inthiscase,thetestdoesfail.Thetestmethod ismoved totheFailedTests.group.

Selectthemethod inTestExplorertoviewthedetailsatthebottomofthewindow.

CanyoufindtheBugbasedontheTestResults?

AnalyzingtheTestResults

• Thetestresultcontainsamessagethatdescribesthefailure.• FortheAreEqualsmethod,messagedisplaysyouwhatwasexpected(the

(Expected<XXX>parameter)andwhatwasactuallyreceived(theActual<YYY> parameter).

• Wewereexpectingthebalancetodeclinefromthebeginningbalance,butinsteadithasincreasedbytheamountofthewithdrawal.

• AreexaminationoftheDebitcodeshowsthattheunittesthassucceededinfindingabug.Theamountofthewithdrawalisaddedtotheaccountbalancewhenitshouldbesubtracted.

m_balance += amount;

CorrectiontheBugm_balance += amount; m_balance -= amount;->

ReruntheTestInTestExplorer, chooseRunAll torerun thetest.Thered/greenbarturnsgreen,andthetestismovedtothePassedTests group.

YouarefinishedwiththeExercise

CodeCoverage• Codecoverageisameasureusedinsoftwaretesting.Itdescribes

thedegreetowhichthesourcecodeofaprogramhasbeentested.

• Dependingontheinputarguments,differentpartsofthecodewillbeexecuted.UnitTestsshouldbewrittentocoverallpartsofthecode.

RunCodeCoverage• Weseethatourtestonlycoverabout43%

ofthecodeinour“Debit”Method.• ThisisbecausetheMethodcontainsif

sentences• Weshould improvetheUnitTestingsothat

wecovermoreofthecode.

=>TrytoImprovetheUnitTestingsoitcovers100%ofthecodeintheDebitMethod

YouarefinishedwiththeExercise

MoreaboutUnitTestinginVisualStudio

http://msdn.microsoft.com/en-us/library/dd264975.aspx

Ifyouwanttolearnmore,visitthefollowing:

VerifyingCodebyUsingUnitTests:

Microsoft.VisualStudio.TestTools.UnitTestingTheUnitTestingnamespace,whichprovidesattributes,exceptions,asserts,andotherclassesthatsupportunittesting.Microsoft.VisualStudio.TestTools.UnitTesting.WebTheUnitTesting.Webnamespace,whichextendstheUnitTestingnamespacebyprovidingsupport forASP.NETandWebserviceunittests.

ProjectAssignment• CreateUnitTestsforsomepartsofyourCodeinyourProject

YouarefinishedwiththeExercise

Poenget er ikke å luretestene,menå sørge foratkoden er feilfri!

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

Theory

References• I.Sommerville,SoftwareEngineering:Pearson,2015.• Wikipedia:UnitTesting:http://en.wikipedia.org/wiki/Unit_test• Wikipedia:JUnit:http://en.wikipedia.org/wiki/JUnit• Wikipedia:NUnit:http://en.wikipedia.org/wiki/NUnit• CreateUnitTestsinVisualStudio:

http://msdn.microsoft.com/en-us/library/ms182532.aspx• CourseatUiO:FoundationsofSoftwareTesting:

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

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:hans.p.halvorsen@hit.noBlog:http://home.hit.no/~hansha/

top related