SlideShare a Scribd company logo
Testing . NET systems using F#
HOW LONG WILL IT TAKE YOU?
@RBARTELINK
DUBLIN ALT.NET
24 OCTOBER, 2013
The test pyramid
 It’s not easy to achieve a good balance in a set of tests
 Too much AT -> slow + doesn’t drive implementation properly
 Too much UT -> Not meeting Living Documentation aim
 One size won’t fit all

http://martinfowler.com/bliki/TestPyramid.html (originated by Mike Cohn)
ATDD workflow

http://www.infoq.com/articles/atdd-from-the-trenches
Why?
 You need to express high and low level tests
 MbUnit, Sub/N/M/Spec are single paradigm
 3 year old tests are uglier than 3yo production code
 Test code is just different to production code
 Learn a language a year
 F# has been in VS since 2009
 One of F#’s many paradigms is to be a better C#
What can I use
 Everything (yes, everything) you know
 NUnit, MSpec, SpecFlow
 FluentAssertions,
 RhinoMocks, FakeItEasy, NSubstitute
 Things you’re already using
 xUnit.net, LINQ, Moq
 Modern Libs
 AutoFixture, AutoFixture.xUnit, AutoFixture.AutoMoq
 Foq, AutoFixture.AutoFoq
 TickSpec
Gherkin
 Feature files (Gherkin)
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario: Add two numbers
When I add 2 and 2
Then I should see 4
TickSpec
 TickFacts
[<TickFact>] let AdditionFeature () =
EmbeddedResourceFeatures.ScenariosFrom “Addition"

 Step Definitions (TickSpec with unquote)
module CalculatorSteps =
[<When>] ``I add (.*) and (.*)`` x y =
result <- Calculator()
|> enter x |> plus |> enter y
|> compute
[<Then>] ``I should see (.*)`` expected =
test <@ expected = result @>
Gherkin Backgrounds and Outlines
Feature: Addition
Background:
Given I switch my calculator to fancy mode
Scenario Outline: The Hard Stuff
When I add <First> and <Second>
Then I should see <Total>
Examples:
|First|Second|Total|
|1
|1
|2
|
|9
|-1
|8
|
Unit Testing
 Tick methods (xUnit/NUnit with unquote)
module CalculatorFacts =
[<Fact>] let ``Adding two small ints works`` () =
let result = Calculator() |> enter 2 |> plus |> enter 2 |> compute

test <@ 2+2 = result @>

 Mocking DSLs (Foq)
Mock<IList<char>>.With(fun x ->
<@ x.Count --> 2
x.Item(0) --> '0'
x.Item(1) --> '1'
x.Contains( any() ) --> true
x.RemoveAt( 2) ==> System.ArgumentOutOfRangeException()
@>)
Low Level Stuff
Type inference any()
(for real)

It.IsAny<DateTime>()

mock()

Mock.Of<T>

Tuples

let tuple = key,value
let _,value = tuple

var tuple = Tuple.Create( key, value)
var value = tuple.Item2

Lambdas

let gen a b = CreateSut a b |> Fill
let gen = CreateSut >> Fill

var gen = (int a, int b) =>
CreateSut( a, b).Fill()

Object
Expressions

let time = { new ITime with
member mock.GetHour() = 15}

class FakeTime15 {int GetHour() { return
15;}}
var time = new FakeTime15()

Custom ops

expected =? Actual // From unquote

Asset.Equal( expected, actual)

Quotations

test <@ expected =
result |> String.Concat “,” @>

Assert.That( expected, Is.EqualTo(
String.Concat( “,”, result)));

let list = Mock<IList>
.Method(fun x -> <@ x.Contains @>)
.Returns( true)

var mock = new Mock<IList>();
mock.Setup( x=> x
.Contains( It.IsAny<object>())
.Returns( true)
The bad points
 Need to install FSC on CI server (2 clicks or 11 MB






MSI or put FSC in tools dir Just Works)
You can’t mix F# and C# in a single project
F12 from F# to/from C# doesn’t work (but built in
Ctrl-, and F12 within files is fine)
No refactoring support of consequence (but not as
important as you think)
C# starts to grate a little more every day (as does “in
F# you can just…”)
Books
 Growing Object Oriented Software, Guided by Tests





(Freeman, Pryce)
Specification By Example (Adzic)
The RSpec book (Chelimsky et al)
Expert F# 3 (Syme)
Real World Functional Programming in F# and C#
(Petricek, Skeet)
Resources
 F#
 @ptrelford http://trelford.com/blog and samples
 SO F# tag
 Google
 @tomaspetricek http://tomasp.net/blog/
 Don’t forget MSDN
 ATDD
 GOOS
 @ploeh Outside In Test Driven Development
http://pluralsight.com/training/Courses/TableOfContents/ou
tside-in-tdd

More Related Content

PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PDF
Golang dot-testing-lite
PDF
Chapter 1 Basic Programming (Python Programming Lecture)
PPTX
Unit2 input output
PDF
Get Functional on the CLR: Intro to Functional Programming with F#
PDF
Python-02| Input, Output & Import
PPTX
Quiz On Strings
PPTX
Beyond lists - Copenhagen 2015
Python Programming Essentials - M16 - Control Flow Statements and Loops
Golang dot-testing-lite
Chapter 1 Basic Programming (Python Programming Lecture)
Unit2 input output
Get Functional on the CLR: Intro to Functional Programming with F#
Python-02| Input, Output & Import
Quiz On Strings
Beyond lists - Copenhagen 2015

What's hot (20)

PDF
Stacks
PPTX
Operators and Control Statements in Python
PPTX
C Programming Homework Help
PPTX
Loops in Python
PPTX
Python programming
ODP
Python quickstart for programmers: Python Kung Fu
PPTX
Iterarators and generators in python
PPTX
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
PPTX
Introduction to Python Programming
PPTX
13 Strings and Text Processing
PPTX
Python programming workshop session 1
PPTX
GE8151 Problem Solving and Python Programming
PDF
Python Loop
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
ODP
F# 101
PPTX
Advance python programming
PDF
Python programming Workshop SITTTR - Kalamassery
PPTX
C Assignment Help
PDF
Python for data science by www.dmdiploma.com
PPTX
10. Recursion
Stacks
Operators and Control Statements in Python
C Programming Homework Help
Loops in Python
Python programming
Python quickstart for programmers: Python Kung Fu
Iterarators and generators in python
F# Eye 4 the C# Guy - DDD Cambridge Nights 2014
Introduction to Python Programming
13 Strings and Text Processing
Python programming workshop session 1
GE8151 Problem Solving and Python Programming
Python Loop
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
F# 101
Advance python programming
Python programming Workshop SITTTR - Kalamassery
C Assignment Help
Python for data science by www.dmdiploma.com
10. Recursion
Ad

Viewers also liked (20)

PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PPTX
5 Best Practices For F# Development
PDF
Railway Oriented Programming
PDF
Tata Communications Network Overview
PDF
Trinity Kings World Leadership is the foundation for all Leadership, anything...
PDF
Linked Open Data - A Means for Public Sector Information Management
PPSX
The Saints by Joe McFadden
PPTX
Quick Look Video 01 29 11 Ws
PDF
Trinity Kings World Leadership: Family Franchising Systems: City of Dallas, TX
PDF
Estudo de ROI com LotusLive Para efeito de Demonstração
PPTX
130315 HCD-Netにおける資格認定制度 (英文 certification system of hcd net)
PDF
Trinity Kings World Leadership: The Law of the Picture: *Leaders Are Examples...
PDF
True definition of biblical leadership
PPSX
Hope (Victoria Kirdiy)
PPTX
121216 uxにもの申す
PPSX
Little Wings (Hummingbird Hawk Moth)
PDF
The King of the United States Kingdom
PDF
King Teola "Tee" Patillo II of the Patillo Family Kingdom goes to court to fi...
PPT
Gherla Romania 2005 - Spring day in Europe project
PDF
Trinity Kings World Leadership: Family Franchising Systems: Human Capital Man...
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
5 Best Practices For F# Development
Railway Oriented Programming
Tata Communications Network Overview
Trinity Kings World Leadership is the foundation for all Leadership, anything...
Linked Open Data - A Means for Public Sector Information Management
The Saints by Joe McFadden
Quick Look Video 01 29 11 Ws
Trinity Kings World Leadership: Family Franchising Systems: City of Dallas, TX
Estudo de ROI com LotusLive Para efeito de Demonstração
130315 HCD-Netにおける資格認定制度 (英文 certification system of hcd net)
Trinity Kings World Leadership: The Law of the Picture: *Leaders Are Examples...
True definition of biblical leadership
Hope (Victoria Kirdiy)
121216 uxにもの申す
Little Wings (Hummingbird Hawk Moth)
The King of the United States Kingdom
King Teola "Tee" Patillo II of the Patillo Family Kingdom goes to court to fi...
Gherla Romania 2005 - Spring day in Europe project
Trinity Kings World Leadership: Family Franchising Systems: Human Capital Man...
Ad

Similar to Dublin ALT.NET session Thu Oct 24 2013 (20)

PPTX
Unit Testing with Foq
PPTX
FSharp in the enterprise
PPTX
F# Eye for the C# guy - Øredev 2013
PPTX
Building unit tests correctly with visual studio 2013
PPTX
Building unit tests correctly
PDF
Testing: ¿what, how, why?
PPTX
Unit tests = maintenance hell ?
PPT
Using xUnit as a Swiss-Aarmy Testing Toolkit
PPTX
F# for functional enthusiasts
PPTX
RIA 06 & 07 - Unit Testing in Detail
PPTX
Principles and patterns for test driven development
PDF
Functions for nothing, and your tests for free
PDF
Writing testable code
PPTX
Rc2010 tdd
PPTX
L2624 labriola
PPTX
F# Eye For The C# Guy - f(by) Minsk 2014
PPTX
Reasonable Code With Fsharp
PPTX
F# Eye 4 the C# Guy
PDF
Testing, Learning and Professionalism — 20171214
PPTX
The secret unit testing tools no one ever told you about
Unit Testing with Foq
FSharp in the enterprise
F# Eye for the C# guy - Øredev 2013
Building unit tests correctly with visual studio 2013
Building unit tests correctly
Testing: ¿what, how, why?
Unit tests = maintenance hell ?
Using xUnit as a Swiss-Aarmy Testing Toolkit
F# for functional enthusiasts
RIA 06 & 07 - Unit Testing in Detail
Principles and patterns for test driven development
Functions for nothing, and your tests for free
Writing testable code
Rc2010 tdd
L2624 labriola
F# Eye For The C# Guy - f(by) Minsk 2014
Reasonable Code With Fsharp
F# Eye 4 the C# Guy
Testing, Learning and Professionalism — 20171214
The secret unit testing tools no one ever told you about

Recently uploaded (20)

PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
project resource management chapter-09.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
A Presentation on Touch Screen Technology
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
A Presentation on Artificial Intelligence
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
August Patch Tuesday
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Hindi spoken digit analysis for native and non-native speakers
project resource management chapter-09.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A Presentation on Touch Screen Technology
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
cloud_computing_Infrastucture_as_cloud_p
Enhancing emotion recognition model for a student engagement use case through...
A Presentation on Artificial Intelligence
A novel scalable deep ensemble learning framework for big data classification...
Digital-Transformation-Roadmap-for-Companies.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Web App vs Mobile App What Should You Build First.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Programs and apps: productivity, graphics, security and other tools
MIND Revenue Release Quarter 2 2025 Press Release
August Patch Tuesday
WOOl fibre morphology and structure.pdf for textiles
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Dublin ALT.NET session Thu Oct 24 2013

  • 1. Testing . NET systems using F# HOW LONG WILL IT TAKE YOU? @RBARTELINK DUBLIN ALT.NET 24 OCTOBER, 2013
  • 2. The test pyramid  It’s not easy to achieve a good balance in a set of tests  Too much AT -> slow + doesn’t drive implementation properly  Too much UT -> Not meeting Living Documentation aim  One size won’t fit all http://martinfowler.com/bliki/TestPyramid.html (originated by Mike Cohn)
  • 4. Why?  You need to express high and low level tests  MbUnit, Sub/N/M/Spec are single paradigm  3 year old tests are uglier than 3yo production code  Test code is just different to production code  Learn a language a year  F# has been in VS since 2009  One of F#’s many paradigms is to be a better C#
  • 5. What can I use  Everything (yes, everything) you know  NUnit, MSpec, SpecFlow  FluentAssertions,  RhinoMocks, FakeItEasy, NSubstitute  Things you’re already using  xUnit.net, LINQ, Moq  Modern Libs  AutoFixture, AutoFixture.xUnit, AutoFixture.AutoMoq  Foq, AutoFixture.AutoFoq  TickSpec
  • 6. Gherkin  Feature files (Gherkin) Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers When I add 2 and 2 Then I should see 4
  • 7. TickSpec  TickFacts [<TickFact>] let AdditionFeature () = EmbeddedResourceFeatures.ScenariosFrom “Addition"  Step Definitions (TickSpec with unquote) module CalculatorSteps = [<When>] ``I add (.*) and (.*)`` x y = result <- Calculator() |> enter x |> plus |> enter y |> compute [<Then>] ``I should see (.*)`` expected = test <@ expected = result @>
  • 8. Gherkin Backgrounds and Outlines Feature: Addition Background: Given I switch my calculator to fancy mode Scenario Outline: The Hard Stuff When I add <First> and <Second> Then I should see <Total> Examples: |First|Second|Total| |1 |1 |2 | |9 |-1 |8 |
  • 9. Unit Testing  Tick methods (xUnit/NUnit with unquote) module CalculatorFacts = [<Fact>] let ``Adding two small ints works`` () = let result = Calculator() |> enter 2 |> plus |> enter 2 |> compute test <@ 2+2 = result @>  Mocking DSLs (Foq) Mock<IList<char>>.With(fun x -> <@ x.Count --> 2 x.Item(0) --> '0' x.Item(1) --> '1' x.Contains( any() ) --> true x.RemoveAt( 2) ==> System.ArgumentOutOfRangeException() @>)
  • 10. Low Level Stuff Type inference any() (for real) It.IsAny<DateTime>() mock() Mock.Of<T> Tuples let tuple = key,value let _,value = tuple var tuple = Tuple.Create( key, value) var value = tuple.Item2 Lambdas let gen a b = CreateSut a b |> Fill let gen = CreateSut >> Fill var gen = (int a, int b) => CreateSut( a, b).Fill() Object Expressions let time = { new ITime with member mock.GetHour() = 15} class FakeTime15 {int GetHour() { return 15;}} var time = new FakeTime15() Custom ops expected =? Actual // From unquote Asset.Equal( expected, actual) Quotations test <@ expected = result |> String.Concat “,” @> Assert.That( expected, Is.EqualTo( String.Concat( “,”, result))); let list = Mock<IList> .Method(fun x -> <@ x.Contains @>) .Returns( true) var mock = new Mock<IList>(); mock.Setup( x=> x .Contains( It.IsAny<object>()) .Returns( true)
  • 11. The bad points  Need to install FSC on CI server (2 clicks or 11 MB     MSI or put FSC in tools dir Just Works) You can’t mix F# and C# in a single project F12 from F# to/from C# doesn’t work (but built in Ctrl-, and F12 within files is fine) No refactoring support of consequence (but not as important as you think) C# starts to grate a little more every day (as does “in F# you can just…”)
  • 12. Books  Growing Object Oriented Software, Guided by Tests     (Freeman, Pryce) Specification By Example (Adzic) The RSpec book (Chelimsky et al) Expert F# 3 (Syme) Real World Functional Programming in F# and C# (Petricek, Skeet)
  • 13. Resources  F#  @ptrelford http://trelford.com/blog and samples  SO F# tag  Google  @tomaspetricek http://tomasp.net/blog/  Don’t forget MSDN  ATDD  GOOS  @ploeh Outside In Test Driven Development http://pluralsight.com/training/Courses/TableOfContents/ou tside-in-tdd