O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing with F#

Book Description

Deliver high-quality, bug-free applications by testing them with efficient and expressive functional programming

In Detail

Starting with an introduction to the concepts behind test automation, you will learn how to set up your test environment for unit testing and how to execute your test suite within and outside of Visual Studio. Using different test frameworks as MSTest, NUnit, and xUnit, you will learn how to write tests in isolation, create test doubles with Foq, and write expressive assertions with FsUnit.

You will learn techniques used to mock database contexts, test stored procedures, use migrations to test databases in isolation, control a web browser with Canopy, and test web services with F# interactive. You will learn how these techniques apply to Agile principles and discover how testing fits into your project.

What You Will Learn

  • Set up a test environment in your code base
  • Write unit tests, integration tests, and functional tests for F# applications
  • Leverage tools such as FsUnit, Foq, Canopy, and TickSpec to run tests both inside and outside your development environment
  • Maximize the productivity of your test code using the language features of F#
  • Extract dependencies to databases, web services, and other IO bound operations
  • Test your applications using executable specifications on a web browser
  • Discover the best practices for developing a sustainable test suite

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Testing with F#
    1. Table of Contents
    2. Testing with F#
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. The Practice of Test Automation
      1. Testing as a practice
        1. Black or white box testing
        2. Manual testing
        3. Test automation
          1. Style check
          2. Static analysis
          3. Unit testing
          4. Integration testing
          5. System testing
      2. Building trust
      3. The purpose of testing
        1. When not to test
      4. Testing with intent
        1. Asserting written code
          1. Contracts versus tests
        2. Designing code to be written
      5. Writing tests for regression
        1. Executable specifications
      6. Summary
    9. 2. Writing Testable Code with Functional Programming
      1. Purely functional
      2. Immutability
        1. Immutable data structures
        2. Built-in immutable types
          1. Tuple
          2. List
          3. Sequence
        3. Creating an immutable type
      3. Writing testable code
        1. Active patterns
        2. Higher order functions
        3. Partial application
        4. Continuations
      4. Expressiveness through functional programming
        1. Imperative programming
        2. Declarative programming
        3. Tail call optimization
        4. Parallel execution
      5. Summary
    10. 3. Setting Up Your Test Environment
      1. F# Interactive
        1. Loading external references
      2. Testing with Visual Studio
        1. NUnit
        2. xUnit
      3. Comparing MSTest, NUnit, and xUnit
      4. Tools and frameworks
        1. FsUnit
        2. ReSharper test runner
          1. xUnit.net
      5. Executing a test suite outside Visual Studio
        1. MSTest
        2. NUnit
        3. xUnit
      6. FAKE
      7. Continuous integration
        1. Git Hooks
        2. TeamCity
      8. Summary
    11. 4. Unit Testing
      1. Structuring your tests
      2. Arrange, act, assert
        1. Separate project versus inline tests
      3. FsUnit
        1. Assertions
        2. Custom assertions
      4. Unquote
        1. Assertions
      5. Testing in isolation
        1. Vertical slice testing
      6. Finding the abstraction level
        1. Public interface
        2. Private functions
      7. Test doubles
      8. Dependency injection
        1. Functional injection
        2. Currying
        3. Higher order functions
      9. Stubbing with Foq
      10. Mocking
      11. Dealing with databases
      12. Summary
    12. 5. Integration Testing
      1. Good integration tests
        1. Layer-for-layer testing
        2. Top down testing
        3. External interface testing
      2. Your first integration test
      3. Setting up and tearing down databases
        1. Brownfield database setup
        2. Really large databases
      4. Speeding up integration testing
        1. Testing in parallel
      5. Testing stored procedures
      6. Data-driven testing
      7. Testing web services
        1. Web service type provider
      8. Summary
    13. 6. Functional Testing
      1. Specifications
      2. Setting up TickSpec
        1. Executable specifications
        2. Combinatorial examples
      3. Web-browser-based testing
        1. Selenium
        2. PhantomJS
        3. Canopy
        4. CSQuery
      4. Regression testing
      5. Summary
    14. 7. The Controversy of Test Automation
      1. Bugs or defects
        1. Bugs
          1. How to avoid them
        2. Defects
          1. How to avoid them
        3. The difference between bugs and defects
      2. The cost of quality
        1. Quality index
        2. The software craftsman
        3. Not all code is created equal
        4. Being a pragmatic programmer
        5. Good enough
        6. Technical debt
      3. The false security of code coverage
        1. Measuring the delta of code coverage
      4. Test-driven development
        1. Red, green, refactor
        2. Aversions to test-driven development
        3. Test first development
          1. Fixing bugs
          2. API design
      5. Testing or fact-checking
        1. Replacing the tester with automation
      6. Summary
    15. 8. Testing in an Agile Context
      1. Building a bridge or tending to a garden
      2. The broken iron triangle
      3. Visualizing metrics
      4. The Kanban board
      5. Predictability
        1. Testing
        2. What it means to be done
        3. Mitigating risks
          1. Known unknowns
          2. Unknown unknowns
        4. Automation
      6. Testing in agile
      7. Summary
    16. 9. Test Smells
      1. Tests that break upon refactoring
      2. Tests that break occasionally
      3. Tests that never break
      4. Tests that are too complex
      5. Tests that require excessive setup
      6. Developers not writing tests
        1. Testing is a waste
        2. Management won't let us
        3. We don't know where to start
        4. Our code is hard to test
        5. It's a thing about culture
      7. Summary
    17. 10. The Ten Commandments of Test Automation
      1. Testing behavior, not implementation
      2. Using ubiquitous language in your test name
      3. Asserting only one thing in your test
      4. Don't mock the Mockingbird
      5. Always refactor your SUT
      6. Your test should never be more than 10 lines of code
      7. Always test in isolation
      8. Controlling your dependencies
      9. Your test is a ninja
      10. The test is not complex
      11. Summary
    18. Index