1
1
# Unit Testing
2
2
3
+ ``` @meta
4
+ DocTestSetup = quote
5
+ using Base.Test
6
+ end
7
+ ```
8
+
3
9
## Testing Base Julia
4
10
5
11
Julia is under rapid development and has an extensive test suite to verify functionality across
@@ -26,7 +32,7 @@ Base.Test.@test_throws
26
32
27
33
For example, suppose we want to check our new function ` foo(x) ` works as expected:
28
34
29
- ``` julia-repl
35
+ ``` jldoctest testfoo
30
36
julia> using Base.Test
31
37
32
38
julia> foo(x) = length(x)^2
@@ -35,28 +41,22 @@ foo (generic function with 1 method)
35
41
36
42
If the condition is true, a ` Pass ` is returned:
37
43
38
- ``` julia-repl
44
+ ``` jldoctest testfoo
39
45
julia> @test foo("bar") == 9
40
46
Test Passed
41
- Expression: foo("bar") == 9
42
- Evaluated: 9 == 9
43
47
44
48
julia> @test foo("fizz") >= 10
45
49
Test Passed
46
- Expression: foo("fizz") >= 10
47
- Evaluated: 16 >= 10
48
50
```
49
51
50
52
If the condition is false, then a ` Fail ` is returned and an exception is thrown:
51
53
52
- ``` julia-repl
54
+ ``` jldoctest testfoo
53
55
julia> @test foo("f") == 20
54
56
Test Failed
55
57
Expression: foo("f") == 20
56
58
Evaluated: 1 == 20
57
59
ERROR: There was an error during testing
58
- in record at test.jl:268
59
- in do_test at test.jl:191
60
60
```
61
61
62
62
If the condition could not be evaluated because an exception was thrown, which occurs in this
@@ -68,23 +68,25 @@ julia> @test foo(:cat) == 1
68
68
Error During Test
69
69
Test threw an exception of type MethodError
70
70
Expression: foo(:cat) == 1
71
- MethodError: `length` has no method matching length(::Symbol)
72
- in foo at none:1
73
- in anonymous at test.jl:159
74
- in do_test at test.jl:180
71
+ MethodError: no method matching length(::Symbol)
72
+ Closest candidates are:
73
+ length(::SimpleVector) at essentials.jl:256
74
+ length(::Base.MethodList) at reflection.jl:521
75
+ length(::MethodTable) at reflection.jl:597
76
+ ...
77
+ Stacktrace:
78
+ [...]
75
79
ERROR: There was an error during testing
76
- in record at test.jl:268
77
- in do_test at test.jl:191
78
80
```
79
81
80
82
If we expect that evaluating an expression * should* throw an exception, then we can use ` @test_throws() `
81
83
to check that this occurs:
82
84
83
- ``` julia-repl
85
+ ``` jldoctest testfoo
84
86
julia> @test_throws MethodError foo(:cat)
85
87
Test Passed
86
- Expression: foo(:cat)
87
- Evaluated : MethodError
88
+
89
+ Thrown : MethodError
88
90
```
89
91
90
92
## Working with Test Sets
@@ -104,19 +106,19 @@ Base.Test.@testset
104
106
105
107
We can put our tests for the ` foo(x) ` function in a test set:
106
108
107
- ``` julia-repl
109
+ ``` jldoctest testfoo
108
110
julia> @testset "Foo Tests" begin
109
111
@test foo("a") == 1
110
112
@test foo("ab") == 4
111
113
@test foo("abc") == 9
112
- end
114
+ end;
113
115
Test Summary: | Pass Total
114
116
Foo Tests | 3 3
115
117
```
116
118
117
119
Test sets can also be nested:
118
120
119
- ``` julia-repl
121
+ ``` jldoctest testfoo
120
122
julia> @testset "Foo Tests" begin
121
123
@testset "Animals" begin
122
124
@test foo("cat") == 9
@@ -126,7 +128,7 @@ julia> @testset "Foo Tests" begin
126
128
@test foo(zeros(i)) == i^2
127
129
@test foo(ones(i)) == i^2
128
130
end
129
- end
131
+ end;
130
132
Test Summary: | Pass Total
131
133
Foo Tests | 8 8
132
134
```
@@ -153,14 +155,13 @@ julia> @testset "Foo Tests" begin
153
155
Arrays: Test Failed
154
156
Expression: foo(ones(4)) == 15
155
157
Evaluated: 16 == 15
156
- in record at test.jl:297
157
- in do_test at test.jl:191
158
+ Stacktrace:
159
+ [...]
158
160
Test Summary: | Pass Fail Total
159
161
Foo Tests | 3 1 4
160
162
Animals | 2 2
161
163
Arrays | 1 1 2
162
164
ERROR: Some tests did not pass: 3 passed, 1 failed, 0 errored, 0 broken.
163
- in finish at test.jl:362
164
165
```
165
166
166
167
## Other Test Macros
@@ -169,15 +170,15 @@ As calculations on floating-point values can be imprecise, you can perform appro
169
170
checks using either ` @test a ≈ b ` (where ` ≈ ` , typed via tab completion of ` \approx ` , is the
170
171
[ ` isapprox() ` ] ( @ref ) function) or use [ ` isapprox() ` ] ( @ref ) directly.
171
172
172
- ``` julia-repl
173
+ ``` jldoctest
173
174
julia> @test 1 ≈ 0.999999999
175
+ Test Passed
174
176
175
177
julia> @test 1 ≈ 0.999999
176
- ERROR: test failed: 1 isapprox 0.999999
177
- in expression: 1 ≈ 0.999999
178
- in error at error.jl:21
179
- in default_handler at test.jl:30
180
- in do_test at test.jl:53
178
+ Test Failed
179
+ Expression: 1 ≈ 0.999999
180
+ Evaluated: 1 isapprox 0.999999
181
+ ERROR: There was an error during testing
181
182
```
182
183
183
184
``` @docs
@@ -264,3 +265,7 @@ And using that testset looks like:
264
265
end
265
266
end
266
267
```
268
+
269
+ ``` @meta
270
+ DocTestSetup = nothing
271
+ ```
0 commit comments