Skip to content

Commit 4ef09d3

Browse files
committed
add some doctest to test
gdffg
1 parent 9b46a0b commit 4ef09d3

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

doc/src/stdlib/test.md

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Unit Testing
22

3+
```@meta
4+
DocTestSetup = quote
5+
using Base.Test
6+
end
7+
```
8+
39
## Testing Base Julia
410

511
Julia is under rapid development and has an extensive test suite to verify functionality across
@@ -26,7 +32,7 @@ Base.Test.@test_throws
2632

2733
For example, suppose we want to check our new function `foo(x)` works as expected:
2834

29-
```julia-repl
35+
```jldoctest testfoo
3036
julia> using Base.Test
3137
3238
julia> foo(x) = length(x)^2
@@ -35,28 +41,22 @@ foo (generic function with 1 method)
3541

3642
If the condition is true, a `Pass` is returned:
3743

38-
```julia-repl
44+
```jldoctest testfoo
3945
julia> @test foo("bar") == 9
4046
Test Passed
41-
Expression: foo("bar") == 9
42-
Evaluated: 9 == 9
4347
4448
julia> @test foo("fizz") >= 10
4549
Test Passed
46-
Expression: foo("fizz") >= 10
47-
Evaluated: 16 >= 10
4850
```
4951

5052
If the condition is false, then a `Fail` is returned and an exception is thrown:
5153

52-
```julia-repl
54+
```jldoctest testfoo
5355
julia> @test foo("f") == 20
5456
Test Failed
5557
Expression: foo("f") == 20
5658
Evaluated: 1 == 20
5759
ERROR: There was an error during testing
58-
in record at test.jl:268
59-
in do_test at test.jl:191
6060
```
6161

6262
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
6868
Error During Test
6969
Test threw an exception of type MethodError
7070
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+
[...]
7579
ERROR: There was an error during testing
76-
in record at test.jl:268
77-
in do_test at test.jl:191
7880
```
7981

8082
If we expect that evaluating an expression *should* throw an exception, then we can use `@test_throws()`
8183
to check that this occurs:
8284

83-
```julia-repl
85+
```jldoctest testfoo
8486
julia> @test_throws MethodError foo(:cat)
8587
Test Passed
86-
Expression: foo(:cat)
87-
Evaluated: MethodError
88+
89+
Thrown: MethodError
8890
```
8991

9092
## Working with Test Sets
@@ -104,19 +106,19 @@ Base.Test.@testset
104106

105107
We can put our tests for the `foo(x)` function in a test set:
106108

107-
```julia-repl
109+
```jldoctest testfoo
108110
julia> @testset "Foo Tests" begin
109111
@test foo("a") == 1
110112
@test foo("ab") == 4
111113
@test foo("abc") == 9
112-
end
114+
end;
113115
Test Summary: | Pass Total
114116
Foo Tests | 3 3
115117
```
116118

117119
Test sets can also be nested:
118120

119-
```julia-repl
121+
```jldoctest testfoo
120122
julia> @testset "Foo Tests" begin
121123
@testset "Animals" begin
122124
@test foo("cat") == 9
@@ -126,7 +128,7 @@ julia> @testset "Foo Tests" begin
126128
@test foo(zeros(i)) == i^2
127129
@test foo(ones(i)) == i^2
128130
end
129-
end
131+
end;
130132
Test Summary: | Pass Total
131133
Foo Tests | 8 8
132134
```
@@ -153,14 +155,13 @@ julia> @testset "Foo Tests" begin
153155
Arrays: Test Failed
154156
Expression: foo(ones(4)) == 15
155157
Evaluated: 16 == 15
156-
in record at test.jl:297
157-
in do_test at test.jl:191
158+
Stacktrace:
159+
[...]
158160
Test Summary: | Pass Fail Total
159161
Foo Tests | 3 1 4
160162
Animals | 2 2
161163
Arrays | 1 1 2
162164
ERROR: Some tests did not pass: 3 passed, 1 failed, 0 errored, 0 broken.
163-
in finish at test.jl:362
164165
```
165166

166167
## Other Test Macros
@@ -169,15 +170,15 @@ As calculations on floating-point values can be imprecise, you can perform appro
169170
checks using either `@test a ≈ b` (where ``, typed via tab completion of `\approx`, is the
170171
[`isapprox()`](@ref) function) or use [`isapprox()`](@ref) directly.
171172

172-
```julia-repl
173+
```jldoctest
173174
julia> @test 1 ≈ 0.999999999
175+
Test Passed
174176
175177
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
181182
```
182183

183184
```@docs
@@ -264,3 +265,7 @@ And using that testset looks like:
264265
end
265266
end
266267
```
268+
269+
```@meta
270+
DocTestSetup = nothing
271+
```

0 commit comments

Comments
 (0)