Scala Test-Driven Development

Author: Gaurav Sood  

Publisher: Packt Publishing‎

Publication year: 2016

E-ISBN: 9781786463029

P-ISBN(Paperback): 9781786464675

Subject: TP312 程序语言、算法语言;TP316.8 WebOS

Keyword: 网络操作系统,自动化技术、计算机技术,程序语言、算法语言

Language: ENG

Access to resources Favorite

Disclaimer: Any content in publications that violate the sovereignty, the constitution or regulations of the PRC is not accepted or approved by CNPIEC.

Description

Build robust Scala applications by implementing the fundamentals of test-driven development in your workflow About This Book • Get a deep understanding of various testing concepts such as test-driven development (TDD) and BDD • Efficient usage of the built-in Scala features such as ScalaTest, specs2, and Scala check • Change your approach towards problem solving by thinking about the boundaries of the problem and its definition rather than focusing on the solution Who This Book Is For This book is for Scala developers who are looking to write better quality and easily maintainable code. No previous knowledge of TDD/BDD is required. What You Will Learn • Understand the basics of TDD and its significance • Refactoring tests to build APIs in order to increase test coverage • How to leverage the inbuilt Scala testing modules like ScalaTest, specs2 and Scala Check • Writing test fixtures and apply the concepts of BDD • How to divide tests to run at different points in continuous delivery cycle • Benefits of refactoring and how it affects the final quality of code produced • Understanding of SBT based build environment and how to use it to run tests • The fundamentals of mocking and stubbing in Scala and how to use it efficiently In Detail Test-driven development (TDD) produces high-quality applications in less time than is possible with traditional methods. Due to the systematic nature of TDD, the application is tested in individual units as well as cumulatively, right from the design stage, to ensure optimum performance and reduced debugging costs. This step-by-step guide shows you how to use the principles of TDD and built-in Scala testing modules to write clean and fully tested Scala code and give your workflow the change it needs to let you create better applications than ever before. After an introduction to TDD, you will learn the basics of ScalaTest, one of the most flexible and most popular testing tools around for Scala, by building your first fully test-driven application. Building on from that you will learn about the ScalaTest API and how to refactor code to produce high-quality applications. We’ll teach you the concepts of BDD (Behavior-driven development) and you’ll see how to add functional tests to the existing suite of tests. You’ll be introduced to the concepts of Mocks and Stubs and will learn to increase test coverage using properties. With a concluding chapter on miscellaneous tools, this book will enable you to write better quality code that is easily maintainable and watch your apps change for the better. Style and approach This step-by-step guide explains the significance of TDD in Scala through various practical examples. You will learn to write a complete test-driven application throughout the course of the book.

Chapter

Chapter 1: Hello, TDD!

What is TDD?

Why TDD?

Changing our approach to problem solving

Iteratively writing failing tests

Baby steps

Brief introduction to Scala and SBT

What is Scala?

Why Scala?

Scala Build Tool

Why SBT?

Resources for learning Scala

Resources for SBT

Setting up a build environment

Steps for downloading and installing Scala

Steps for downloading and installing SBT

Creating a project directory structure

Build definition files

Hello World!

Creating a directory structure

Creating a build definition

Test first!

Summary

Chapter 2: First Test-Driven Application

Testing frameworks

ScalaTest

A quick tutorial

Adding ScalaTest to the project

Choose your testing style

FunSuite

FlatSpec

FunSpec

WordSpec

FreeSpec

Spec

PropSpec

FeatureSpec

Resources for ScalaTest

Problem statements

IDE

Project structure

Write a failing test – RED

Writing application code to fix the test – GREEN

More tests – REPEAT

Intercepting exceptions

Summary

Chapter 3: Clean Code Using ScalaTest

Assertions

Deliberately failing tests

Assumptions

Canceling tests

Failure messages and clues

Matchers

Matchers for equality

Matchers for instance and identity checks of objects

Matchers for size and length

Matching strings

Matching greater and less than

Matching Boolean properties

Matching number within ranges

Matching emptiness

Writing your own BeMatchers

Some more Matchers

Matchers for containers

Combining Matchers with logical expressions

Matching options

Matching properties

Checking that a snippet of code does not compile

Base test classes

Test fixtures

Calling get-fixture methods

Instantiating fixture-context objects

Overriding withFixture(NoArgTest)

Calling loan-fixture methods

Overriding withFixture(OneArgTest)

Mixing in BeforeAndAfter

Composing fixtures by stacking traits

Problem statement

Feature – decimal to hexadecimal conversion

BinaryToDecimalSpec.scala

HexadecimalToDecimalSpec.scala

DecimalHexadecimalSpec.scala

BeanSpec.scala

package.scala.packt

BaseConversion.scala

Summary

Chapter 4: Refactor Mercilessly

Clean code

Red-Green-Refactor

Code smell

Expendable

Couplers

Modification thwarters

Bloaters

Object-oriented abusers

Obsolete libraries

To refactor or not to refactor

Doing it thrice (rule of three)

Adding new feature

Bug fixing

Code reviews

Refactoring techniques

Composing methods

Moving features between objects

Organizing data

Summary

Chapter 5: Another Level of Testing

Integration testing

Functional testing

Acceptance testing

Need for user acceptance testing

Behavior-driven development

Introduction

Three amigos

Bird's-eye view of BDD

Gherkin

Executable specification

Don't repeat yourself

Talk is cheap

Adding Cucumber dependency

Directory structure

The feature file

Running the feature

Step definition

Summary

Chapter 6: Mock Objects and Stubs

History

Coupling

Stubs

Mock objects

Expectations

Verifications

Fakes

Spy

Mocking frameworks

JMock

Advantages of JMock

EasyMock

Mockito

ScalaMock

Advantages of ScalaMock

mockFunction

Proxy mocks

Generated mocks

Let's dig a little deeper

Specifying expectations

Summary

Chapter 7: Property-Based Testing

Introduction to property-based testing

Table-driven properties

Generator-driven properties

ScalaCheck

Generators

Generating case classes

Conditional Generators

Generating containers

Arbitrary Generator

Generation statistics

Executing property checks

Our own Generator-driven property checks

Summary

Chapter 8: Scala TDD with Specs2

Introduction to Specs2

Differences between Specs2 and ScalaTest

Setting up Specs2

Styles

Unit specifications

Acceptance specification

Matchers

Simple Matchers

Matchers for strings

Matchers for relational operators

Matchers for floating point

Matchers for references

Matchers for Option/Either

Matchers for the try monad

Matching exception

Iterable Matchers

Matchers for sequences and traversables

Matchers for maps

Matchers for XML

Matchers for files

Matchers for partial functions

Other Matchers

Specs2 data tables

Running Specs2 tests

Summary

Chapter 9: Miscellaneous and Emerging Trends in Scala TDD

Scala Futures and Promises

ExecutionContext

Futures

The Inside trait

The OptionValue trait

The EitherValue trait

Eventually

How to configure eventually

Simple backoff algorithm

Integration patience

Consumer-Driven Contracts

How services interface with each other

The gigantic way

Microservices

Using CDC to integrate microservices

Traditional way

Using CDC

Benefits of CDC

Summary

Index

The users who browse this book also browse


No browse record.