Sachin Kainth
Unit Testing
Contents
• General discussion on unit testing
• Unit testing tools
• Test Driven Development
• Conclude
What is Unit Testing
• Software design and development method that
gives developer confidence that individual units
of code are fit for purpose
The Importance of Unit Testing
• Forced to keep code simple and testable
• Units tend to do only one thing
• Code is more maintainable
• Can take the place of formal design
• Code is less buggy
• Code is more robust
The Importance of Unit Testing
• Provides executable documentation
• Natural set of regression tests
• Makes refactoring safer
• Less (severe) bugs founds further down the cycle
The Importance of Unit Testing
Ariane 5 Flight 501 (1996)
• Launch system developed under the authority of
the European Space Agency
• The navigation package was inherited from the
Ariane 4 without proper testing
• Veered off course and exploded soon after take
off
• Cost of failure was more than US$370 million
Why Unit Testing is not always done
• Developer
• Considered tedious and uninteresting
• “My code doesn’t need testing”
• Gray box testing done instead
• Unfamiliarity with unit testing tools and techniques
• Test development is a combinatorial problem
• More lines of test code needed than production code
• Developers test as they code
• Tests are a maintenance overhead
• Who will test the unit tests
• Not all software projects are “complex”
• Maintaining a system which has no unit tests
Why Unit Testing is not always done
• Client
• Not a deliverable
• No time, budget
• As clients do not always know what they want, tests
may need to be thrown away
Why Unit Testing is not always done
• Management
• Doesn’t add anything to the project
Unit Testing Tools
• NUnit
• Microsoft
• TestDriven.NET add-in
NUnit Attributes
• [TestFixture]
• [Test]
• [TestFixtureSetUp]
• [TestFixtureTearDown]
• [Setup]
• [TearDown]
• [ExpectedException]
Demo
A simple demo of NUnit
Demo
A simple demo of Visual Studio test projects
Demo
A simple demo of TestDriven.NET
Demo
Testing without a framework
Test Driven Development Technique
Add test(s) before implementing feature
Run all tests and see if new test(s) fail
Write code to pass new test(s)
Run all tests and see if all pass
Refactor code and rerun all tests
Test Driven Development Technique
Benefits:
• Makes developer focus on behaviour and
requirements early on
• 100% test coverage
• Extensive refactoring made possible
Demo
An example of Test Driven Development
Summary
• Unit testing is a Good Thing
• Tools
• TDD
References
• http://en.wikipedia.org/wiki/Test-
driven_development
• http://www.nunit.org/index.php
• http://en.wikipedia.org/wiki/V-
Model_(software_development)
• http://www.wired.com/software/coolapps/news/20
05/11/69355
• http://en.wikipedia.org/wiki/List_of_notable_softw
are_bugs
• http://www.coleyconsulting.co.uk/testtype.htm
• http://www.youtube.com/watch?v=REDO7eYwGTU
Questions
?

Testing

  • 1.
  • 2.
    Contents • General discussionon unit testing • Unit testing tools • Test Driven Development • Conclude
  • 3.
    What is UnitTesting • Software design and development method that gives developer confidence that individual units of code are fit for purpose
  • 4.
    The Importance ofUnit Testing • Forced to keep code simple and testable • Units tend to do only one thing • Code is more maintainable • Can take the place of formal design • Code is less buggy • Code is more robust
  • 5.
    The Importance ofUnit Testing • Provides executable documentation • Natural set of regression tests • Makes refactoring safer • Less (severe) bugs founds further down the cycle
  • 6.
    The Importance ofUnit Testing Ariane 5 Flight 501 (1996) • Launch system developed under the authority of the European Space Agency • The navigation package was inherited from the Ariane 4 without proper testing • Veered off course and exploded soon after take off • Cost of failure was more than US$370 million
  • 7.
    Why Unit Testingis not always done • Developer • Considered tedious and uninteresting • “My code doesn’t need testing” • Gray box testing done instead • Unfamiliarity with unit testing tools and techniques • Test development is a combinatorial problem • More lines of test code needed than production code • Developers test as they code • Tests are a maintenance overhead • Who will test the unit tests • Not all software projects are “complex” • Maintaining a system which has no unit tests
  • 8.
    Why Unit Testingis not always done • Client • Not a deliverable • No time, budget • As clients do not always know what they want, tests may need to be thrown away
  • 9.
    Why Unit Testingis not always done • Management • Doesn’t add anything to the project
  • 10.
    Unit Testing Tools •NUnit • Microsoft • TestDriven.NET add-in
  • 11.
    NUnit Attributes • [TestFixture] •[Test] • [TestFixtureSetUp] • [TestFixtureTearDown] • [Setup] • [TearDown] • [ExpectedException]
  • 12.
  • 13.
    Demo A simple demoof Visual Studio test projects
  • 14.
    Demo A simple demoof TestDriven.NET
  • 15.
  • 16.
    Test Driven DevelopmentTechnique Add test(s) before implementing feature Run all tests and see if new test(s) fail Write code to pass new test(s) Run all tests and see if all pass Refactor code and rerun all tests
  • 17.
    Test Driven DevelopmentTechnique Benefits: • Makes developer focus on behaviour and requirements early on • 100% test coverage • Extensive refactoring made possible
  • 18.
    Demo An example ofTest Driven Development
  • 19.
    Summary • Unit testingis a Good Thing • Tools • TDD
  • 20.
    References • http://en.wikipedia.org/wiki/Test- driven_development • http://www.nunit.org/index.php •http://en.wikipedia.org/wiki/V- Model_(software_development) • http://www.wired.com/software/coolapps/news/20 05/11/69355 • http://en.wikipedia.org/wiki/List_of_notable_softw are_bugs • http://www.coleyconsulting.co.uk/testtype.htm • http://www.youtube.com/watch?v=REDO7eYwGTU
  • 21.

Editor's Notes

  • #4 Business Case: What benefits new system or change will deliver to the business and outlines costs Requirements: Define what the system will achieve to meet the need identified. Functional and Non-Functional. Functional: What the system should do. E.g. “should print invoices” Non-Functional: How a system is supposed to be. E.g. “should print them quickly”. They are the qualities of the system. Examples are accessibility, response time, usability System Specification: How the system shall achieve requirements by defining them in computer terms System Design: Detailed design document mapping features required onto components, defining relationships between these components Component Design: Tells you how each component performs its processing Shouldn’t really think of this as a linear process like Waterfall but can be iterative. Interface testing: communication of components.
  • #6 Executable documentation: unit tests tell you inappropriate/appropriate use of the unit as well as negative behaviours that are trapped by the unit Refactoring: changes to code that leave the original functionality unchanged but do change the design of the code sp that it is cleaner, more efficient and easier to test
  • #7  Ariane 5 flew faster, resulting in larger values of some variables in the navigation software.   Shortly after launch, an attempt to convert a 64-bit floating-point number into a 16-bit integer generated an overflow.  The error was caught, but the code that caught it elected to shut down the subsystem 
  • #8 Gray Box Testing - Test designed based on the knowledge of algorithm, internal states, architectures, or other high -level descriptions of the program behaviour.
  • #9 Combinatorial problem: if statement requires two tests at least More lines of test code: 3-5 lines for 1 line
  • #11 Open source unit testing framework for .NET - free .Net version of the XUnit family of tools
  • #12 Attributes can be used to document classes, specify runtime information, and even dictate runtime behaviour. NUnit Attributes [TestFixture] - Marks a class as containing tests and optionally setup or teardown methods. The class must be public (so that NUnit can call the methods in it) and not abstract. [Test] - Marks a method as a test method. The method must not have any parameters. [Setup] - Used to identify a method that contains code that gets executed before each test method is called. [TearDown] - Used to identify a method that contains code that gets executed after each test method is called. [TestFixtureSetUp] - Used to identify a method that contains code that gets executed before any of the test methods is called. [TestFixtureTearDown] - Used to identify a method that contains code that gets executed after all test methods have been called. [ExpectedException] - Specifies that the execution of a test is expected to throw an exception.
  • #16 Lucene DateTime only supports all AD ranges SQL dates 1753 or 1 AD Dotlucene only supports 1970 onwards
  • #17 The reason you write code to just pass the test as quick as possible is that you want to verify quickly that the new code does what you want it to And you will only write code that passes the test and not ‘allow for’ additional untested code Software development technique that uses short development iterations based on pre-written test cases that define desired improvements or new features Not just a testing methodology but a software design technique Key concept: write tests first Developers should make 1 to 10 edits per cycle to the code.
  • #18 Writing tests first enable developer to focus on just what is required Refactoring possible: because you have tests to help you check whether the changes are good or not Loosely coupled: components depend on each other to the least possible extent. Developer thinks about code as small unit that can be written and tested independently. This leads to smaller and more focused classes.
  • #19 1 2 3 4 to 20 21 to 29 30 31 to 39 40 to 99 100 101 to 199 200 201 to 299 300 to 999 1000 1001 to 9999 10,000 to 999,999 1,000,000 to 999,999,999 1,000,000,000 to 2,147,483,647 0 -1 -2,147,483,648