Preface |
|
xiii | |
|
|
1 | (8) |
|
1.1 Interacting vs. Programming |
|
|
1 | (1) |
|
|
2 | (1) |
|
1.3 How Run-Time Testing Will Help You |
|
|
2 | (2) |
|
1.4 How Development-Time Testing Will Help You |
|
|
4 | (3) |
|
|
7 | (2) |
|
2 Run-Time Testing with assertive |
|
|
9 | (30) |
|
|
9 | (2) |
|
2.1.1 Chaining Assertions Together with Pipes |
|
|
10 | (1) |
|
2.2 Using Predicates: is and has Functions |
|
|
11 | (3) |
|
2.2.1 Exercise: Using Predicates and Assertions |
|
|
13 | (1) |
|
2.3 The Virtual Package System |
|
|
14 | (1) |
|
2.4 A Tour of the assertive Package |
|
|
14 | (18) |
|
|
14 | (2) |
|
2.4.2 assertive.properties |
|
|
16 | (3) |
|
|
19 | (1) |
|
|
20 | (2) |
|
2.4.5 Exercise: Examining an Object |
|
|
22 | (1) |
|
|
22 | (1) |
|
2.4.7 Exercise: Inspecting Files and Directories |
|
|
23 | (1) |
|
|
24 | (1) |
|
|
25 | (1) |
|
2.4.10 Exercise: Testing Properties of Matrices |
|
|
25 | (1) |
|
|
25 | (1) |
|
|
26 | (1) |
|
2.4.13 assertive.reflection |
|
|
27 | (1) |
|
2.4.14 Exercise: Explore Your R Setup |
|
|
28 | (1) |
|
2.4.15 assertive.datetimes |
|
|
28 | (1) |
|
2.4.16 assertive.data, assertive.data.us, and assertive.data.uk |
|
|
29 | (1) |
|
2.4.17 Exercise: Checking Customer Data |
|
|
30 | (1) |
|
|
31 | (1) |
|
|
32 | (1) |
|
2.6 Fail Early, Fail Often |
|
|
33 | (1) |
|
2.7 Case Study: Calculating the Geometric Mean |
|
|
34 | (3) |
|
2.7.1 Exercise: Calculating the Harmonic Mean |
|
|
37 | (1) |
|
|
37 | (1) |
|
|
38 | (1) |
|
3 Development-Time Testing with testthat |
|
|
39 | (24) |
|
|
39 | (1) |
|
3.2 The Structure of a Unit Test |
|
|
39 | (3) |
|
3.2.1 Exercise: Using expect_equal |
|
|
40 | (1) |
|
3.2.2 How Equal Is Equal? |
|
|
41 | (1) |
|
|
42 | (1) |
|
3.3.1 Exercise: Using expect_error |
|
|
42 | (1) |
|
3.4 Different Types of Expectation |
|
|
42 | (1) |
|
3.5 Testing Warnings and Messages |
|
|
43 | (3) |
|
3.5.1 Chaining Expectations Together with Pipes |
|
|
44 | (1) |
|
3.5.2 Exercise: Using expect_output |
|
|
45 | (1) |
|
3.5.3 Testing for Lack of Output |
|
|
45 | (1) |
|
3.6 Varying the Strictness of Expectations |
|
|
46 | (1) |
|
3.7 Providing Additional Information on Failure |
|
|
47 | (1) |
|
3.8 Case Study: Calculating Square Roots |
|
|
48 | (6) |
|
3.8.1 Exercise: Find More Tests for square_root |
|
|
53 | (1) |
|
|
54 | (3) |
|
3.9.1 Exercise: Testing the Return Type of Replicates |
|
|
56 | (1) |
|
3.10 Organising Tests Using Contexts |
|
|
57 | (1) |
|
|
57 | (2) |
|
3.12 Customizing How Test Results Are Reported |
|
|
59 | (1) |
|
|
60 | (1) |
|
|
61 | (2) |
|
4 Writing Easily Maintainable and Testable Code |
|
|
63 | (22) |
|
4.1 Don't Repeat Yourself |
|
|
63 | (9) |
|
4.1.1 Case Study: Drawing Lots of Plots |
|
|
64 | (4) |
|
4.1.2 Idea 1: Use Variables Rather Than Hard-Coded Values |
|
|
68 | (1) |
|
4.1.3 Idea 2: For Values That You Want To Change Everywhere, Update Global Settings |
|
|
69 | (1) |
|
4.1.4 Idea 3: Wrap the Contents into a Function |
|
|
70 | (1) |
|
4.1.5 Exercise: Reducing Duplication |
|
|
71 | (1) |
|
4.2 Keep It Simple, Stupid |
|
|
72 | (12) |
|
4.2.1 Simplifying Function Interfaces |
|
|
72 | (2) |
|
4.2.2 Idea 1: Pass Arguments for Advanced Functionality to Another Function |
|
|
74 | (2) |
|
4.2.3 Exercise: Outsourcing Argument Checking |
|
|
76 | (1) |
|
4.2.4 Idea 2: Having Wrapper Functions for Specific Use Cases |
|
|
76 | (1) |
|
4.2.5 Exercise: Wrappers for Formatting Currency |
|
|
77 | (1) |
|
4.2.6 Idea 3: Auto-Guessing Defaults |
|
|
77 | (1) |
|
4.2.7 Exercise: Providing Better Defaults for write.csv |
|
|
78 | (1) |
|
4.2.8 Idea 4: Split Functionality into Many Functions |
|
|
79 | (1) |
|
4.2.9 Exercise: Decomposing the quantile Function |
|
|
80 | (1) |
|
4.2.10 Cyclomatic Complexity |
|
|
80 | (3) |
|
4.2.11 How to Reduce Cyclomatic Complexity |
|
|
83 | (1) |
|
4.2.12 Exercise: Calculating Leap Years |
|
|
83 | (1) |
|
|
84 | (1) |
|
5 Integrating Testing into Packages |
|
|
85 | (14) |
|
5.1 How to Make an R Package |
|
|
85 | (2) |
|
|
85 | (1) |
|
5.1.2 The Package Directory Structure |
|
|
86 | (1) |
|
5.1.3 Including Tests in Your Package |
|
|
86 | (1) |
|
5.2 Case Study: The hypotenuser Package |
|
|
87 | (3) |
|
|
90 | (2) |
|
5.3.1 Exercise: Make a Package with Tests |
|
|
91 | (1) |
|
5.4 Using Version Control, Online Package Hosting, and Continuous Integration |
|
|
92 | (3) |
|
5.4.1 Version Control with git |
|
|
92 | (1) |
|
5.4.2 Online Project Hosting |
|
|
92 | (1) |
|
5.4.3 Continuous Integration Services |
|
|
93 | (2) |
|
5.5 Testing Packages on CRAN |
|
|
95 | (2) |
|
5.5.1 Testing Packages with r-hub |
|
|
96 | (1) |
|
5.6 Calculating Test Coverage Using coveralls.io |
|
|
97 | (1) |
|
|
98 | (1) |
|
6 Advanced Development-Time Testing |
|
|
99 | (28) |
|
6.1 Code with Side Effects |
|
|
99 | (2) |
|
6.1.1 Exercise: Writing a Test That Handles Side Effects |
|
|
101 | (1) |
|
6.2 Testing Complex Objects |
|
|
101 | (3) |
|
6.2.1 Exercise: Testing a Complex Object |
|
|
104 | (1) |
|
6.3 Testing Database Connections |
|
|
104 | (6) |
|
6.3.1 Option 1: Mock the Connection |
|
|
106 | (1) |
|
6.3.2 Option 2: Mock the Connection Wrapper |
|
|
107 | (1) |
|
6.3.3 Option 3: Mock the Specific Query Functions |
|
|
108 | (2) |
|
6.3.4 Summarizing the Pros and Cons of Each Database Method |
|
|
110 | (1) |
|
|
110 | (8) |
|
6.4.1 Getting Set Up to Use C++ |
|
|
111 | (3) |
|
6.4.2 Case Study: Extending the hypotenuser Package to Include C++ Code |
|
|
114 | (3) |
|
6.4.3 Exercise: Testing an Rcpp Function |
|
|
117 | (1) |
|
6.5 Testing Write Functions |
|
|
118 | (2) |
|
6.5.1 Exercise: Writing INI Configuration Files |
|
|
120 | (1) |
|
|
120 | (5) |
|
6.6.1 Generating the Report from the markdown |
|
|
124 | (1) |
|
6.6.2 Including Graphics Tests in Packages |
|
|
124 | (1) |
|
6.6.3 Exercise: Write a Graphics Test Report |
|
|
124 | (1) |
|
|
125 | (2) |
|
7 Writing Your Own Assertions and Expectations |
|
|
127 | (12) |
|
7.1 The Quick, Nearly Good Enough Option |
|
|
127 | (1) |
|
7.2 Writing Scalar Predicates |
|
|
128 | (2) |
|
7.2.1 Exercise: Writing a Custom Scalar Predicate |
|
|
129 | (1) |
|
7.2.2 Writing Type Predicates |
|
|
129 | (1) |
|
7.3 Writing Scalar Assertions |
|
|
130 | (1) |
|
7.3.1 Exercise: Writing a Custom Scalar Assertion |
|
|
131 | (1) |
|
7.4 Writing Vector Predicates |
|
|
131 | (2) |
|
7.4.1 Exercise: Writing a Vector Predicate |
|
|
132 | (1) |
|
7.5 Writing Vector Assertions |
|
|
133 | (2) |
|
7.5.1 Exercise: Writing a Vector Assertion |
|
|
134 | (1) |
|
7.6 Creating Custom Expectations |
|
|
135 | (2) |
|
7.6.1 Exercise: Create a Custom Expectation |
|
|
137 | (1) |
|
|
137 | (2) |
|
|
139 | (34) |
|
|
139 | (1) |
|
A.1.1 Exercise: Are You Ready? |
|
|
139 | (1) |
|
|
139 | (10) |
|
A.2.1 Exercise: Using Predicates and Assertions |
|
|
139 | (2) |
|
A.2.2 Exercise: Examining an Object |
|
|
141 | (1) |
|
A.2.3 Exercise: Inspecting Files and Directories |
|
|
142 | (1) |
|
A.2.4 Exercise: Testing Properties of Matrices |
|
|
143 | (1) |
|
A.2.5 Exercise: Explore Your R Setup |
|
|
143 | (1) |
|
A.2.6 Exercise: Checking Customer Data |
|
|
143 | (4) |
|
A.2.7 Exercise: Calculating the Harmonic Mean |
|
|
147 | (2) |
|
|
149 | (4) |
|
A.3.1 Exercise: Using expect_equal |
|
|
149 | (1) |
|
A.3.2 Exercise: Using expect_error |
|
|
150 | (1) |
|
A.3.3 Exercise: Using expect_output |
|
|
151 | (1) |
|
A.3.4 Exercise: Find More Tests for square_root |
|
|
151 | (1) |
|
A.3.5 Exercise: Testing the Return Type of Replicates |
|
|
152 | (1) |
|
|
153 | (6) |
|
A.4.1 Exercise: Reducing duplication |
|
|
153 | (2) |
|
A.4.2 Exercise: Outsourcing Argument Checking |
|
|
155 | (1) |
|
A.4.3 Exercise: Wrappers for Formatting Currency |
|
|
156 | (1) |
|
A.4.4 Exercise: Providing Better Defaults for write.csv |
|
|
157 | (1) |
|
A.4.5 Exercise: Decomposing the quantile Function |
|
|
157 | (1) |
|
A.4.6 Exercise: Calculating Leap Years |
|
|
158 | (1) |
|
|
159 | (3) |
|
A.5.1 Exercise: Make a Package with Tests |
|
|
159 | (3) |
|
|
162 | (5) |
|
A.6.1 Exercise: Writing a Test That Handles Side Effects |
|
|
162 | (1) |
|
A.6.2 Exercise: Testing a Complex Object |
|
|
162 | (1) |
|
A.6.3 Exercise: Testing an Rcpp Function |
|
|
163 | (2) |
|
A.6.4 Exercise: Writing INI Configuration Files |
|
|
165 | (2) |
|
A.6.5 Exercise: Write a Graphics Test Report |
|
|
167 | (1) |
|
|
167 | (6) |
|
A.7.1 Exercise: Writing a Custom Scalar Predicate |
|
|
167 | (1) |
|
A.7.2 Exercise: Writing a Custom Scalar Assertion |
|
|
168 | (1) |
|
A.7.3 Exercise: Writing a Vector Predicate |
|
|
169 | (1) |
|
A.7.4 Exercise: Writing a Vector Assertion |
|
|
169 | (1) |
|
A.7.5 Exercise: Create a Custom Expectation |
|
|
170 | (3) |
Bibliography |
|
173 | (4) |
Concept Index |
|
177 | (2) |
Package Index |
|
179 | (2) |
Dataset Index |
|
181 | (2) |
People Index |
|
183 | (2) |
Function Index |
|
185 | |