Getting Started With Performance Testing Webinar (RedLine13 + Testing Diaries)
Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method...
Transcript of Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method...
TDD
Getting started with Unit-testing
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
Peculiarities of units
• Independence
• Lightweight
• Certain stages:
– Set up
– Execute
– Verify
Unit vs Integration tests
Unit testing
• Tests Module
• On bill for one person
Integration testing
• Group of modules
• One bill for a group
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
Tools for unit testing
Simple Tests
Unit tests using nUnit
Setup Execute Verify TearDown
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
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
Unit tests using nUnit. Assertion.
• Equality – AreEqual
– AreNotEqual
• Condition – IsTrue
– IsFalse
– IsNull
– IsEmpty
• Comparison – Greater\ Less
– GreaterOrEqual\LessOrEqual
Unit tests using nUnit. Assertion.
• Identity
• Type
• Utility
• String
• Collection
• File etc.
http://www.nunit.org/index.php?p=assertions&r=2.4
Test Driven Development
Red
Green Refactor
TDD. Coverage
• Normal coverage : > 60%
• Tools:
– NCover
– http://www.ncover.com/
– dotCover
http://www.jetbrains.com/dotcover/
– TestMatrix
http://submain.com/products/testmatrix.aspx
Tools for unit testing
TDD. First steps. Coverage.
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
DEMO. Fake objects
Mock Objects. Tools
• Can mock public members of interfaces
• Popular Tools
– Moq http://code.google.com/p/moq/
– RhinoMocks http://ayende.com/blog
DEMO. Mock using moq
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
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
Home task
Questions
Thanks