Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying D Cookbook
  • Table Of Contents Toc
D Cookbook

D Cookbook

By : Adam Ruppe
5 (9)
close
close
D Cookbook

D Cookbook

5 (9)
By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
close
close
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Index

Unit testing your code


Unit tests are blocks of code that verify the correct output of one small part of your code (a unit) at a time, such as an individual function or class. D has a built-in support for basic unit testing. Here, we'll see what we can do with it.

How to do it…

Perform the following steps:

  1. Add unittest {} blocks to your modules, under your functions and inside your classes.

  2. Put any imports needed by the test inside the unittest block.

  3. You should write helper function and class definitions inside the unittest block.

  4. Write assertions to perform your tests.

  5. Use std.exception for additional helper functions.

  6. Compile with dmd –unittest yourfiles.d.

  7. Run the program.

    Tip

    Design your classes with dependency injection for easier mocking of tests. This is when instead of constructing child objects yourself, you accept them as arguments to the constructor. The unit test may then pass a dummy object to the class instead of, for example, a live network connection to test the code.

    Consider the following...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
D Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon