Book Image

Scala Test-Driven Development

By : Gaurav Sood
Book Image

Scala Test-Driven Development

By: Gaurav Sood

Overview of this book

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.
Table of Contents (16 chapters)
Scala Test-Driven Development
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface

Preface

Test-Driven Development (TDD) goes hand-in-hand with Agile practices, which is gradually becoming the undisputed standard process or delivering quality software on time. The TDD process originated from the need to better understand and refine the requirements for a system.

Scala has gradually, despite all apprehensions, made a niche for itself as the language of choice for delivering modern versatile systems and microservices. It is slowly and steadily replacing the conventional object-oriented models with more robust and immutable functional constructs. The popularity of Scala emanates from the fact that it provides a bridge for developers to make a transition from the object-oriented to the functional world.

Scala, like all other languages, can allow developers to get so engrossed in its intricacies and magic that the real purpose of the application can easily get lost in over-engineering. Therefore, TDD is required to keep the application code tightly tied to the requirements.

What this book covers

Chapter 1, Hello, TDD!, contains a brief introduction of TDD and Agile process. We discussed the benefits of TDD and how and why it needs to be used.

Chapter 2, First Test-Driven Application, creates our very first working application using TDD. The purpose of this chapter is to provide a quick win and give a taste of what lies ahead.

Chapter 3, Clean Code Using ScalaTest, discusses some of the principles of clean code and delves into the ScalaTest framework.

Chapter 4, Refactor Mercilessly, discusses various refactoring techniques and their benefits. The idea is to build a gradual appreciation of the refactoring process.

Chapter 5, Another Level of Testing, discusses the concepts of functional testing and behaviour-driven development.

Chapter 6, Mock Objects and Stubs, takes an in-depth look into the mocking frameworks that can be used with ScalaTest and Specs2 to enable mocking out the dependencies during testing.

Chapter 7, Property-Based Testing, discusses the techniques of writing tests that are driven from inputs provided in the form of tabular data or that is randomly generated.

Chapter 8, Scala TDD with Specs2, looks at an alternative testing framework for test-driving Scala code. We also compare ScalaTest and Specs2 so the reader can make an informed choice.

Chapter 9, Miscellaneous and Emerging Trends in Scala TDD, explores some new techniques, features, and processes that may soon become incorporated into the mainstream application development process.

What you need for this book

The following software is recommended for use with this book:

  • Scala

  • SBT

  • IntelliJ or Eclipse IDE

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.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The cancel() method forces the test to be interrupted."

A block of code is set as follows:

test("one plus one with result") {
  val two = 2
  assertResult(two) { 1 + 1 }
}

Any command-line input or output is written as follows:

  $ sbt test 
  [info] Loading project definition from /helloworld/project 
  [info] Set current project to Chap1 (in build file:/Packt/ helloworld /) 
  [info] Compiling 1 Scala source to /Packt/ helloworld 
  /target/scala/classes...

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Tail-call optimization is where we can escape allocating a new stack frame for a function."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Scala-Test-Driven-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.