Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method...

22
TDD

Transcript of Getting started with Unit-testing · Getting started with Unit-testing . Unit testing is •Method...

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

Home task

Questions

Thanks