Test driven development in .Net - 2010 + Eclipse

23
TEST DRIVEN DEVELOPMENT + UNIT TESTING Using Visual Studio 2010/2012 and Eclipse By Andrzej Piotrowski

description

Test driven development in .Net

Transcript of Test driven development in .Net - 2010 + Eclipse

Page 1: Test driven development in .Net - 2010 + Eclipse

TEST DRIVEN DEVELOPMENT +UNIT TESTING

Using Visual Studio 2010/2012 and EclipseBy Andrzej Piotrowski

Page 2: Test driven development in .Net - 2010 + Eclipse

Agenda

Importance of Unit Testing Test Driven Development TDD Techniques Unit test in .net (VS2010)

1. MSTest2. nUnit3. Mock

Big Projects (VS2010 and Eclipse)

Page 3: Test driven development in .Net - 2010 + Eclipse

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.

Unit Testing

Integration Testing

System Testing

Acceptance Testing

Page 4: Test driven development in .Net - 2010 + Eclipse

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”

Page 5: Test driven development in .Net - 2010 + Eclipse

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.

Page 6: Test driven development in .Net - 2010 + Eclipse

Metric of usage TTD in Projects

Level 0 – huh? What’s a unit test?

Level 1 – we write unit tests. We wrote some tests for critical parts.

Level 2 – we write (most of) our unit tests first. We write a unit test, then some code, then some more

code. Level 3 – we do Test-Driven Development

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.

Page 7: Test driven development in .Net - 2010 + Eclipse

TTD = RGR + CT

Write a little test

Stub the code.

Watch if fails

Get the test pass

Refactor

Cycle loop < 10min

Continuous (integration) TestingRed-Green-Refactor

Test Driven Development

Page 8: Test driven development in .Net - 2010 + Eclipse

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

Page 9: Test driven development in .Net - 2010 + Eclipse

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

Page 10: Test driven development in .Net - 2010 + Eclipse

Visual Studio 2010

Page 11: Test driven development in .Net - 2010 + Eclipse

TDD Techniques

Common Unit Test Patterns Simple-test – 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

Page 12: Test driven development in .Net - 2010 + Eclipse

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.

Page 13: Test driven development in .Net - 2010 + Eclipse

Unit test in .net

NUnit MSTest TestDriven Moq RhinoMocks Typemock Microsoft Fakes …

Page 14: Test driven development in .Net - 2010 + Eclipse

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]

Page 15: Test driven development in .Net - 2010 + Eclipse

MSTest

the [TestMethod()] attribute. Without the TestClass attribute, the test methods are ignored

Page 16: Test driven development in .Net - 2010 + Eclipse

nUnit vs MSTest

Page 17: Test driven development in .Net - 2010 + Eclipse

Moq

Page 18: Test driven development in .Net - 2010 + Eclipse

Architecture in Visual Studio 2012

Visual Studio Unit Test Explorer

Command Line Runner

TeamBuild Unit Test Activity

Visual Studio Unit Test Platform

MS-Test Manage

d

MS-Test Native

NUnitxUnit.ne

tQUnit MORE!

Page 19: Test driven development in .Net - 2010 + Eclipse

Developer Focused Unit Test Experience

Red-Green Bar

Most important

tests shown first

Timings

Shows tests from any

framework

Search

Run Details

Page 20: Test driven development in .Net - 2010 + Eclipse

New features - better testing

Derived from Microsoft Research “Moles” project

Fakes come in two flavors Stubs – concrete implementations of interfaces or

abstract classes that you can pass in to your system-under-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!

Page 23: Test driven development in .Net - 2010 + Eclipse

Eclipse

http://www.happyprog.com/tdgotchi/