TEST DRIVEN
DEVELOPMENT
+
UNIT TESTING
Using Visual Studio 2010/2012 and Eclipse
By Andrzej Piotrowski
Agenda





Importance of Unit Testing
Test Driven Development
TDD Techniques
Unit test in .net (VS2010)
1.
2.
3.



MSTest
nUnit
Mock

Big Projects (VS2010 and Eclipse)
Unit Testing



A unit is the smallest testable part of software.
In object-oriented programming, the smallest unit
is a method, which may belong to a base/super
class, abstract class or derived/child class.
Acceptance Testing

System Testing

Integration Testing

Unit Testing
Unit testing myths











“Testers test, developers develop”
“There’s no time to write tests.”
“We’ll write the unit tests later.”
“The code will change, so the tests will fail
in the (near) future.”
“The test we wrote some time ago
(probably) don’t work anymore.”
“Not easy to maitain for big projects”
“Time waste for existing projects”
Test Driven Development (TDD)
 A software development technique that uses short

development iterations based on pre-written test cases that
define desired improvements or new functions.
 Each iteration produces code necessary to pass that

iteration's tests. Finally, the programmer or team refactors
the code to accommodate changes.
 A key TDD concept is that preparing tests before coding

facilitates rapid feedback changes.
Metric of usage TTD in Projects


Level 0 – huh?




Level 1 – we write unit tests.




We write a unit test, then some code, then some more code.

Level 3 – we do Test-Driven Development




We wrote some tests for critical parts.

Level 2 – we write (most of) our unit tests first.




What’s a unit test?

We write a unit test, then we write some code to make it pass.
Then we refactor, and do it all over again.

Level 4 – we do Acceptance Test-Driven development


We automate our acceptance criteria into acceptance tests
first, and then use failing AT’s until our AT’s pass.
TTD = RGR + CT

Refactor

Write a little
test

Cycle loop
< 10min
Get the test
pass

Red-Green-Refactor

Stub the
code.
Watch if
fails

Continuous (integration) Testing

Test Driven Development
Test Driven Development Rules


Adding New Functionality “Test First” ( Red) (Green)
Write an “empty” implementation
 Write a failing test: this is your requirements
 Make the test pass
 Requirements are completed




Fixing a Bug (Red)
Write a failing test that demonstrates the bug
 Fix the bug
 The test will make sure the bug does not happen again




Changing Implementation (Refactor)
Make your changes
 Make sure all tests pass
 All requirements/change request are satisfied

TTD – words of wisdom


Arrange
Setup. Set up all the data and conditions that the test
needs to execute. Create anything that is needed for
the test to operate independently of its environment.
 Mock out any dependent systems
 Control all variables not under test




Act




Execution – execute the mainline code

Assert


Validate that the code operated as intended
Visual Studio 2010
TDD Techniques


Common Unit Test Patterns
– provide input and check result for
acceptance or failure.
 Code-path – provide values that force the code
through a specific path.
 Parameter-range – pass a set of parameters that
cause success, and a set that cause failure.
 Data-driven – pass in sets of data and their
expected outcomes
 Dependency Injection
 Simple-test
More TDD Techniques


Mock Objects
 Mocks

are used to remove dependencies on
external systems or components.
 Example: Mock Database
 The mock will just be programmed to return
certain packages of data according to which test
is being run.
Unit test in .net











NUnit
MSTest
TestDriven
Moq
RhinoMocks
Typemock
Microsoft Fakes
…
nUnit
Important Attributes

1. [SetUp]
SetUp is generally used for any initialization purpose.
Any code that must be initialized or set prior to executing a test,
are put in functions marked with this attribute.
As a consequence, it avoids the problem of code repetition in each test.
2. [Ignore]
3. [ExpectedException]
4. [TearDown]
MSTest
the [TestMethod()] attribute. Without the
TestClass attribute, the test methods are
ignored
nUnit vs MSTest
Moq
Architecture in Visual Studio 2012
Developer Focused Unit Test Experience
New features - better testing



Derived from Microsoft Research “Moles” project
Fakes come in two flavors
– concrete implementations of interfaces or
abstract classes that you can pass in to your systemunder-test to isolate it from real implementations
 Shims – generated classes that enable you to intercept
and replace calls to existing classes, even those from
the .NET BCL!
 Stubs
References


http://msdn.microsoft.com/enus/library/dd264975.aspx



http://msdn.microsoft.com/en-us/library/ms182409



http://blog.stevensanderson.com/2009/08/24/writi
ng-great-unit-tests-best-and-worst-practises/



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

http://weblogs.asp.net/adilakhter/archive/2008/05/04/
more-on-unit-testing-testcontext.aspx



http://www.c-sharpcorner.com/uploadfile/dommym/atest-driven-development-tutorial-in-C-Sharp-4-0/



http://msdn.microsoft.com/enus/vstudio/ff718185.aspx



http://www.simple-talk.com/sql/t-sqlprogramming/close-these-loopholes---reproducedatabase-errors/
Eclipse


http://www.happyprog.com/tdgotchi/

More Related Content

PDF
Unit testing, principles
PPTX
Unit Testing Concepts and Best Practices
PDF
Clean Unit Test Patterns
PPTX
Unit tests & TDD
PDF
How and what to unit test
PPTX
An Introduction to Unit Testing
PPTX
Unit Tests And Automated Testing
PDF
Workshop unit test
Unit testing, principles
Unit Testing Concepts and Best Practices
Clean Unit Test Patterns
Unit tests & TDD
How and what to unit test
An Introduction to Unit Testing
Unit Tests And Automated Testing
Workshop unit test

What's hot (20)

PPTX
Unit Testing And Mocking
PPT
N Unit Presentation
PDF
Unit Testing Fundamentals
PPTX
Understanding Unit Testing
PPTX
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
PDF
Unit Testing
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Benefit From Unit Testing In The Real World
PPTX
Unit Testing
PPTX
Unit testing
PDF
An Introduction to Unit Test Using NUnit
PPT
Unit Testing
ODP
Beginners - Get Started With Unit Testing in .NET
PPT
Xp Day 080506 Unit Tests And Mocks
ODP
Testing In Java
PPT
Unit testing
PPTX
Unit testing with NUnit
ODP
Embrace Unit Testing
PPT
Testing and Mocking Object - The Art of Mocking.
PPTX
Unit test
Unit Testing And Mocking
N Unit Presentation
Unit Testing Fundamentals
Understanding Unit Testing
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit Testing
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Benefit From Unit Testing In The Real World
Unit Testing
Unit testing
An Introduction to Unit Test Using NUnit
Unit Testing
Beginners - Get Started With Unit Testing in .NET
Xp Day 080506 Unit Tests And Mocks
Testing In Java
Unit testing
Unit testing with NUnit
Embrace Unit Testing
Testing and Mocking Object - The Art of Mocking.
Unit test

Similar to Test driven development in .Net - 2010 + Eclipse (20)

PDF
An Introduction to Test Driven Development
PDF
Introduction to Unit Tests and TDD
PPTX
Unit tests and TDD
PPTX
VT.NET 20160411: An Intro to Test Driven Development (TDD)
PPTX
TDD with Visual Studio 2010
PPTX
Test Driven Development
PDF
TDD Workshop UTN 2012
PPTX
Test driven development(tdd)
PPTX
Skillwise Unit Testing
PPTX
TDD talk
PPTX
Test Driven Development
PPTX
Tdd is not about testing (OOP)
PPT
Automated Unit Testing and TDD
PPTX
Test-Driven Development
PPTX
Test Driven Development
PPTX
Test Driven Development
PPT
TDD (Test Driven Design)
PPTX
Test Driven Development:Unit Testing, Dependency Injection, Mocking
PPTX
Test-Driven Development In Action
PDF
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
An Introduction to Test Driven Development
Introduction to Unit Tests and TDD
Unit tests and TDD
VT.NET 20160411: An Intro to Test Driven Development (TDD)
TDD with Visual Studio 2010
Test Driven Development
TDD Workshop UTN 2012
Test driven development(tdd)
Skillwise Unit Testing
TDD talk
Test Driven Development
Tdd is not about testing (OOP)
Automated Unit Testing and TDD
Test-Driven Development
Test Driven Development
Test Driven Development
TDD (Test Driven Design)
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test-Driven Development In Action
Hey You Got Your TDD in my SQL DB by Jeff McKenzie

More from UTC Fire & Security (20)

PPTX
Jaming the IoT device- nie dziala mi alarm IoT
PDF
The past and present iot technologies andrzej piotrowski
PPTX
Big Data - Targi Kreatywne - szczecin
PDF
Informatyk w Pracy - Informatyka na Rynku Pracy
PPTX
"Startups : Shortcut Everything"
PDF
TEDxWSB - 2012 - Sky is the limit
PPTX
So, what will the future UX of TV look like?
PPTX
How to achieve the ux goals
PPTX
Preznetacja ochrona innowacji - cz ii
PPTX
Preznetacja ochrona innowacji - cz i
PPTX
Przyszłość Rolnictwa w Polsce
PPTX
Skalowalność systemów informatycznych dedykowanych rolnictwu z perspektywy st...
PPTX
Workshop - Szkolenie Xamarin Android
PDF
Speaker Idol - Wpf wtf - Mts 2008
PDF
ImagineCup 2008 - LookAround - II Round Finalist
PDF
Speaker Idol - ccwwa2009 - Winner Solution :)
PDF
Manual - BadBug Studio - Xbox Game
PDF
Game design document - BadBug Studio - Xbox Game
PDF
Technical document - BadBug Studio - Xbox Game
PDF
Imagine Cup 2010 - Project Rolnictwo - Wygrana na Warszawskie Dni Informatyki :)
Jaming the IoT device- nie dziala mi alarm IoT
The past and present iot technologies andrzej piotrowski
Big Data - Targi Kreatywne - szczecin
Informatyk w Pracy - Informatyka na Rynku Pracy
"Startups : Shortcut Everything"
TEDxWSB - 2012 - Sky is the limit
So, what will the future UX of TV look like?
How to achieve the ux goals
Preznetacja ochrona innowacji - cz ii
Preznetacja ochrona innowacji - cz i
Przyszłość Rolnictwa w Polsce
Skalowalność systemów informatycznych dedykowanych rolnictwu z perspektywy st...
Workshop - Szkolenie Xamarin Android
Speaker Idol - Wpf wtf - Mts 2008
ImagineCup 2008 - LookAround - II Round Finalist
Speaker Idol - ccwwa2009 - Winner Solution :)
Manual - BadBug Studio - Xbox Game
Game design document - BadBug Studio - Xbox Game
Technical document - BadBug Studio - Xbox Game
Imagine Cup 2010 - Project Rolnictwo - Wygrana na Warszawskie Dni Informatyki :)

Recently uploaded (20)

PDF
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
PDF
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
PPTX
Ppt obs emergecy.pptxydirnbduejguxjjdjidjdbuc
PDF
horaris de grups del curs 2025-2026 de l'institut
PDF
The 10 Most Inspiring Education Leaders to Follow in 2025.pdf
PDF
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PDF
Unleashing the Potential of the Cultural and creative industries
PDF
Physical pharmaceutics two in b pharmacy
PDF
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
PDF
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PDF
Developing speaking skill_learning_mater.pdf
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PPTX
Juvenile delinquency-Crim Research day 3x
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PPTX
Chapter-4-Rizal-Higher-Education-1-2_081545.pptx
DOCX
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
PPTX
CHF refers to the condition wherein heart unable to pump a sufficient amount ...
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
Ppt obs emergecy.pptxydirnbduejguxjjdjidjdbuc
horaris de grups del curs 2025-2026 de l'institut
The 10 Most Inspiring Education Leaders to Follow in 2025.pdf
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
MMW-CHAPTER-1-final.pptx major Elementary Education
Unleashing the Potential of the Cultural and creative industries
Physical pharmaceutics two in b pharmacy
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
Developing speaking skill_learning_mater.pdf
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
Juvenile delinquency-Crim Research day 3x
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
Chapter-4-Rizal-Higher-Education-1-2_081545.pptx
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
CHF refers to the condition wherein heart unable to pump a sufficient amount ...

Test driven development in .Net - 2010 + Eclipse

Editor's Notes

  • #21: This is the “New Project” dialog that is accessed from the IDE “File” menu. Here is where you add the Test Project to your application’s solution. To add the Test Project to your solution, you must select the “Test Project” template from the template list in the center of the dialog. Then you must give the project a name. The name of the application being tested with “Test” appended to it is a good naming convention to follow for your unit test projects. Choose a location at the same directory level or below your application directory. Be sure you choose “Add to Solution” on the solution line from the dropdown list.
  • #24: This is an example unit test project for an executable that is part of an ETL process. It contains test class files for each of the test classes in the application. It contains a folder, TestFiles, which contains data files for each of the test classes. Finally, it contains a utility class with common methods used by all of the unit test classes.You may or may not choose to follow this structure. It is not required or necessarily suited to your environment.
  • #25: Add the Microsoft.VisualStudio.TestTools.UnitTesting namespace with a using directive at the top of your unit test files.
  • #26: A unit test class must be marked with the [TestClass] attribute in order for tests within the class to be executed by the testing framework.
  • #27: Each unit test class must have a TestContext property that can be accessed by the test framework.
  • #28: Example of UnitTest class TestContext declaration section.
  • #29: Initialize methods are typically used to setup the test environment to a known state. ClassInitialize does this once before any of the test methods are run in the UnitTest class.
  • #30: ClassInitialize method. This example shows file directories used by tests being cleared, test files being copied, a test database being created and a test table being created. These are the types of tasks done in initialize methods. They bring the test environment into a known state so that the test results can be relied upon to be accurate.
  • #31: Cleanup methods are the counterparts to initialize methods. They handle any of the tasks that would need to be performed subsequent to test methods executing. The ClassCleanUp method executes after all of the test methods in the unit test class have run.
  • #32: ClassCleanUp method. This example shows test database being dropped after all tests in class have been executed.
  • #33: TestInitialize is run prior to each test method in the unit test class executing. This differs from the ClassInitialize in that it performs tasks that must be done before each of the tests is run rather than once before all of the tests are run.
  • #34: TestCleanUp is the counterpart to TestInitialize. This method will execute after each of the test methods in a unit test class.
  • #38: Example of tests that test public properties. Test names are the Property Name with the word Test appended to them.
  • #39: Examples of tests that test a specific requirement. The name of each test clearly indicates what the requirement is. Each method name is appended with the word Test.
  • #41: For more information about theMicrosoft.VisualStudio.TestTools.UnitTesting namespace see MSDN documentation found at:http://msdn.microsoft.com/en-us/library/ms244252http://msdn.microsoft.com/en-us/library/ms244252
  • #45: The add new test dialog. Choose the Unit Test item to add a standard unit test to your project.
  • #46: This is what a default unit test class skeleton looks like before you modify it with your code. It contains a class definition with the [TestClass] attribute. It also contains a definition for the TestContext class variable, commented initialize/cleanup methods and a sample test method skeleton called TestMethod1.
  • #49: The “Add New Test” dialog with Unit Test Wizard selected.
  • #51: Unit Test Wizard page that allows you to select what tests you wish to have generated.
  • #52: Adding tests when you create a new property or method in your code is easy. Place the cursor within the body of the property/method. Right-click the mouse to display the context menu. Choose the “Create Unit Tests” option. Accept the defaults Visual Studio provides. Click OK. The new skeleton method will be added to the class that corresponds to your application class.
  • #54: Test List Editor with tests grouped by Class Name for easiest viewing/selecting of tests to run. Green arrow on upper left of window is for running or debugging tests. Choose by clicking the little black down arrow to select whether you run or debug the tests that are checked in the test pane of the editor.You may create custom groups of tests to run. By doing this, you can avoid having to check/uncheck specific tests in left pane before executing them.
  • #56: Test Results window shows summary of test run. Test runs can be selected from dropdown list at top left of screen. Double-clicking on individual row will show detail window for individual test result.
  • #58: This window shows details of an individual test result. It is most useful when a test fails as it gives detailed error information.
  • #59: Test Results window will load most recent run by default.Select run name from dropdown at top of test results window to view other than most recent test run.
  • #64: To learn more about the Visual Studio 2010 testing framework, general unit testing best practices, unit testing concepts and mastering features of the unit testing framework, see the links provided in the following three reference pages.