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.



...
Unit Testing



A unit is the smallest testable part of software.
In object-oriented programming, the smallest unit
is a...
Unit testing myths











“Testers test, developers develop”
“There’s no time to write tests.”
“We’ll write the...
Test Driven Development (TDD)
 A software development technique that uses short

development iterations based on pre-writ...
Metric of usage TTD in Projects


Level 0 – huh?




Level 1 – we write unit tests.




We write a unit test, then so...
TTD = RGR + CT

Refactor

Write a little
test

Cycle loop
< 10min
Get the test
pass

Red-Green-Refactor

Stub the
code.
Wa...
Test Driven Development Rules


Adding New Functionality “Test First” ( Red) (Green)
Write an “empty” implementation
 Wr...
TTD – words of wisdom


Arrange
Setup. Set up all the data and conditions that the test
needs to execute. Create anything...
Visual Studio 2010
TDD Techniques


Common Unit Test Patterns
– provide input and check result for
acceptance or failure.
 Code-path – prov...
More TDD Techniques


Mock Objects
 Mocks

are used to remove dependencies on
external systems or components.
 Example:...
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 initi...
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 im...
References


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



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

...
References


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



http://www.c...
Eclipse


http://www.happyprog.com/tdgotchi/
Upcoming SlideShare
Loading in …5
×

Test driven development in .Net - 2010 + Eclipse

853 views

Published on

Test driven development in .Net

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Test driven development in .Net - 2010 + Eclipse

  1. 1. TEST DRIVEN DEVELOPMENT + UNIT TESTING Using Visual Studio 2010/2012 and Eclipse By Andrzej Piotrowski
  2. 2. 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)
  3. 3. 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
  4. 4. 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”
  5. 5. 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.
  6. 6. 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.
  7. 7. 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
  8. 8. 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 
  9. 9. 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
  10. 10. Visual Studio 2010
  11. 11. 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
  12. 12. 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.
  13. 13. Unit test in .net         NUnit MSTest TestDriven Moq RhinoMocks Typemock Microsoft Fakes …
  14. 14. 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]
  15. 15. MSTest the [TestMethod()] attribute. Without the TestClass attribute, the test methods are ignored
  16. 16. nUnit vs MSTest
  17. 17. Moq
  18. 18. Architecture in Visual Studio 2012
  19. 19. Developer Focused Unit Test Experience
  20. 20. 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
  21. 21. 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
  22. 22. 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/
  23. 23. Eclipse  http://www.happyprog.com/tdgotchi/

×