getting started with unit-testing · getting started with unit-testing . unit testing is •method...
TRANSCRIPT
![Page 1: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/1.jpg)
TDD
![Page 2: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/2.jpg)
Getting started with Unit-testing
![Page 3: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/3.jpg)
Unit testing is
• Method – Does: Check functioning
– Uses: Units ( Modules)
– Is used by: Developers
– Usage: build automation
– Usage: developer assurance
• Unit = smallest testable part – Method
– Interface
– Class
![Page 4: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/4.jpg)
Peculiarities of units
• Independence
• Lightweight
• Certain stages:
– Set up
– Execute
– Verify
![Page 5: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/5.jpg)
Unit vs Integration tests
Unit testing
• Tests Module
• On bill for one person
Integration testing
• Group of modules
• One bill for a group
![Page 6: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/6.jpg)
Tools for unit testing
Nunit http://www.nunit.org/
xUnit http://xunit.codeplex.com/
MsTest http://msdn.microsoft.com/en-us/library/ms243147(v=vs.80).aspx
![Page 7: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/7.jpg)
Tools for unit testing
Simple Tests
![Page 8: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/8.jpg)
Unit tests using nUnit
Setup Execute Verify TearDown
![Page 9: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/9.jpg)
Unit tests using nUnit. Attributes.
• General – TestFixture – set of tests
– Test – a method that will be run as test during session
– Ignore – the test that should be ignored when running
• Verify
– ExpectedException – an exception of a certain type is expected
![Page 10: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/10.jpg)
Unit tests using nUnit. Attributes.
• Setting up – SetUp – settings for each test in a fixture
– TestFixtureSetUp – initialization that runs only ones for a fixture
– SetUpFixture – Setup method from such a fixture will run for each fixture from the namespace
• Rolling back
– TearDown – once for each test
– TestFixtureTearDown – once for fixture
![Page 11: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/11.jpg)
Unit tests using nUnit. Assertion.
• Equality – AreEqual
– AreNotEqual
• Condition – IsTrue
– IsFalse
– IsNull
– IsEmpty
• Comparison – Greater\ Less
– GreaterOrEqual\LessOrEqual
![Page 12: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/12.jpg)
Unit tests using nUnit. Assertion.
• Identity
• Type
• Utility
• String
• Collection
• File etc.
http://www.nunit.org/index.php?p=assertions&r=2.4
![Page 13: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/13.jpg)
Test Driven Development
Red
Green Refactor
![Page 14: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/14.jpg)
TDD. Coverage
• Normal coverage : > 60%
• Tools:
– NCover
– http://www.ncover.com/
– dotCover
http://www.jetbrains.com/dotcover/
– TestMatrix
http://submain.com/products/testmatrix.aspx
![Page 15: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/15.jpg)
Tools for unit testing
TDD. First steps. Coverage.
![Page 16: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/16.jpg)
Test Objects
• Dummy object – passed as parameter, nothing happens (null, string.empty)
• Test stub – used for getting data from external dependencies
• Test-spy – writes sequence of calls
• Mock-object
• Fake object - replacement for heavy-weight objects
![Page 17: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/17.jpg)
DEMO. Fake objects
![Page 18: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/18.jpg)
Mock Objects. Tools
• Can mock public members of interfaces
• Popular Tools
– Moq http://code.google.com/p/moq/
– RhinoMocks http://ayende.com/blog
![Page 19: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/19.jpg)
DEMO. Mock using moq
![Page 20: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/20.jpg)
Anti-patterns for unit tests
• Inspector – encapsulation rules are broken to achieve 100% coverage
• The local hero – dependent on something specific
• The Secret Catcher – doesn’t have any assertions, relies on exceptions
• Excessive Setup – multiple lines for setup, its difficult to get what is testes
• Giant – long tests
![Page 21: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/21.jpg)
Anti-patterns for unit tests
• The free ride – instead of creating a new test, assert is added to existing one
• Success against all odds – we just need to make the test green!
• The slow poke – to slow tests
• Generous Leftovers – reusage of data created by another test
![Page 22: Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method –Does: Check functioning –Uses: Units ( Modules) –Is used by: Developers –Usage:](https://reader030.vdocuments.mx/reader030/viewer/2022040113/5f35e7e131ee7462555c7d67/html5/thumbnails/22.jpg)
Home task
Questions
Thanks