component testing ( unit testing )

Download Component Testing  ( Unit  Testing )

If you can't read please download the document

Post on 08-Feb-2016

34 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Component Testing ( Unit Testing ). Introduction to Unit Testing. Nikolay Kostov. Technical Trainer. http://www.nikolay.it. Telerik QA Academy. http://qaacademy.telerik.com. Table of Contents. What is Unit Testing ? Code and Test vs. Test Driven Development Unit testing Frameworks - PowerPoint PPT Presentation

TRANSCRIPT

High-Quality Programming Code Construction

Component Testing (Unit Testing)Introduction to Unit TestingNikolay KostovTechnical Trainerhttp://www.nikolay.it

Telerik QA Academyhttp://qaacademy.telerik.com , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Table of ContentsWhat is Unit Testing?Code and Test vs. Test Driven DevelopmentUnit testing FrameworksVisual Studio Team TestNunitGallioUnit Testing Best Practices

, , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , *2##What is Unit Testing?

, , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Unit Test DefinitionA 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.Program testing can be used to show the presence of bugs, but never to show their absence!Edsger Dijkstra, [1972] , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , *4##Manual TestingYou have already done unit testingManually, by handManual tests are less efficientNot structuredNot repeatableNot on all your codeNot easy to do as it should be5

, , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Unit Test Exampleint Sum(int[] array){ sum = 0; for (int i=0; i returns sum of a and bpublic void Sum_AddsOneAndTwo(){ int result = Sum(1,2); Assert.AreEqual(4, result, "Bad sum");}60 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , What Should Assert Messages Say?Assert message in a test is one of the most important thingsTells us what we expected to happen but didnt, and what happened insteadGood assert message helps us track bugs and understand unit tests more easilyExample:"Withdrawal failed: accounts are not supposed to have negative balance."61 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , What Should Assert Messages Say? (2)Express what should have happened and what did not happenVerify() did not throw any exceptionConnect() did not open the connection before returning itDo not:Provide empty or meaningless messages Provide messages that repeat the name of the test case62 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Avoid Multiple Asserts in a Single Unit TestAvoid multiple asserts in a single test caseIf the first assert fails, the test execution stops for this test caseAffect future coders to add assertions to test rather than introducing a new one63void TestSum_AnyParamBiggerThan1000IsNotSummed() { Assert.AreEqual(3, Sum(1001, 1, 2); Assert.AreEqual(3, Sum(1, 1001, 2); Assert.AreEqual(3, Sum(1, 2, 1001);} , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Unit Testing The ChallengeThe concept of Unit Testing has been around for many yearsNew methodologies in particular XP, have turned unit testing into a cardinal foundation of software developmentWriting good & effective Unit Tests is hard!This is where supporting integrated tools and suggested guidelines enter the pictureThe ultimate goal is tools that generate unit tests automatically64 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Mock Objects UsageQ: When should mock objects be used?A: Mock objects are used when one needs to replace or remove dependencies from code under testExample:Class LoginManager manages user logins with the following responsibility:When login fails, class reports to a logger class65 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Mock Objects Usage (2)The unit test should test the class logicWithout having to configure or rely on the availability of the loggerSo we replace the logger class with a "fake" one, a mocking object66 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Component Testing (Unit Testing)Questions???????????http://academy.telerik.com , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , ExercisesWrite three classes: Student, Course and School. Students should have name and unique number (inside the entire School). Name can not be empty and the unique number is between 10000 and 99999. Each course contains a set of students. Students in a course should be less than 30 and can join and leave courses.Write VSTT tests for these two classesUse 2 class library projects in Visual Studio: School.csproj and TestSchool.csprojExecute the tests using Visual Studio and check the code coverage. Ensure it is at least 90%.68 , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , Useful LinksA Unit Testing Walkthrough with Visual Studio Team Test http://msdn.microsoft.com/en-us/library/ms379625(VS.80).aspxNUnit www.nunit.orgExtreme Programming www.extremeprogramming.orgXP Programming www.xprogramming.comAdvanced Unit Testing www.codeproject.com/csharp/autp1.asp , , SEO - , HTML, CSS, JavaScript, Photoshop ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC " cloud "BG Coder - - online judge , " " , ASP.NET - , , C#, .NET, ASP.NET iPhone, Android, WP7, PhoneGapfree C# book, C#, Java, C# - - C# , , *69##

Recommended

View more >